-
Notifications
You must be signed in to change notification settings - Fork 15
/
Provider.php
210 lines (186 loc) · 7.42 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
<?php
namespace SocialiteProviders\Microsoft;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Microsoft\MicrosoftUser as User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'MICROSOFT';
/**
* 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'];
protected $scopes = ['User.Read'];
protected $scopeSeparator = ' ';
protected function getAuthUrl($state): string
{
return
$this->buildAuthUrlFromBase(
sprintf(
'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize',
$this->getConfig('tenant', 'common')
),
$state
);
}
protected function getTokenUrl(): string
{
return sprintf('https://login.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://login.microsoftonline.com/%s/oauth2/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;
}
}
if ($this->getConfig('include_tenant_info', false)) {
try {
$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;
} catch (RequestException) {
//if exception then tenant does not exist.
$formattedResponse['tenant'] = 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',
];
}
}