1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\NextEngine\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 Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class NextEngineProvider extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string Key used in a token response to identify the resource owner. |
15
|
|
|
*/ |
16
|
|
|
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'uid'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
1 |
|
* Returns the base URL for authorizing a client. |
20
|
|
|
* |
21
|
1 |
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getBaseAuthorizationUrl() |
24
|
|
|
{ |
25
|
|
|
return 'https://base.next-engine.org/users/sign_in/'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the base URL for requesting an access token. |
30
|
1 |
|
* |
31
|
|
|
* Eg. https://oauth.service.com/token |
32
|
1 |
|
* |
33
|
1 |
|
* @param array $params |
34
|
1 |
|
* @return string |
35
|
|
|
*/ |
36
|
1 |
|
public function getBaseAccessTokenUrl(array $params) |
37
|
1 |
|
{ |
38
|
|
|
return 'https://api.next-engine.org/api_neauth/'; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the URL for requesting the resource owner's details. |
43
|
|
|
* |
44
|
|
|
* @param AccessToken $token |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
48
|
1 |
|
{ |
49
|
|
|
return null; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the default scopes used by this provider. |
54
|
|
|
* |
55
|
|
|
* This should only be the scopes that are required to request the details |
56
|
|
|
* of the resource owner, rather than all the available scopes. |
57
|
|
|
* |
58
|
|
|
* @return string[] |
59
|
1 |
|
*/ |
60
|
|
|
protected function getDefaultScopes() |
61
|
1 |
|
{ |
62
|
1 |
|
return []; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
/** |
66
|
|
|
* Checks a provider response for errors. |
67
|
|
|
* |
68
|
|
|
* @throws IdentityProviderException |
69
|
|
|
* @param ResponseInterface $response |
70
|
|
|
* @param array|string $data Parsed response data |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function checkResponse(ResponseInterface $response, $data) |
74
|
|
|
{ |
75
|
|
|
if ('error' === $data['result']) { |
76
|
2 |
|
throw new IdentityProviderException( |
77
|
|
|
$data['message'] ?: $response->getReasonPhrase(), |
78
|
2 |
|
$response->getStatusCode(), |
79
|
|
|
$response->getBody() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns authorization parameters based on provided options. |
86
|
|
|
* |
87
|
1 |
|
* @param array $options |
88
|
|
|
* @return array Authorization parameters |
89
|
1 |
|
*/ |
90
|
|
|
protected function getAuthorizationParameters(array $options) |
91
|
|
|
{ |
92
|
|
|
if (!isset($options['redirect_uri'])) { |
93
|
|
|
$options['redirect_uri'] = $this->redirectUri; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$options['client_id'] = $this->clientId; |
97
|
|
|
$options['client_secret'] = $this->clientSecret; |
98
|
|
|
|
99
|
|
|
return $options; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Requests an access token using a specified grant and option set. |
104
|
|
|
* |
105
|
|
|
* @param mixed $grant |
106
|
|
|
* @param array $options |
107
|
|
|
* @return AccessToken |
108
|
|
|
*/ |
109
|
|
|
public function getAccessToken($grant, array $options = []) |
110
|
|
|
{ |
111
|
|
|
$grant = $this->verifyGrant($grant); |
112
|
|
|
|
113
|
2 |
|
$request = $this->getAccessTokenRequest($options); |
114
|
|
|
$response = $this->getParsedResponse($request); |
115
|
2 |
|
$prepared = $this->prepareAccessTokenResponse($response); |
|
|
|
|
116
|
1 |
|
$token = $this->createAccessToken($prepared, $grant); |
117
|
1 |
|
|
118
|
1 |
|
return $token; |
119
|
1 |
|
} |
120
|
1 |
|
|
121
|
|
|
/** |
122
|
1 |
|
* Generates a resource owner object from a successful resource owner |
123
|
|
|
* details request. |
124
|
|
|
* |
125
|
|
|
* @param array $response |
126
|
|
|
* @param AccessToken $token |
127
|
|
|
* @return ResourceOwnerInterface |
128
|
|
|
*/ |
129
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
130
|
|
|
{ |
131
|
|
|
return new NextEngineResourceOwner($token->getValues()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Requests and returns the resource owner of given access token. |
136
|
|
|
* |
137
|
|
|
* @param AccessToken $token |
138
|
|
|
* @return ResourceOwnerInterface |
139
|
|
|
*/ |
140
|
|
|
public function getResourceOwner(AccessToken $token) |
141
|
|
|
{ |
142
|
|
|
return $this->createResourceOwner([], $token); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.