forked from dbarzin/deming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericSocialiteProvider.php
More file actions
208 lines (182 loc) · 5.81 KB
/
GenericSocialiteProvider.php
File metadata and controls
208 lines (182 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace App\Providers\Socialite;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Log;
/**
* Generic OpenId Connect provider for Socialite.
*/
class GenericSocialiteProvider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
public const IDENTIFIER = 'OIDC';
/**
* Scope definitions.
*/
public const SCOPE_EMAIL = 'email';
public const SCOPE_OPENID = 'openid';
public const SCOPE_PROFILE = 'profile';
/**
* Adjust the available read / write attributes in cognito client app.
*
* {@inheritdoc}
*/
protected $scopes = [
self::SCOPE_OPENID,
self::SCOPE_PROFILE,
self::SCOPE_EMAIL,
];
/**
* {@inheritdoc}
*/
protected $scopeSeparator = ' ';
/**
* Return provider Url.
*
* @return string
*/
public function getOIDCUrl()
{
return rtrim(config('services.oidc.host'), '/').config('services.oidc.suffix', '');
}
/**
* {@inheritdoc}
*/
public static function additionalConfigKeys(): array
{
return [
'client_id',
'host',
'logout_uri',
'redirect',
'authorize_endpoint',
'userinfo_endpoint',
'token_endpoint',
'map_user_attr',
];
}
/**
* @param string $state
*
* @return string
*/
protected function getAuthUrl($state)
{
$base_url = $this->getOIDCUrl().'/authorize';
// If authorize endpoint set, use it instead
if (config('services.oidc.authorize_endpoint')) {
$base_url = config('services.oidc.authorize_endpoint');
}
Log::debug('Buiild auth url from base : '.$base_url);
return $this->buildAuthUrlFromBase($base_url, $state);
}
/**
* @return string
*/
protected function getTokenUrl()
{
// If token endpoint set, use it instead
if (config('services.oidc.token_endpoint')) {
return config('services.oidc.token_endpoint');
}
return $this->getOIDCUrl() . '/token';
}
/**
* {@inheritdoc}
*/
public function user()
{
if ($this->user) {
return $this->user;
}
if ($this->hasInvalidState()) {
throw new \Laravel\Socialite\Two\InvalidStateException;;
}
$response = $this->getAccessTokenResponse($this->getCode());
$user = $this->getUserByToken(Arr::get($response, 'access_token'), Arr::get($response, 'id_token'));
return $this->userInstance($response, $user);
}
/**
* @param string $token
*
* @throws GuzzleException
*
* @return array|mixed
*/
protected function getUserByToken($token, $idToken = null)
{
$useIdToken = config('services.oidc.use_id_token', false);
if ($useIdToken) {
if (!$idToken) {
throw new \Exception('OIDC_USE_ID_TOKEN=true but id_token not received');
}
return $this->decodeIdToken($idToken);
}
$base_url = $this->getOIDCUrl() . '/userinfo';
// If userinfo endpoint set, use it instead
if (config('services.oidc.userinfo_endpoint')) {
$base_url = config('services.oidc.userinfo_endpoint');
}
Log::debug('Get user info from '.$base_url);
$response = $this->getHttpClient()->post($base_url, [
'headers' => [
'cache-control' => 'no-cache',
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* @return User
*/
protected function mapUserToObject(array $user)
{
Log::debug('Provider return user :'.var_export($user, true));
$socialite_user = [];
foreach (config('services.oidc.map_user_attr') as $socialite_attr => $provider_attr) {
if (! array_key_exists($provider_attr, $user)) {
Log::debug("'{$provider_attr}' not provided");
continue;
}
Log::debug("Map socialite_user['{$socialite_attr}']=".$user[$provider_attr]);
$socialite_user[$socialite_attr] = $user[$provider_attr];
}
return (new User())->setRaw($user)->map($socialite_user);
}
protected function decodeIdToken($idToken)
{
$alg = config('services.oidc.jwt_alg', 'RS256');
$key = config('services.oidc.jwt_secret_or_key');
if (!$key) {
throw new \Exception('JWT secret or public key not configured');
}
try {
$decoded = JWT::decode($idToken, new Key($key, $alg));
} catch (\Exception $e) {
throw new \Exception('Failed to decode ID token: '.$e->getMessage(), 0, $e);
}
$claims = (array) $decoded;
$clientId = config('services.oidc.client_id');
$expectedIssuer = rtrim(config('services.oidc.issuer', $this->getOIDCUrl()), '/');
$aud = $claims['aud'] ?? null;
$audiences = is_array($aud) ? $aud : ($aud !== null ? [$aud] : []);
if (($claims['iss'] ?? null) !== $expectedIssuer) {
throw new \Exception('Invalid ID token issuer');
}
if (!in_array($clientId, $audiences, true)) {
throw new \Exception('Invalid ID token audience');
}
if (count($audiences) > 1 && ($claims['azp'] ?? null) !== $clientId) {
throw new \Exception('Invalid ID token authorized party');
}
return $claims;
}
}