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