|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DesignMyNight\Laravel\OAuth2; |
| 4 | + |
| 5 | +use GuzzleHttp\Exception\RequestException; |
| 6 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 7 | +use Illuminate\Auth\AuthenticationException; |
| 8 | +use Illuminate\Foundation\Auth\User; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Laravel\Passport\Exceptions\MissingScopeException; |
| 11 | + |
| 12 | +class Introspect |
| 13 | +{ |
| 14 | + protected $accessTokenCacheKey = 'access_token'; |
| 15 | + protected $client = null; |
| 16 | + protected $result; |
| 17 | + protected $userDataKey = 'user'; |
| 18 | + protected $userModelClass = User::class; |
| 19 | + |
| 20 | + public function __construct(IntrospectClient $client, Request $request) |
| 21 | + { |
| 22 | + $this->client = $client; |
| 23 | + $this->request = $request; |
| 24 | + } |
| 25 | + |
| 26 | + protected function getIntrospectionResult() |
| 27 | + { |
| 28 | + if ($this->result !== null) { |
| 29 | + return $this->result; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + $this->result = $this->client->introspect($this->request->bearerToken()); |
| 34 | + } catch (RequestException $e) { |
| 35 | + if ($e->hasResponse()) { |
| 36 | + $result = json_decode((string) $e->getResponse()->getBody(), true); |
| 37 | + |
| 38 | + if (isset($result['error'])) { |
| 39 | + throw new AuthenticationException($result['error']['title'] ?? ''); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + throw new AuthenticationException($e->getMessage()); |
| 44 | + } |
| 45 | + |
| 46 | + return $this->result; |
| 47 | + } |
| 48 | + |
| 49 | + public function mustHaveScopes(array $requiredScopes = []) |
| 50 | + { |
| 51 | + $result = $this->getIntrospectionResult(); |
| 52 | + $givenScopes = explode(' ', $result['scope']); |
| 53 | + $missingScopes = array_diff($requiredScopes, $givenScopes); |
| 54 | + |
| 55 | + if (count($missingScopes) > 0) { |
| 56 | + throw new MissingScopeException($missingScopes); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function setUserDataKey(string $key): self |
| 61 | + { |
| 62 | + $this->userDataKey = $key; |
| 63 | + |
| 64 | + return $this; |
| 65 | + } |
| 66 | + |
| 67 | + public function setUserModelClass(string $class): self |
| 68 | + { |
| 69 | + $this->userModelClass = $class; |
| 70 | + |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + public function tokenIsActive(): bool |
| 75 | + { |
| 76 | + $result = $this->getIntrospectionResult(); |
| 77 | + |
| 78 | + return $result['active'] === true; |
| 79 | + } |
| 80 | + |
| 81 | + public function tokenIsNotActive(): bool |
| 82 | + { |
| 83 | + return !$this->tokenIsActive(); |
| 84 | + } |
| 85 | + |
| 86 | + public function getUser() |
| 87 | + { |
| 88 | + $result = $this->getIntrospectionResult(); |
| 89 | + |
| 90 | + if (isset($result[$this->userDataKey]) && !empty($result[$this->userDataKey])) { |
| 91 | + $user = $this->getUserModel(); |
| 92 | + $user->forceFill($result[$this->userDataKey]); |
| 93 | + |
| 94 | + return $user; |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + public function getUserModel(): Authenticatable |
| 101 | + { |
| 102 | + $class = $this->getUserModelClass(); |
| 103 | + |
| 104 | + return new $class(); |
| 105 | + } |
| 106 | + |
| 107 | + public function getUserModelClass(): string |
| 108 | + { |
| 109 | + return $this->userModelClass; |
| 110 | + } |
| 111 | + |
| 112 | + public function verifyToken() |
| 113 | + { |
| 114 | + if ($this->tokenIsNotActive()) { |
| 115 | + throw new AuthenticationException('Invalid token'); |
| 116 | + } |
| 117 | + |
| 118 | + return $this; |
| 119 | + } |
| 120 | +} |
0 commit comments