1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexMasterov\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use AlexMasterov\OAuth2\Client\Provider\Exception\StackExchangeException; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
8
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
9
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use UnexpectedValueException; |
13
|
|
|
|
14
|
|
|
class StackExchange extends AbstractProvider |
15
|
|
|
{ |
16
|
|
|
use BearerAuthorizationTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $urlApi = 'https://api.stackexchange.com/2.2/'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $urlAuthorize = 'https://stackexchange.com/oauth'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $urlAccessToken = 'https://stackexchange.com/oauth/access_token'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $scope; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $key; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $site = 'stackoverflow'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $state; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $redirectUri; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
1 |
|
public function getBaseAuthorizationUrl() |
62
|
|
|
{ |
63
|
1 |
|
return $this->urlAuthorize; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
3 |
|
public function getBaseAccessTokenUrl(array $params) |
70
|
|
|
{ |
71
|
3 |
|
if (empty($params['code'])) { |
72
|
1 |
|
$params['code'] = ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $this->urlAccessToken.'?'. |
76
|
3 |
|
$this->buildQueryString($params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
1 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
83
|
|
|
{ |
84
|
1 |
|
return $this->urlApi.'/me?'. |
85
|
1 |
|
$this->buildQueryString([ |
86
|
1 |
|
'access_token' => (string) $token, |
87
|
1 |
|
'key' => $this->key, |
88
|
1 |
|
'site' => $this->site |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
1 |
|
protected function getDefaultScopes() |
96
|
|
|
{ |
97
|
1 |
|
return []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
1 |
|
protected function getAuthorizationParameters(array $options) |
104
|
|
|
{ |
105
|
1 |
|
$options['response_type'] = 'code'; |
106
|
1 |
|
$options['client_id'] = $this->clientId; |
107
|
|
|
|
108
|
1 |
|
if (empty($options['state'])) { |
109
|
1 |
|
$options['state'] = $this->state; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
if (empty($options['scope'])) { |
113
|
1 |
|
$options['scope'] = $this->scope; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
if (empty($options['redirect_uri'])) { |
117
|
1 |
|
$options['redirect_uri'] = $this->redirectUri; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return $options; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
4 |
|
protected function parseResponse(ResponseInterface $response) |
127
|
|
|
{ |
128
|
4 |
|
$type = $this->getContentType($response); |
129
|
|
|
|
130
|
4 |
|
if (strpos($type, 'plain') !== false) { |
131
|
1 |
|
$content = (string) $response->getBody(); |
132
|
1 |
|
parse_str($content, $parsed); |
133
|
|
|
|
134
|
1 |
|
return $parsed; |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
return parent::parseResponse($response); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
3 |
|
protected function checkResponse(ResponseInterface $response, $data) |
144
|
|
|
{ |
145
|
3 |
|
if (isset($data['error'])) { |
146
|
1 |
|
throw StackExchangeException::errorResponse($response, $data); |
147
|
|
|
} |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritDoc |
152
|
|
|
*/ |
153
|
1 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
154
|
|
|
{ |
155
|
1 |
|
return new StackExchangeResourceOwner($response); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|