JwtGuard.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * Indicates if the logout method has been called.
  32. *
  33. * @var bool
  34. */
  35. protected $loggedOut = false;
  36. /**
  37. * Create a new authentication guard.
  38. *
  39. * @param \Illuminate\Contracts\Auth\UserProvider $provider
  40. * @param \Illuminate\Http\Request $request
  41. * @param string $inputKey
  42. * @param string $storageKey
  43. * @return void
  44. */
  45. public function __construct(UserProvider $provider, Request $request, $inputKey = 'jwt_token', $storageKey = 'jwt_token')
  46. {
  47. $this->request = $request;
  48. $this->provider = $provider;
  49. $this->inputKey = $inputKey;
  50. $this->storageKey = $storageKey;
  51. }
  52. /**
  53. * Get the currently authenticated user.
  54. *
  55. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  56. */
  57. public function user()
  58. {
  59. // If we've already retrieved the user for the current request we can just
  60. // return it back immediately. We do not want to fetch the user data on
  61. // every call to this method because that would be tremendously slow.
  62. if (!is_null($this->user)) {
  63. return $this->user;
  64. }
  65. $user = null;
  66. $token = $this->getTokenForRequest();
  67. if (!empty($token)) {
  68. $user = $this->provider->retrieveByToken(null, $token);
  69. }
  70. return $this->user = $user;
  71. }
  72. /**
  73. * Attempt to authenticate a user using the given credentials.
  74. *
  75. * @param array $credentials
  76. * @return Authenticatable|null
  77. */
  78. public function login(array $credentials)
  79. {
  80. $token = $this->provider->retrieveByCredentials($credentials);
  81. // If an implementation of UserInterface was returned, we'll ask the provider
  82. // to validate the user against the given credentials, and if they are in
  83. // fact valid we'll log the users into the application and return true.
  84. if ($token) {
  85. $user = $this->provider->retrieveByToken(null, $token);
  86. $this->setUser($user);
  87. }
  88. return $token;
  89. }
  90. /**
  91. * Get the token for the current request.
  92. *
  93. * @return string
  94. */
  95. public function getTokenForRequest()
  96. {
  97. $token = $this->request->query($this->inputKey);
  98. if (empty($token)) {
  99. $token = $this->request->input($this->inputKey);
  100. }
  101. if (empty($token)) {
  102. $token = $this->request->bearerToken();
  103. }
  104. if (empty($token)) {
  105. $token = $this->request->cookie($this->inputKey);
  106. }
  107. return $token;
  108. }
  109. /**
  110. * Validate a user's credentials.
  111. *
  112. * @param array $credentials
  113. * @return bool
  114. */
  115. public function validate(array $credentials = [])
  116. {
  117. if (empty($credentials[$this->inputKey])) {
  118. return false;
  119. }
  120. $credentials = [$this->storageKey => $credentials[$this->inputKey]];
  121. if ($this->provider->validateCredentials(new UserItem, $credentials)) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. * Set the current request instance.
  128. *
  129. * @param \Illuminate\Http\Request $request
  130. * @return $this
  131. */
  132. public function setRequest(Request $request)
  133. {
  134. $this->request = $request;
  135. return $this;
  136. }
  137. /**
  138. * log out
  139. */
  140. public function logout()
  141. {
  142. $this->user = null;
  143. $this->loggedOut = true;
  144. }
  145. }