123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App\Services\Auth;
- use App\MicroApi\Items\UserItem;
- use Illuminate\Auth\GuardHelpers;
- use Illuminate\Contracts\Auth\Authenticatable;
- use Illuminate\Http\Request;
- use Illuminate\Contracts\Auth\Guard;
- use Illuminate\Contracts\Auth\UserProvider;
- class JwtGuard implements Guard
- {
- use GuardHelpers;
- /**
- * The request instance.
- *
- * @var \Illuminate\Http\Request
- */
- protected $request;
- /**
- * The name of the query string item from the request containing the API token.
- *
- * @var string
- */
- protected $inputKey;
- /**
- * The name of the token "column" in persistent storage.
- *
- * @var string
- */
- protected $storageKey;
- /**
- * Indicates if the logout method has been called.
- *
- * @var bool
- */
- protected $loggedOut = false;
- /**
- * Create a new authentication guard.
- *
- * @param \Illuminate\Contracts\Auth\UserProvider $provider
- * @param \Illuminate\Http\Request $request
- * @param string $inputKey
- * @param string $storageKey
- * @return void
- */
- public function __construct(UserProvider $provider, Request $request, $inputKey = 'jwt_token', $storageKey = 'jwt_token')
- {
- $this->request = $request;
- $this->provider = $provider;
- $this->inputKey = $inputKey;
- $this->storageKey = $storageKey;
- }
- /**
- * Get the currently authenticated user.
- *
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function user()
- {
- // If we've already retrieved the user for the current request we can just
- // return it back immediately. We do not want to fetch the user data on
- // every call to this method because that would be tremendously slow.
- if (!is_null($this->user)) {
- return $this->user;
- }
- $user = null;
- $token = $this->getTokenForRequest();
- if (!empty($token)) {
- $user = $this->provider->retrieveByToken(null, $token);
- }
- return $this->user = $user;
- }
- /**
- * Attempt to authenticate a user using the given credentials.
- *
- * @param array $credentials
- * @return Authenticatable|null
- */
- public function login(array $credentials)
- {
- $token = $this->provider->retrieveByCredentials($credentials);
- // If an implementation of UserInterface was returned, we'll ask the provider
- // to validate the user against the given credentials, and if they are in
- // fact valid we'll log the users into the application and return true.
- if ($token) {
- $user = $this->provider->retrieveByToken(null, $token);
- $this->setUser($user);
- }
- return $token;
- }
- /**
- * Get the token for the current request.
- *
- * @return string
- */
- public function getTokenForRequest()
- {
- $token = $this->request->query($this->inputKey);
- if (empty($token)) {
- $token = $this->request->input($this->inputKey);
- }
- if (empty($token)) {
- $token = $this->request->bearerToken();
- }
- if (empty($token)) {
- $token = $this->request->cookie($this->inputKey);
- }
- return $token;
- }
- /**
- * Validate a user's credentials.
- *
- * @param array $credentials
- * @return bool
- */
- public function validate(array $credentials = [])
- {
- if (empty($credentials[$this->inputKey])) {
- return false;
- }
- $credentials = [$this->storageKey => $credentials[$this->inputKey]];
- if ($this->provider->validateCredentials(new UserItem, $credentials)) {
- return true;
- }
- return false;
- }
- /**
- * Set the current request instance.
- *
- * @param \Illuminate\Http\Request $request
- * @return $this
- */
- public function setRequest(Request $request)
- {
- $this->request = $request;
- return $this;
- }
- /**
- * log out
- */
- public function logout()
- {
- $this->user = null;
- $this->loggedOut = true;
- }
- }
|