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
|
|
|
* Default host. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $host = 'https://account.orbitrondev.org'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Gets host. |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
4 |
|
public function getHost() |
30
|
|
|
{ |
31
|
4 |
|
return $this->host; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets host. Can be used for example when you testing the service-account in localhost. |
36
|
|
|
* |
37
|
|
|
* @param string $host The domain for accessing the user data |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
2 |
|
public function setHost($host) |
42
|
|
|
{ |
43
|
2 |
|
$this->host = $host; |
44
|
|
|
|
45
|
2 |
|
return $this; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the base URL for authorizing a client. |
50
|
|
|
* |
51
|
|
|
* Eg. https://oauth.service.com/authorize |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
6 |
|
public function getBaseAuthorizationUrl() |
56
|
|
|
{ |
57
|
6 |
|
return $this->host.'/oauth/authorize'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the base URL for requesting an access token. |
62
|
|
|
* |
63
|
|
|
* Eg. https://oauth.service.com/token |
64
|
|
|
* |
65
|
|
|
* @param array $params Special parameters |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
70
|
|
|
{ |
71
|
12 |
|
return $this->host.'/oauth/token'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the URL for requesting the resource owner's details. |
76
|
|
|
* |
77
|
|
|
* @param AccessToken $token The received access token from the server |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
82
|
|
|
{ |
83
|
2 |
|
return $this->host.'/oauth/resource'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the default scopes used by this provider. |
88
|
|
|
* |
89
|
|
|
* This should only be the scopes that are required to request the details |
90
|
|
|
* of the resource owner, rather than all the available scopes. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
4 |
|
protected function getDefaultScopes() |
95
|
|
|
{ |
96
|
4 |
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the string used to separate scopes. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
6 |
|
protected function getScopeSeparator() |
105
|
|
|
{ |
106
|
6 |
|
return ' '; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Checks a provider response for errors. |
111
|
|
|
* |
112
|
|
|
* @param ResponseInterface $response The response from the server |
113
|
|
|
* @param array|string $data Parsed response data |
114
|
|
|
* |
115
|
|
|
* @throws IdentityProviderException |
116
|
|
|
*/ |
117
|
10 |
|
protected function checkResponse(ResponseInterface $response, $data) |
118
|
|
|
{ |
119
|
10 |
|
if ($response->getStatusCode() >= 400) { |
120
|
4 |
|
$errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); |
121
|
6 |
|
} elseif (isset($data['error'])) { |
122
|
|
|
$errorMessage = isset($data['error']) ? $data['error'] : $response->getReasonPhrase(); |
123
|
|
|
} |
124
|
|
|
|
125
|
10 |
|
if (isset($errorMessage)) { |
126
|
|
|
throw new IdentityProviderException( |
127
|
|
|
$errorMessage, |
128
|
|
|
$response->getStatusCode(), |
129
|
|
|
$response->getBody() |
130
|
|
|
); |
131
|
|
|
} |
132
|
10 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Generates a resource owner object from a successful resource owner |
136
|
|
|
* details request. |
137
|
|
|
* |
138
|
|
|
* @param array $response Response data from server |
139
|
|
|
* @param AccessToken $token The used access token |
140
|
|
|
* |
141
|
|
|
* @return \League\OAuth2\Client\Provider\ResourceOwnerInterface |
142
|
|
|
*/ |
143
|
2 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
144
|
|
|
{ |
145
|
2 |
|
return new OrbitronDevResourceOwner($response); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|