1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\OAuth2\Provider; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
10
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
11
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
12
|
|
|
use SocialConnect\OAuth2\AccessToken; |
13
|
|
|
use SocialConnect\Common\Entity\User; |
14
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
15
|
|
|
|
16
|
|
|
class Microsoft extends \SocialConnect\OAuth2\AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
3 |
|
public function getBaseUri() |
22
|
|
|
{ |
23
|
3 |
|
return 'https://apis.live.net/v5.0/'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
2 |
|
public function getAuthorizeUri() |
30
|
|
|
{ |
31
|
2 |
|
return 'https://login.live.com/oauth20_authorize.srf'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
2 |
|
public function getRequestTokenUri() |
38
|
|
|
{ |
39
|
2 |
|
return 'https://login.live.com/oauth20_token.srf'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
3 |
|
public function getName() |
46
|
|
|
{ |
47
|
3 |
|
return 'microsoft'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
3 |
|
public function parseToken($body) |
54
|
|
|
{ |
55
|
3 |
|
if (empty($body)) { |
56
|
|
|
throw new InvalidAccessToken('Provider response with empty body'); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$result = json_decode($body, true); |
60
|
3 |
|
if ($result) { |
61
|
1 |
|
return new AccessToken($result); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
throw new InvalidAccessToken('Provider response with not valid JSON'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
71
|
|
|
{ |
72
|
2 |
|
$response = $this->httpClient->request( |
73
|
2 |
|
$this->getBaseUri() . 'me', |
74
|
|
|
[ |
75
|
2 |
|
'access_token' => $accessToken->getToken() |
76
|
2 |
|
] |
77
|
2 |
|
); |
78
|
|
|
|
79
|
2 |
|
if (!$response->isSuccess()) { |
80
|
1 |
|
throw new InvalidResponse( |
81
|
1 |
|
'API response with error code', |
82
|
|
|
$response |
83
|
1 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$result = $response->json(); |
87
|
1 |
|
if (!$result) { |
88
|
1 |
|
throw new InvalidResponse( |
89
|
1 |
|
'API response is not a valid JSON object', |
90
|
1 |
|
$response->getBody() |
91
|
1 |
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$hydrator = new ObjectMap( |
95
|
|
|
[ |
96
|
|
|
'id' => 'id', |
97
|
|
|
'first_name' => 'firstname', |
98
|
|
|
'last_name' => 'lastname', |
99
|
|
|
'name' => 'fullname', |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
/** @var User $user */ |
104
|
|
|
$user = $hydrator->hydrate(new User(), $result); |
105
|
|
|
|
106
|
|
|
if ($result->emails) { |
107
|
|
|
if ($result->emails->preferred) { |
108
|
|
|
$user->email = $result->emails->preferred; |
109
|
|
|
} elseif ($result->emails->account) { |
110
|
|
|
$user->email = $result->emails->account; |
111
|
|
|
} elseif ($result->emails->personal) { |
112
|
|
|
$user->email = $result->emails->personal; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $user; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|