| Total Complexity | 23 |
| Total Lines | 174 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | class OidConnectGuard implements Guard { |
||
| 17 | |||
| 18 | protected $request; |
||
|
1 ignored issue
–
show
|
|||
| 19 | protected $provider; |
||
|
1 ignored issue
–
show
|
|||
| 20 | protected $requestTokenParser; |
||
|
1 ignored issue
–
show
|
|||
| 21 | protected $jwtValidator; |
||
|
1 ignored issue
–
show
|
|||
| 22 | protected $tokenRefresher; |
||
|
1 ignored issue
–
show
|
|||
| 23 | |||
| 24 | protected $user; |
||
|
1 ignored issue
–
show
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * Set to true when user() has already ran once. |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $userAlreadyAttempted; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a new authentication guard. |
||
| 34 | * |
||
| 35 | * |
||
| 36 | * @param UserProvider $provider |
||
|
2 ignored issues
–
show
|
|||
| 37 | * @param RequestTokenParser $requestTokenParser |
||
|
1 ignored issue
–
show
|
|||
| 38 | * @param JwtValidator $jwtValidator |
||
|
2 ignored issues
–
show
|
|||
| 39 | * @param TokenRefresher $tokenRefresher |
||
|
2 ignored issues
–
show
|
|||
| 40 | * @param Request $request |
||
|
2 ignored issues
–
show
|
|||
| 41 | */ |
||
| 42 | public function __construct(UserProvider $provider, |
||
| 43 | RequestTokenParser $requestTokenParser, |
||
| 44 | JwtValidator $jwtValidator, |
||
| 45 | TokenRefresher $tokenRefresher, |
||
| 46 | Request $request) { |
||
| 47 | $this->request = $request; |
||
| 48 | $this->provider = $provider; |
||
| 49 | $this->requestTokenParser = $requestTokenParser; |
||
| 50 | $this->jwtValidator = $jwtValidator; |
||
| 51 | $this->tokenRefresher = $tokenRefresher; |
||
| 52 | $this->user = NULL; |
||
| 53 | $this->userAlreadyAttempted = false; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Determine if the current user is authenticated. |
||
| 58 | * |
||
| 59 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 60 | */ |
||
| 61 | public function check() { |
||
| 62 | return !is_null($this->user()); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Determine if the current user is a guest. |
||
| 67 | * |
||
| 68 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 69 | */ |
||
| 70 | public function guest() { |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get the ID for the currently authenticated user. |
||
| 76 | * |
||
| 77 | * @return int|null |
||
|
1 ignored issue
–
show
|
|||
| 78 | */ |
||
| 79 | public function id() |
||
| 80 | { |
||
| 81 | if ($this->user()) { |
||
| 82 | return $this->user()->getAuthIdentifier(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set the current user. |
||
| 88 | * |
||
| 89 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
|
2 ignored issues
–
show
|
|||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function setUser(Authenticatable $user) |
||
| 95 | } |
||
| 96 | |||
| 97 | public function user() { |
||
|
1 ignored issue
–
show
|
|||
| 98 | // If we've already retrieved the user for the current request we can just |
||
| 99 | // return it back immediately. We do not want to fetch the user data on |
||
| 100 | // every call to this method because that would be tremendously slow. |
||
| 101 | if (! is_null($this->user) || $this->userAlreadyAttempted) { |
||
| 102 | return $this->user; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->userAlreadyAttempted = true; |
||
| 106 | $user = null; |
||
| 107 | |||
| 108 | try { |
||
| 109 | $idToken = $this->requestTokenParser->parse($this->request); |
||
| 110 | } catch (AuthenticationException $exception) { |
||
| 111 | //Return a null user is enough, swallow the exception here |
||
| 112 | return $user; |
||
| 113 | } |
||
| 114 | |||
| 115 | if (!$this->jwtValidator->claimsAreValid($idToken) || |
||
| 116 | !$this->jwtValidator->signatureIsValid($idToken)) { |
||
| 117 | return $user; |
||
| 118 | } |
||
| 119 | |||
| 120 | //At this point, token is definitely valid |
||
| 121 | if ($this->jwtValidator->isExpired($idToken)) { |
||
| 122 | |||
| 123 | $iss = $idToken->getClaim("iss"); |
||
| 124 | $sub = $idToken->getClaim("sub"); |
||
| 125 | try { |
||
| 126 | $idToken = $this->tokenRefresher->refreshIDToken($iss, $sub); |
||
| 127 | |||
| 128 | } catch (TokenStorageException $storageException) { |
||
| 129 | //DO NOTHING |
||
| 130 | } catch (TokenRequestException $requestException) { |
||
| 131 | return $user; |
||
| 132 | } |
||
| 133 | $this->requestTokenParser->save($idToken); |
||
| 134 | } |
||
| 135 | |||
| 136 | $credentials = $idToken->getClaims(); |
||
| 137 | |||
| 138 | $user = $this->provider->retrieveByCredentials($credentials); |
||
| 139 | |||
| 140 | $this->user = $user; |
||
| 141 | return $user; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function validate(array $credentials = array()) { |
||
|
1 ignored issue
–
show
|
|||
| 145 | if (empty($credentials['id_token'])) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | $token = $this->requestTokenParser->parseFromString($credentials['id_token']); |
||
| 149 | |||
| 150 | return $this->jwtValidator->claimsAreValid($idToken) && |
||
| 151 | !$this->jwtValidator->isExpired($idToken) && |
||
| 152 | $this->jwtValidator->signatureIsValid($idToken); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the user provider used by the guard. |
||
| 157 | * |
||
| 158 | * @return \Illuminate\Contracts\Auth\UserProvider |
||
| 159 | */ |
||
| 160 | public function getProvider() |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the user provider used by the guard. |
||
| 167 | * |
||
| 168 | * @param \Illuminate\Contracts\Auth\UserProvider $provider |
||
|
2 ignored issues
–
show
|
|||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function setProvider(UserProvider $provider) |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Determine if the current user is authenticated. |
||
| 178 | * |
||
| 179 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
| 180 | * |
||
| 181 | * @throws \Illuminate\Auth\AuthenticationException |
||
| 182 | */ |
||
| 183 | public function authenticate() |
||
| 192 |