|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlexMasterov\OAuth2\Client\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use AlexMasterov\OAuth2\Client\Provider\HeadHunterException; |
|
6
|
|
|
use League\OAuth2\Client\{ |
|
7
|
|
|
Provider\AbstractProvider, |
|
8
|
|
|
Token\AccessToken, |
|
9
|
|
|
Tool\BearerAuthorizationTrait |
|
10
|
|
|
}; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
class HeadHunter extends AbstractProvider |
|
14
|
|
|
{ |
|
15
|
|
|
use BearerAuthorizationTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $urlApi = 'https://api.hh.ru'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $urlAuthorize = 'https://hh.ru/oauth/authorize'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $urlAccessToken = 'https://hh.ru/oauth/token'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $state; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $redirectUri; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritDoc |
|
44
|
|
|
*/ |
|
45
|
1 |
|
protected function getDefaultScopes() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getBaseAuthorizationUrl() |
|
54
|
|
|
{ |
|
55
|
1 |
|
return $this->urlAuthorize; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getBaseAccessTokenUrl(array $params) |
|
62
|
|
|
{ |
|
63
|
1 |
|
if (empty($params['code'])) { |
|
64
|
1 |
|
$params['code'] = ''; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $this->urlAccessToken . '?' . |
|
68
|
1 |
|
$this->buildQueryString($params); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->urlApi . '/me?' . |
|
77
|
1 |
|
$this->buildQueryString([ |
|
78
|
1 |
|
'access_token' => (string) $token, |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritDoc |
|
84
|
|
|
*/ |
|
85
|
1 |
|
protected function getAuthorizationParameters(array $options) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$options['response_type'] = 'code'; |
|
88
|
1 |
|
$options['client_id'] = $this->clientId; |
|
89
|
|
|
|
|
90
|
1 |
|
if (empty($options['state'])) { |
|
91
|
1 |
|
$options['state'] = $this->state; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
if (empty($options['redirect_uri'])) { |
|
95
|
1 |
|
$options['redirect_uri'] = $this->redirectUri; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $options; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritDoc |
|
103
|
|
|
*/ |
|
104
|
1 |
|
protected function checkResponse(ResponseInterface $response, $data) |
|
105
|
|
|
{ |
|
106
|
1 |
|
if (isset($data['error'])) { |
|
107
|
1 |
|
throw HeadHunterException::errorResponse($response, $data); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @inheritDoc |
|
113
|
|
|
*/ |
|
114
|
1 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
115
|
|
|
{ |
|
116
|
1 |
|
return new HeadHunterResourceOwner($response); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|