JwtGuard.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $user = $this->provider->retrieveByCredentials($credentials);
  81. $token = null;
  82. if ($user && $token = $this->provider->validateCredentials($user, $credentials)) {
  83. $this->setUser($user);
  84. }
  85. return $token;
  86. }
  87. /**
  88. * Get the token for the current request.
  89. *
  90. * @return string
  91. */
  92. public function getTokenForRequest()
  93. {
  94. $token = $this->request->query($this->inputKey);
  95. if (empty($token)) {
  96. $token = $this->request->input($this->inputKey);
  97. }
  98. if (empty($token)) {
  99. $token = $this->request->bearerToken();
  100. }
  101. if (empty($token)) {
  102. $token = $this->request->cookie($this->inputKey);
  103. }
  104. return $token;
  105. }
  106. /**
  107. * Validate a user's credentials.
  108. *
  109. * @param array $credentials
  110. * @return bool
  111. */
  112. public function validate(array $credentials = [])
  113. {
  114. if (empty($credentials[$this->inputKey])) {
  115. return false;
  116. }
  117. $credentials = [$this->storageKey => $credentials[$this->inputKey]];
  118. if ($this->provider->validateCredentials(new UserItem, $credentials)) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. /**
  124. * Set the current request instance.
  125. *
  126. * @param \Illuminate\Http\Request $request
  127. * @return $this
  128. */
  129. public function setRequest(Request $request)
  130. {
  131. $this->request = $request;
  132. return $this;
  133. }
  134. /**
  135. * log out
  136. */
  137. public function logout()
  138. {
  139. $this->user = null;
  140. $this->loggedOut = true;
  141. }
  142. }