Skip to content

Commit a3ef951

Browse files
committed
Fix for Resource Owner Details URL and parsing
1 parent f24f79d commit a3ef951

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/Provider/Microsoft.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
use League\OAuth2\Client\Provider\AbstractProvider;
55
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
66
use League\OAuth2\Client\Token\AccessToken;
7+
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
78
use Psr\Http\Message\ResponseInterface;
89

910
class Microsoft extends AbstractProvider
1011
{
12+
use BearerAuthorizationTrait;
13+
1114
/**
1215
* Default scopes
1316
*
@@ -34,7 +37,7 @@ class Microsoft extends AbstractProvider
3437
*
3538
* @var string
3639
*/
37-
protected $urlResourceOwnerDetails = 'https://apis.live.net/v5.0/me';
40+
protected $urlResourceOwnerDetails = 'https://graph.microsoft.com/v1.0/me';
3841

3942
/**
4043
* Get authorization url to begin OAuth flow

src/Provider/MicrosoftResourceOwner.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ public function getId()
3838
*/
3939
public function getEmail()
4040
{
41-
return $this->response['emails']['preferred'] ?: null;
41+
return $this->response['mail'] ?: null;
42+
}
43+
44+
/**
45+
* Get user principal name
46+
*
47+
* @return string|null
48+
*/
49+
public function getPrincipalName()
50+
{
51+
return $this->response['userPrincipalName'] ?: null;
4252
}
4353

4454
/**
@@ -48,7 +58,7 @@ public function getEmail()
4858
*/
4959
public function getFirstname()
5060
{
51-
return $this->response['first_name'] ?: null;
61+
return $this->response['givenName'] ?: null;
5262
}
5363

5464
/**
@@ -58,7 +68,7 @@ public function getFirstname()
5868
*/
5969
public function getLastname()
6070
{
61-
return $this->response['last_name'] ?: null;
71+
return $this->response['surname'] ?: null;
6272
}
6373

6474
/**
@@ -68,17 +78,19 @@ public function getLastname()
6878
*/
6979
public function getName()
7080
{
71-
return $this->response['name'] ?: null;
81+
return $this->response['displayName'] ?: null;
7282
}
7383

7484
/**
85+
* @deprecated To be removed in 3.0
86+
*
7587
* Get user urls
7688
*
7789
* @return string|null
7890
*/
7991
public function getUrls()
8092
{
81-
return isset($this->response['link']) ? $this->response['link'].'/cid-'.$this->getId() : null;
93+
return null;
8294
}
8395

8496
/**
@@ -88,6 +100,6 @@ public function getUrls()
88100
*/
89101
public function toArray()
90102
{
91-
return $this->response;
103+
return $this->response + ['link' => null];
92104
}
93105
}

tests/src/Provider/MicrosoftTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ public function testUserData()
118118
$lastname = uniqid();
119119
$name = uniqid();
120120
$userId = rand(1000,9999);
121-
$urls = uniqid();
121+
$userPrincipalName = uniqid();
122122

123123
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
124124
$postResponse->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}');
125125
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
126126

127127
$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
128-
$userResponse->shouldReceive('getBody')->andReturn('{"id": '.$userId.', "name": "'.$name.'", "first_name": "'.$firstname.'", "last_name": "'.$lastname.'", "emails": {"preferred": "'.$email.'"}, "link": "'.$urls.'"}');
128+
$userResponse->shouldReceive('getBody')->andReturn('{"id": '.$userId.', "displayName": "'.$name.'", "givenName": "'.$firstname.'", "surname": "'.$lastname.'", "mail": "'.$email.'", "userPrincipalName": "'.$userPrincipalName.'"}');
129129
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
130130

131131
$client = m::mock('GuzzleHttp\ClientInterface');
@@ -138,17 +138,19 @@ public function testUserData()
138138
$user = $this->provider->getResourceOwner($token);
139139

140140
$this->assertEquals($email, $user->getEmail());
141-
$this->assertEquals($email, $user->toArray()['emails']['preferred']);
141+
$this->assertEquals($email, $user->toArray()['mail']);
142142
$this->assertEquals($firstname, $user->getFirstname());
143-
$this->assertEquals($firstname, $user->toArray()['first_name']);
143+
$this->assertEquals($firstname, $user->toArray()['givenName']);
144144
$this->assertEquals($lastname, $user->getLastname());
145-
$this->assertEquals($lastname, $user->toArray()['last_name']);
145+
$this->assertEquals($lastname, $user->toArray()['surname']);
146146
$this->assertEquals($name, $user->getName());
147-
$this->assertEquals($name, $user->toArray()['name']);
147+
$this->assertEquals($name, $user->toArray()['displayName']);
148148
$this->assertEquals($userId, $user->getId());
149149
$this->assertEquals($userId, $user->toArray()['id']);
150-
$this->assertEquals($urls.'/cid-'.$userId, $user->getUrls());
151-
$this->assertEquals($urls.'/cid-'.$userId, $user->toArray()['link'].'/cid-'.$user->toArray()['id']);
150+
$this->assertNull($user->getUrls());
151+
$this->assertNull($user->toArray()['link']);
152+
$this->assertEquals($userPrincipalName, $user->getPrincipalName());
153+
$this->assertEquals($userPrincipalName, $user->toArray()['userPrincipalName']);
152154
}
153155

154156
/**

0 commit comments

Comments
 (0)