-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathProvider.php
363 lines (315 loc) · 13 KB
/
Provider.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace SocialiteProviders\Microsoft;
use Exception;
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\InvalidStateException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Microsoft\MicrosoftUser as User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'MICROSOFT';
/**
* The tenant id associated with personal Microsoft accounts (services like Xbox, Teams for Life, or Outlook).
* Note: only reported in JWT ID_TOKENs and not in call's to Graph Organization endpoint.
*/
public const MS_ENTRA_CONSUMER_TENANT_ID = '9188040d-6c67-4c5b-b112-36a304b66dad';
/**
* Default field list to request from Microsoft.
*
* @see https://docs.microsoft.com/en-us/graph/permissions-reference#user-permissions
*/
protected const DEFAULT_FIELDS_USER = [
'id',
'displayName',
'businessPhones',
'givenName',
'jobTitle',
'department',
'mail',
'mobilePhone',
'officeLocation',
'preferredLanguage',
'surname',
'userPrincipalName',
'employeeId',
];
/**
* Default tenant field list to request from Microsoft.
*
* @see https://docs.microsoft.com/en-us/graph/permissions-reference#user-permissions
*/
protected const DEFAULT_FIELDS_TENANT = ['id', 'displayName', 'city', 'country', 'countryLetterCode', 'state', 'street', 'verifiedDomains'];
/**
* {@inheritdoc}
* https://learn.microsoft.com/en-us/graph/permissions-overview
* https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc#openid-connect-scopes
* https://learn.microsoft.com/en-us/entra/identity-platform/id-tokens
*/
protected $scopes = ['openid', 'profile', 'User.Read'];
protected $scopeSeparator = ' ';
protected function getAuthUrl($state): string
{
return
$this->buildAuthUrlFromBase(
sprintf(
'https://#.microsoftonline.com/%s/oauth2/v2.0/authorize',
$this->getConfig('tenant', 'common')
),
$state
);
}
protected function getTokenUrl(): string
{
return sprintf('https://#.microsoftonline.com/%s/oauth2/v2.0/token', $this->getConfig('tenant', 'common'));
}
/**
* Return the logout endpoint with an optional post_logout_redirect_uri query parameter.
*
* @param string|null $redirectUri The URI to redirect to after logout, if provided.
* If not provided, no post_logout_redirect_uri parameter will be included.
* @return string The logout endpoint URL.
*/
public function getLogoutUrl(?string $redirectUri = null)
{
$logoutUrl = sprintf('https://#.microsoftonline.com/%s/oauth2/v2.0/logout', $this->getConfig('tenant', 'common'));
return $redirectUri === null ?
$logoutUrl :
$logoutUrl.'?'.http_build_query(['post_logout_redirect_uri' => $redirectUri], '', '&', $this->encodingType);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$responseUser = $this->getHttpClient()->get(
'https://graph.microsoft.com/v1.0/me',
[
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
RequestOptions::QUERY => [
'$select' => implode(',', array_merge(self::DEFAULT_FIELDS_USER, $this->getConfig('fields', []))),
],
RequestOptions::PROXY => $this->getConfig('proxy'),
]
);
$formattedResponse = json_decode((string) $responseUser->getBody(), true);
if ($this->getConfig('include_avatar', false)) {
try {
$imageSize = $this->getConfig('include_avatar_size', '648x648');
$responseAvatar = $this->getHttpClient()->get(
"https://graph.microsoft.com/v1.0/me/photos/{$imageSize}/\$value",
[
RequestOptions::HEADERS => [
'Accept' => 'image/jpg',
'Authorization' => 'Bearer '.$token,
],
RequestOptions::PROXY => $this->getConfig('proxy'),
]
);
$formattedResponse['avatar'] = base64_encode($responseAvatar->getBody()->getContents()) ?? null;
} catch (ClientException) {
//if exception then avatar does not exist.
$formattedResponse['avatar'] = null;
}
}
$formattedResponse['tenant'] = null;
if ($this->getConfig('include_tenant_info', false) && ! $this->isConsumerTenant()) {
$responseTenant = $this->getHttpClient()->get(
'https://graph.microsoft.com/v1.0/organization',
[
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
RequestOptions::QUERY => [
'$select' => implode(',', array_merge(self::DEFAULT_FIELDS_TENANT, $this->getConfig('tenant_fields', []))),
],
RequestOptions::PROXY => $this->getConfig('proxy'),
]
);
$formattedResponse['tenant'] = json_decode((string) $responseTenant->getBody(), true)['value'][0] ?? null;
}
return $formattedResponse;
}
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
RequestOptions::PROXY => $this->getConfig('proxy'),
]);
return json_decode((string) $response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => null,
'name' => $user['displayName'],
'email' => $user['userPrincipalName'],
'avatar' => Arr::get($user, 'avatar'),
'businessPhones' => Arr::get($user, 'businessPhones'),
'displayName' => Arr::get($user, 'displayName'),
'givenName' => Arr::get($user, 'givenName'),
'jobTitle' => Arr::get($user, 'jobTitle'),
'department' => Arr::get($user, 'department'),
'mail' => Arr::get($user, 'mail'),
'mobilePhone' => Arr::get($user, 'mobilePhone'),
'officeLocation' => Arr::get($user, 'officeLocation'),
'preferredLanguage' => Arr::get($user, 'preferredLanguage'),
'surname' => Arr::get($user, 'surname'),
'userPrincipalName' => Arr::get($user, 'userPrincipalName'),
'employeeId' => Arr::get($user, 'employeeId'),
'tenant' => Arr::get($user, 'tenant'),
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator),
]);
}
public static function additionalConfigKeys(): array
{
return [
'tenant',
'include_tenant_info',
'include_avatar',
'include_avatar_size',
'fields',
'tenant_fields',
'proxy',
];
}
/**
* Check the ID_TOKEN for tenant details via JWT decode.
* https://learn.microsoft.com/en-us/entra/identity-platform/optional-claims
*
* @return bool
*/
public function isConsumerTenant(): bool
{
if ($idToken = $this->parseIdToken($this->credentialsResponseBody)) {
$claims = $this->validate($idToken);
return ($claims?->tid ?? '') === self::MS_ENTRA_CONSUMER_TENANT_ID;
}
return false;
}
/**
* When Scope includes 'openid' or 'profile' the ID_TOKEN is made available to us.
* https://learn.microsoft.com/en-us/entra/identity-platform/id-tokens
*
* @param $body
* @return array|\ArrayAccess|mixed
*/
protected function parseIdToken($body)
{
return Arr::get($body, 'id_token');
}
/**
* Get public keys to verify id_token from jwks_uri.
* Retrieves the list of public keys in the JWKS format (JSON Web Key Set)
*
* @return array
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function getJWTKeys(): array
{
$response = $this->getHttpClient()->get($this->getOpenIdConfiguration()->jwks_uri);
return json_decode((string) $response->getBody(), true);
}
/**
* Get OpenID Configuration.
*
* @return mixed
*
* @throws \Laravel\Socialite\Two\InvalidStateException
*/
private function getOpenIdConfiguration(): mixed
{
try {
// URI Discovery Mechanism for the Provider Configuration URI
//
// https://learn.microsoft.com/en-us/entra/identity-platform/v2-protocols-oidc#fetch-the-openid-configuration-document
//
$discovery = sprintf('https://#.microsoftonline.com/%s/v2.0/.well-known/openid-configuration', $this->getConfig('tenant', 'common'));
$response = $this->getHttpClient()->get($discovery);
} catch (Exception $ex) {
throw new InvalidStateException("Error on getting OpenID Configuration. {$ex}");
}
return json_decode((string) $response->getBody());
}
/**
* Extract algorithm from header, failing that defer to OIDC speced supported algorithms then service's default.
*
* @param $jwtHeader
* @return string
*/
private function getTokenSigningAlgorithm($jwtHeader): string
{
return $jwtHeader?->alg ?? (string) collect(
array_merge($this->getOpenIdConfiguration()->id_token_signing_alg_values_supported,
[$this->getConfig('default_algorithm', 'RS256')])
)->first();
}
/**
* validate id_token
* - signature validation using firebase/jwt library.
* - claims validation
* iss: MUST match iss = issuer value on metadata.
* aud: MUST include client_id for this client.
* exp: MUST time() < exp.
*
* @param string $idToken
* @return mixed|\stdClass
*
* @throws \Laravel\Socialite\Two\InvalidStateException
*/
private function validate(string $idToken)
{
// https://learn.microsoft.com/en-us/entra/identity-platform/access-token-claims-reference
try {
[$headersB64, $payloadB64, $sig] = explode('.', $idToken);
$jwtHeaders = JWT::jsonDecode(JWT::urlsafeB64Decode($headersB64));
// decode body without signature check
// $jwtPayload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadB64));
// decode body with signature check
$alg = $this->getTokenSigningAlgorithm($jwtHeaders);
$headers = new \stdClass;
$jwtPayload = JWT::decode($idToken, JWK::parseKeySet($this->getJWTKeys(), $alg), $headers);
// iss validation - a security token service (STS) URI
// Identifies the STS that constructs and returns the token, and the Microsoft Entra tenant of the authenticated user.
// https://learn.microsoft.com/en-au/entra/identity-platform/access-tokens#multitenant-applications
$issuer = str_replace('{tenantid}', $jwtPayload->tid, $this->getOpenIdConfiguration()->issuer);
if (strcmp($iss = $jwtPayload->iss, $issuer)) {
throw new InvalidStateException('iss on id_token does not match issuer value on the OpenID configuration');
}
// aud validation - an Application ID URI or GUID
// Identifies the intended audience of the token.
if (! str_contains($jwtPayload->aud, $this->clientId)) {
throw new InvalidStateException('aud on id_token does not match the client_id for this application');
}
// exp validation - int, a Unix timestamp
// Specifies the expiration time before which the JWT can be accepted for processing.
if ((int) $jwtPayload->exp < time()) {
throw new InvalidStateException('id_token is expired');
}
return $jwtPayload;
} catch (Exception $e) {
throw new InvalidStateException("Error on validating id_token. {$e}");
}
}
}