Skip to content

Commit 1295f80

Browse files
authored
Avoid making getUserByToken() depend on cached state.
1 parent 5126141 commit 1295f80

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

app/Providers/Socialite/GenericSocialiteProvider.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Providers\Socialite;
44

55
use GuzzleHttp\Exception\GuzzleException;
6+
use Illuminate\Support\Arr;
67
use Laravel\Socialite\Two\AbstractProvider;
78
use Laravel\Socialite\Two\ProviderInterface;
89
use Laravel\Socialite\Two\User;
@@ -42,7 +43,6 @@ class GenericSocialiteProvider extends AbstractProvider implements ProviderInter
4243
* {@inheritdoc}
4344
*/
4445
protected $scopeSeparator = ' ';
45-
protected $idToken;
4646

4747
/**
4848
* Return provider Url.
@@ -99,35 +99,43 @@ protected function getTokenUrl()
9999
return $this->getOIDCUrl() . '/token';
100100
}
101101

102-
/**
103-
* Get the access token response for the given code.
104-
*
105-
* @param string $code
106-
* @return mixed
102+
/**
103+
* {@inheritdoc}
107104
*/
108-
public function getAccessTokenResponse($code)
105+
public function user()
109106
{
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);
113120
}
114121

122+
115123
/**
116124
* @param string $token
117125
*
118126
* @throws GuzzleException
119127
*
120128
* @return array|mixed
121129
*/
122-
protected function getUserByToken($token)
130+
protected function getUserByToken($token, $idToken = null)
123131
{
124132
$useIdToken = config('services.oidc.use_id_token', false);
125133

126134
if ($useIdToken) {
127-
if (!$this->idToken) {
135+
if (!$idToken) {
128136
throw new \Exception('OIDC_USE_ID_TOKEN=true but id_token not received');
129137
}
130-
return $this->decodeIdToken($this->idToken);
138+
return $this->decodeIdToken($idToken);
131139
}
132140

133141
$base_url = $this->getOIDCUrl() . '/userinfo';

0 commit comments

Comments
 (0)