JwtGuard.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Services\Auth;
  3. use App\MicroApi\Items\UserItem;
  4. use Illuminate\Auth\GuardHelpers;
  5. use Illuminate\Contracts\Auth\Authenticatable;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Contracts\Auth\Guard;
  8. use Illuminate\Contracts\Auth\UserProvider;
  9. class JwtGuard implements Guard
  10. {
  11. use GuardHelpers;
  12. /**
  13. * The request instance.
  14. *
  15. * @var \Illuminate\Http\Request
  16. */
  17. protected $request;
  18. /**
  19. * The name of the query string item from the request containing the API token.
  20. *
  21. * @var string
  22. */
  23. protected $inputKey;
  24. /**
  25. * The name of the token "column" in persistent storage.
  26. *
  27. * @var string
  28. */
  29. protected $storageKey;
  30. /**
  31. * Create a new authentication guard.
  32. *
  33. * @param \Illuminate\Contracts\Auth\UserProvider $provider
  34. * @param \Illuminate\Http\Request $request
  35. * @param string $inputKey
  36. * @param string $storageKey
  37. * @return void
  38. */
  39. public function __construct(UserProvider $provider, Request $request, $inputKey = 'jwt_token', $storageKey = 'jwt_token')
  40. {
  41. $this->request = $request;
  42. $this->provider = $provider;
  43. $this->inputKey = $inputKey;
  44. $this->storageKey = $storageKey;
  45. }
  46. /**
  47. * Get the currently authenticated user.
  48. *
  49. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  50. */
  51. public function user()
  52. {
  53. // If we've already retrieved the user for the current request we can just
  54. // return it back immediately. We do not want to fetch the user data on
  55. // every call to this method because that would be tremendously slow.
  56. if (!is_null($this->user)) {
  57. return $this->user;
  58. }
  59. $user = null;
  60. $token = $this->getTokenForRequest();
  61. if (!empty($token)) {
  62. $user = $this->provider->retrieveByToken(null, $token);
  63. }
  64. return $this->user = $user;
  65. }
  66. /**
  67. * Attempt to authenticate a user using the given credentials.
  68. *
  69. * @param array $credentials
  70. * @return Authenticatable|null
  71. */
  72. public function login(array $credentials)
  73. {
  74. $token = $this->provider->retrieveByCredentials($credentials);
  75. // If an implementation of UserInterface was returned, we'll ask the provider
  76. // to validate the user against the given credentials, and if they are in
  77. // fact valid we'll log the users into the application and return true.
  78. if ($token) {
  79. $user = $this->provider->retrieveByToken(null, $token);
  80. $this->setUser($user);
  81. }
  82. return $token;
  83. }
  84. /**
  85. * Get the token for the current request.
  86. *
  87. * @return string
  88. */
  89. public function getTokenForRequest()
  90. {
  91. $token = $this->request->query($this->inputKey);
  92. if (empty($token)) {
  93. $token = $this->request->input($this->inputKey);
  94. }
  95. if (empty($token)) {
  96. $token = $this->request->bearerToken();
  97. }
  98. if (empty($token)) {
  99. $token = $this->request->cookie($this->inputKey);
  100. }
  101. return $token;
  102. }
  103. /**
  104. * Validate a user's credentials.
  105. *
  106. * @param array $credentials
  107. * @return bool
  108. */
  109. public function validate(array $credentials = [])
  110. {
  111. if (empty($credentials[$this->inputKey])) {
  112. return false;
  113. }
  114. $credentials = [$this->storageKey => $credentials[$this->inputKey]];
  115. if ($this->provider->validateCredentials(new UserItem, $credentials)) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Set the current request instance.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @return $this
  125. */
  126. public function setRequest(Request $request)
  127. {
  128. $this->request = $request;
  129. return $this;
  130. }
  131. }