-
Notifications
You must be signed in to change notification settings - Fork 7
/
Provider.php
executable file
·130 lines (115 loc) · 3.51 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
<?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';
/**
* {@inheritdoc}
*/
protected $scopes = ['User.Read'];
/**
* The separating character for the requested scopes.
*
* @var string
*/
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');
}
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
sprintf(
'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize',
$this->getTenantId()
),
$state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
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'),
]);
}
/**
* {@inheritdoc}
*/
public static function additionalConfigKeys()
{
return [
'tenant_id',
'user_endpoint_version',
];
}
}