|
1
|
|
|
<?php namespace Stevenmaguire\OAuth2\Client\Provider; |
|
2
|
|
|
|
|
3
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
4
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
5
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
6
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Microsoft extends AbstractProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* No access token type |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
const ACCESS_TOKEN_TYPE_NONE = ''; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Access token type 'Bearer' |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
const ACCESS_TOKEN_TYPE_BEARER = 'Bearer'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Default scopes |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
public $defaultScopes = ['wl.basic', 'wl.emails']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Base url for authorization. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $urlAuthorize = 'https://login.live.com/oauth20_authorize.srf'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Base url for access token. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $urlAccessToken = 'https://login.live.com/oauth20_token.srf'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Base url for resource owner. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $urlResourceOwnerDetails = 'https://apis.live.net/v5.0/me'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The access token type to use. Defaults to none. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $accessTokenType = self::ACCESS_TOKEN_TYPE_NONE; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get authorization url to begin OAuth flow |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
12 |
|
public function getBaseAuthorizationUrl() |
|
66
|
|
|
{ |
|
67
|
12 |
|
return $this->urlAuthorize; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get access token url to retrieve token |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
15 |
|
public function getBaseAccessTokenUrl(array $params) |
|
76
|
|
|
{ |
|
77
|
15 |
|
return $this->urlAccessToken; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets the access token type used for authorization. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string The access token type to use. |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function setAccessTokenType($accessTokenType) |
|
86
|
|
|
{ |
|
87
|
3 |
|
$this->accessTokenType = $accessTokenType; |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get default scopes |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
9 |
|
protected function getDefaultScopes() |
|
96
|
|
|
{ |
|
97
|
9 |
|
return $this->defaultScopes; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Check a provider response for errors. |
|
102
|
|
|
* |
|
103
|
|
|
* @throws IdentityProviderException |
|
104
|
|
|
* @param ResponseInterface $response |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
|
108
|
|
|
{ |
|
109
|
9 |
|
if (isset($data['error'])) { |
|
110
|
3 |
|
throw new IdentityProviderException( |
|
111
|
3 |
|
(isset($data['error']['message']) ? $data['error']['message'] : $response->getReasonPhrase()), |
|
112
|
3 |
|
$response->getStatusCode(), |
|
113
|
2 |
|
$response |
|
|
|
|
|
|
114
|
1 |
|
); |
|
115
|
|
|
} |
|
116
|
6 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Generate a user object from a successful user details request. |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $response |
|
122
|
|
|
* @param AccessToken $token |
|
123
|
|
|
* @return MicrosoftResourceOwner |
|
124
|
|
|
*/ |
|
125
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
126
|
|
|
{ |
|
127
|
3 |
|
return new MicrosoftResourceOwner($response); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get provider url to fetch user details |
|
132
|
|
|
* |
|
133
|
|
|
* @param AccessToken $token |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
138
|
|
|
{ |
|
139
|
6 |
|
$uri = new Uri($this->urlResourceOwnerDetails); |
|
140
|
|
|
|
|
141
|
6 |
|
return (string) Uri::withQueryValue($uri, 'access_token', (string) $token); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns the authorization headers used by this provider. |
|
146
|
|
|
* |
|
147
|
|
|
* @param mixed|null $token Either a string or an access token instance |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
6 |
|
protected function getAuthorizationHeaders($token = null) |
|
151
|
|
|
{ |
|
152
|
6 |
|
switch ($this->accessTokenType) { |
|
153
|
6 |
|
case self::ACCESS_TOKEN_TYPE_BEARER: |
|
154
|
3 |
|
return ['Authorization' => 'Bearer ' . $token]; |
|
155
|
6 |
|
case self::ACCESS_TOKEN_TYPE_NONE: |
|
156
|
2 |
|
default: |
|
157
|
6 |
|
return []; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: