|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2016 Peter Deltchev |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Poniverse\Lib\Service\Poniverse; |
|
20
|
|
|
|
|
21
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
22
|
|
|
use Poniverse\Lib\Errors\InvalidAccessTokenException; |
|
23
|
|
|
use Poniverse\Lib\Service\Service; |
|
24
|
|
|
|
|
25
|
|
|
class Meta extends Service { |
|
26
|
|
|
/** |
|
27
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
28
|
|
|
* @throws InvalidAccessTokenException |
|
29
|
|
|
* @throws \Poniverse\Lib\Errors\ApiException |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function handleError(\Psr\Http\Message\ResponseInterface $response) { |
|
32
|
|
|
if (404 === $response->getStatusCode()) { |
|
33
|
|
|
throw new InvalidAccessTokenException('This access token is expired or invalid!'); |
|
34
|
|
|
} else { |
|
35
|
|
|
throw new \Poniverse\Lib\Errors\ApiException( |
|
36
|
|
|
$response->getStatusCode(), |
|
37
|
|
|
['response' => var_export($response, true)], |
|
|
|
|
|
|
38
|
|
|
'An unknown error occurred while contacting the Poniverse API.'); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Gets information about the given access token. |
|
44
|
|
|
* |
|
45
|
|
|
* @link https://tools.ietf.org/html/draft-richer-oauth-introspection-06 |
|
46
|
|
|
* |
|
47
|
|
|
* @param $accessTokenToIntrospect |
|
48
|
|
|
* @return \Poniverse\Lib\AccessToken |
|
49
|
|
|
*/ |
|
50
|
|
|
public function introspect($accessTokenToIntrospect) { |
|
51
|
|
|
$clientToken = $this->client->getOAuthProvider()->getAccessToken('client_credentials'); |
|
52
|
|
|
$this->client->setAccessToken($clientToken); |
|
53
|
|
|
|
|
54
|
|
|
$request = $this->request( |
|
55
|
|
|
'post', |
|
56
|
|
|
$this->client->getPoniverseUrl() . "/v1/meta/introspect?token={$accessTokenToIntrospect}", |
|
57
|
|
|
['headers' => array_merge( |
|
58
|
|
|
['Accept' => 'application/json'], |
|
59
|
|
|
$this->client->getAuthHeader() |
|
60
|
|
|
)] |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$response = json_decode($request->getBody(), true); |
|
64
|
|
|
|
|
65
|
|
|
$tokenInfo = new \Poniverse\Lib\AccessToken($accessTokenToIntrospect); |
|
66
|
|
|
$tokenInfo |
|
67
|
|
|
->setIsActive($response['active']) |
|
68
|
|
|
->setScopes($response['scope']) |
|
69
|
|
|
->setClientId($response['client_id']); |
|
70
|
|
|
return $tokenInfo; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Generates a new refresh token for a user and updates their email address. |
|
75
|
|
|
* |
|
76
|
|
|
* @see \League\OAuth2\Client\Token\AccessToken |
|
77
|
|
|
* |
|
78
|
|
|
* @param int $poniverseUserId |
|
79
|
|
|
* @param callable $tokenUpdater A callable that updates your stored access token |
|
80
|
|
|
* and refresh token for the user. A League AccessToken |
|
81
|
|
|
* object (see above) is passed as the callable's only argument. |
|
82
|
|
|
* @param callable $emailAddressUpdater A callable that updates the user's locally |
|
83
|
|
|
* stored email address. The user's current |
|
84
|
|
|
* email address is passed as its only argument. |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function syncAccount($poniverseUserId, $tokenUpdater, $emailAddressUpdater) { |
|
88
|
|
|
$clientToken = $this->client->getOAuthProvider()->getAccessToken('client_credentials'); |
|
89
|
|
|
$this->client->setAccessToken($clientToken); |
|
90
|
|
|
|
|
91
|
|
|
$request = $this->request( |
|
92
|
|
|
'post', |
|
93
|
|
|
$this->client->getPoniverseUrl() . "/v1/meta/sync-account?user_id={$poniverseUserId}", |
|
94
|
|
|
[ |
|
95
|
|
|
'headers' => array_merge( |
|
96
|
|
|
['Accept' => 'application/json'], |
|
97
|
|
|
$this->client->getAuthHeader() |
|
98
|
|
|
) |
|
99
|
|
|
] |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$response = json_decode($request->getBody(), true); |
|
103
|
|
|
$tokenDetails = $response['token_info']; |
|
104
|
|
|
|
|
105
|
|
|
$accessToken = new AccessToken([ |
|
106
|
|
|
'access_token' => $tokenDetails['access_token'], |
|
107
|
|
|
'expires_in' => $tokenDetails['expires_in'], |
|
108
|
|
|
'refresh_token' => $tokenDetails['refresh_token'], |
|
109
|
|
|
'resource_owner_id' => $poniverseUserId, |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
$tokenUpdater($accessToken); |
|
113
|
|
|
$emailAddressUpdater($response['email']); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: