|
3 | 3 | namespace App\Providers\Socialite; |
4 | 4 |
|
5 | 5 | use GuzzleHttp\Exception\GuzzleException; |
| 6 | +use Illuminate\Support\Arr; |
6 | 7 | use Laravel\Socialite\Two\AbstractProvider; |
7 | 8 | use Laravel\Socialite\Two\ProviderInterface; |
8 | 9 | use Laravel\Socialite\Two\User; |
@@ -42,7 +43,6 @@ class GenericSocialiteProvider extends AbstractProvider implements ProviderInter |
42 | 43 | * {@inheritdoc} |
43 | 44 | */ |
44 | 45 | protected $scopeSeparator = ' '; |
45 | | - protected $idToken; |
46 | 46 |
|
47 | 47 | /** |
48 | 48 | * Return provider Url. |
@@ -99,35 +99,43 @@ protected function getTokenUrl() |
99 | 99 | return $this->getOIDCUrl() . '/token'; |
100 | 100 | } |
101 | 101 |
|
102 | | - /** |
103 | | - * Get the access token response for the given code. |
104 | | - * |
105 | | - * @param string $code |
106 | | - * @return mixed |
| 102 | + /** |
| 103 | + * {@inheritdoc} |
107 | 104 | */ |
108 | | - public function getAccessTokenResponse($code) |
| 105 | + public function user() |
109 | 106 | { |
110 | | - $response = parent::getAccessTokenResponse($code); |
111 | | - $this->idToken = $response['id_token'] ?? null; |
112 | | - return $response; |
| 107 | + if ($this->user) { |
| 108 | + return $this->user; |
| 109 | + } |
| 110 | + |
| 111 | + if ($this->hasInvalidState()) { |
| 112 | + throw new InvalidStateException; |
| 113 | + } |
| 114 | + |
| 115 | + $response = $this->getAccessTokenResponse($this->getCode()); |
| 116 | + |
| 117 | + $user = $this->getUserByToken(Arr::get($response, 'access_token'), Arr::get($response, 'id_token')); |
| 118 | + |
| 119 | + return $this->userInstance($response, $user); |
113 | 120 | } |
114 | 121 |
|
| 122 | + |
115 | 123 | /** |
116 | 124 | * @param string $token |
117 | 125 | * |
118 | 126 | * @throws GuzzleException |
119 | 127 | * |
120 | 128 | * @return array|mixed |
121 | 129 | */ |
122 | | - protected function getUserByToken($token) |
| 130 | + protected function getUserByToken($token, $idToken = null) |
123 | 131 | { |
124 | 132 | $useIdToken = config('services.oidc.use_id_token', false); |
125 | 133 |
|
126 | 134 | if ($useIdToken) { |
127 | | - if (!$this->idToken) { |
| 135 | + if (!$idToken) { |
128 | 136 | throw new \Exception('OIDC_USE_ID_TOKEN=true but id_token not received'); |
129 | 137 | } |
130 | | - return $this->decodeIdToken($this->idToken); |
| 138 | + return $this->decodeIdToken($idToken); |
131 | 139 | } |
132 | 140 |
|
133 | 141 | $base_url = $this->getOIDCUrl() . '/userinfo'; |
|
0 commit comments