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