-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProvider.php
executable file
·113 lines (98 loc) · 3.29 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
<?php
namespace SocialiteProviders\Graph;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'GRAPH';
protected $scopes = ['User.Read'];
protected $scopeSeparator = ' ';
/**
* Allows you to override the tenant id that the provider is configured
* with.
*
* @param string $tenantId
* @return \SocialiteProviders\Graph\Provider
*/
public function setTenantId($tenantId)
{
$this->config = array_merge($this->config, [
'tenant_id' => $tenantId,
]);
return $this;
}
/**
* Returns the configured tenant that we're authenticating with, or common
* if one is not configured.
*
* @return string
*/
private function getTenantId()
{
return $this->getConfig('tenant_id', 'common');
}
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase(
sprintf(
'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize',
$this->getTenantId()
),
$state
);
}
protected function getTokenUrl(): string
{
return sprintf(
'https://login.microsoftonline.com/%s/oauth2/v2.0/token',
$this->getTenantId()
);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$userEndpointVersion = $this->getConfig('user_endpoint_version', 'v1.0');
$response = $this->getHttpClient()->get("https://graph.microsoft.com/$userEndpointVersion/me/", [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode((string) $response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
// Mapping default Laravel user keys to the keys that are nested in the
// response from the provider.
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'name' => $user['displayName'],
'email' => $user['mail'] ?? $user['userPrincipalName'],
// The following values are not always required by the provider. We
// cannot guarantee they will exist in the $user array.
'businessPhones' => Arr::get($user, 'businessPhones'),
'displayName' => Arr::get($user, 'displayName'),
'givenName' => Arr::get($user, 'givenName'),
'jobTitle' => Arr::get($user, 'jobTitle'),
'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'),
]);
}
public static function additionalConfigKeys(): array
{
return [
'tenant_id',
'user_endpoint_version',
];
}
}