Skip to content

Commit c2ebc63

Browse files
Provider class modified for Azure v2 API compatibility (#734)
1 parent fc92845 commit c2ebc63

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Provider.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,29 @@ class Provider extends AbstractProvider
1818
*
1919
* @var string
2020
*/
21-
protected $graphUrl = 'https://graph.windows.net/myorganization/me';
21+
protected $graphUrl = 'https://graph.microsoft.com/v1.0/me';
2222

2323
/**
24-
* The Graph API version for the request.
24+
* The scopes being requested.
2525
*
26-
* @var string
26+
* @var array
2727
*/
28-
protected $version = '1.5';
28+
protected $scopes = ['User.Read'];
2929

3030
/**
3131
* {@inheritdoc}
3232
*/
3333
protected function getAuthUrl($state)
3434
{
35-
return $this->buildAuthUrlFromBase(
36-
'https://login.microsoftonline.com/'.($this->config['tenant'] ?? 'common').'/oauth2/authorize',
37-
$state
38-
);
35+
return $this->buildAuthUrlFromBase($this->getBaseUrl().'/oauth2/v2.0/authorize', $state);
3936
}
4037

4138
/**
4239
* {@inheritdoc}
4340
*/
4441
protected function getTokenUrl()
4542
{
46-
return 'https://login.microsoftonline.com/common/oauth2/token';
43+
return $this->getBaseUrl().'/oauth2/v2.0/token';
4744
}
4845

4946
public function getAccessToken($code)
@@ -63,9 +60,6 @@ public function getAccessToken($code)
6360
protected function getUserByToken($token)
6461
{
6562
$response = $this->getHttpClient()->get($this->graphUrl, [
66-
RequestOptions::QUERY => [
67-
'api-version' => $this->version,
68-
],
6963
RequestOptions::HEADERS => [
7064
'Accept' => 'application/json',
7165
'Authorization' => 'Bearer '.$token,
@@ -81,27 +75,45 @@ protected function getUserByToken($token)
8175
protected function mapUserToObject(array $user)
8276
{
8377
return (new User())->setRaw($user)->map([
84-
'id' => $user['objectId'], 'nickname' => null, 'name' => $user['displayName'],
85-
'email' => $user['userPrincipalName'], 'avatar' => null,
78+
'id' => $user['id'],
79+
'nickname' => null,
80+
'name' => $user['displayName'],
81+
'email' => $user['userPrincipalName'],
82+
'avatar' => null,
8683
]);
8784
}
8885

8986
/**
9087
* {@inheritdoc}
9188
*/
92-
protected function getTokenFields($code)
89+
public static function additionalConfigKeys()
9390
{
94-
return array_merge(parent::getTokenFields($code), [
95-
'grant_type' => 'authorization_code',
96-
'resource' => 'https://graph.windows.net',
91+
return ['tenant', 'logout_url', 'proxy'];
92+
}
93+
94+
/**
95+
* Get the access token response for the given code.
96+
*
97+
* @param string $code
98+
*
99+
* @return array
100+
*/
101+
public function getAccessTokenResponse($code)
102+
{
103+
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
104+
RequestOptions::HEADERS => ['Accept' => 'application/json'],
105+
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
106+
RequestOptions::PROXY => $this->getConfig('proxy'),
97107
]);
108+
109+
return json_decode($response->getBody(), true);
98110
}
99111

100112
/**
101-
* {@inheritdoc}
113+
* @return string
102114
*/
103-
public static function additionalConfigKeys()
115+
protected function getBaseUrl(): string
104116
{
105-
return ['tenant'];
117+
return 'https://login.microsoftonline.com/'.$this->getConfig('tenant', 'common');
106118
}
107119
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Please see the [Base Installation Guide](https://socialiteproviders.com/usage/),
1515
'client_id' => env('AZURE_CLIENT_ID'),
1616
'client_secret' => env('AZURE_CLIENT_SECRET'),
1717
'redirect' => env('AZURE_REDIRECT_URI')
18+
'tenant' => env('AZURE_TENANT_ID'),
19+
'logout_url' => 'https://login.microsoftonline.com/'.env('AZURE_TENANT_ID').'/oauth2/v2.0/logout?post_logout_redirect_uri=',
20+
'proxy' => env('PROXY') // optionally
1821
],
1922
```
2023

0 commit comments

Comments
 (0)