123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?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;
-
- protected $request;
-
- protected $inputKey;
-
- protected $storageKey;
-
- protected $loggedOut = false;
-
- 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;
- }
-
- public function user()
- {
-
-
-
- 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;
- }
-
- public function login(array $credentials)
- {
- $user = $this->provider->retrieveByCredentials($credentials);
- $token = null;
- if ($user && $token = $this->provider->validateCredentials($user, $credentials)) {
- $this->setUser($user);
- }
- return $token;
- }
-
- 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;
- }
-
- 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;
- }
-
- public function setRequest(Request $request)
- {
- $this->request = $request;
- return $this;
- }
-
- public function logout()
- {
- $this->user = null;
- $this->loggedOut = true;
- }
- }
|