1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OrbitronDev\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class OrbitronDevProvider extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
use BearerAuthorizationTrait; |
14
|
|
|
|
15
|
|
|
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns the base URL for authorizing a client. |
19
|
|
|
* |
20
|
|
|
* Eg. https://oauth.service.com/authorize |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
6 |
|
public function getBaseAuthorizationUrl() |
25
|
|
|
{ |
26
|
6 |
|
return 'https://account.orbitrondev.org/oauth/authorize'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns the base URL for requesting an access token. |
31
|
|
|
* |
32
|
|
|
* Eg. https://oauth.service.com/token |
33
|
|
|
* |
34
|
|
|
* @param array $params Special parameters |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
39
|
|
|
{ |
40
|
12 |
|
return 'https://account.orbitrondev.org/oauth/token'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the URL for requesting the resource owner's details. |
45
|
|
|
* |
46
|
|
|
* @param AccessToken $token The received access token from the server |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
51
|
|
|
{ |
52
|
|
|
return 'https://account.orbitrondev.org/oauth/resource'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the default scopes used by this provider. |
57
|
|
|
* |
58
|
|
|
* This should only be the scopes that are required to request the details |
59
|
|
|
* of the resource owner, rather than all the available scopes. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
4 |
|
protected function getDefaultScopes() |
64
|
|
|
{ |
65
|
4 |
|
return []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the string used to separate scopes. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
6 |
|
protected function getScopeSeparator() |
74
|
|
|
{ |
75
|
6 |
|
return ' '; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks a provider response for errors. |
80
|
|
|
* |
81
|
|
|
* @param ResponseInterface $response The response from the server |
82
|
|
|
* @param array|string $data Parsed response data |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
* @throws IdentityProviderException |
86
|
|
|
*/ |
87
|
10 |
|
protected function checkResponse(ResponseInterface $response, $data) |
88
|
|
|
{ |
89
|
10 |
|
if ($response->getStatusCode() >= 400) { |
90
|
4 |
|
$errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); |
91
|
2 |
|
} elseif (isset($data['error'])) { |
92
|
|
|
$errorMessage = isset($data['error']) ? $data['error'] : $response->getReasonPhrase(); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
if (isset($errorMessage)) { |
96
|
|
|
throw new IdentityProviderException( |
97
|
|
|
$errorMessage, |
98
|
|
|
$response->getStatusCode(), |
99
|
|
|
$response->getBody() |
100
|
|
|
); |
101
|
|
|
} |
102
|
4 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Generates a resource owner object from a successful resource owner |
106
|
|
|
* details request. |
107
|
|
|
* |
108
|
|
|
* @param array $response Response data from server |
109
|
|
|
* @param AccessToken $token The used access token |
110
|
|
|
* |
111
|
|
|
* @return \League\OAuth2\Client\Provider\ResourceOwnerInterface |
112
|
|
|
*/ |
113
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
114
|
|
|
{ |
115
|
|
|
return new OrbitronDevResourceOwner($response); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|