|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2017 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\OAuth\Client\Http; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\OAuth\Client\AccessToken; |
|
22
|
|
|
use fkooman\OAuth\Client\Exception\OAuthServerException; |
|
23
|
|
|
use fkooman\OAuth\Client\OAuth2Client; |
|
24
|
|
|
use fkooman\OAuth\Client\TokenStorageInterface; |
|
25
|
|
|
|
|
26
|
|
|
class BearerClient |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var string|null */ |
|
29
|
|
|
private $userId; |
|
30
|
|
|
|
|
31
|
|
|
/** @var \fkooman\OAuth\Client\OAuth2Client */ |
|
32
|
|
|
private $oauthClient; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \fkooman\OAuth\Client\TokenStorageInterface */ |
|
35
|
|
|
private $tokenStorage; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(OAuth2Client $oauthClient, TokenStorageInterface $tokenStorage, $userId = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->oauthClient = $oauthClient; |
|
40
|
|
|
$this->tokenStorage = $tokenStorage; |
|
41
|
|
|
$this->userId = $userId; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string|null |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setUserId($userId) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->userId = $userId; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return Response|false |
|
54
|
|
|
*/ |
|
55
|
|
|
public function get($requestUri, array $requestHeaders = []) |
|
56
|
|
|
{ |
|
57
|
|
|
// make sure we have an access token |
|
58
|
|
|
$accessToken = $this->tokenStorage->getAccessToken($this->userId); |
|
59
|
|
|
if (is_null($accessToken)) { |
|
60
|
|
|
error_log('no access_token available'); |
|
61
|
|
|
|
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$refreshedToken = false; |
|
66
|
|
|
if ($accessToken->isExpired()) { |
|
67
|
|
|
error_log('access_token expired'); |
|
68
|
|
|
// access_token is expired, try to refresh it |
|
69
|
|
|
if (is_null($accessToken->getRefreshToken())) { |
|
70
|
|
|
error_log('no refresh_token available, delete access_token'); |
|
71
|
|
|
// we do not have a refresh_token, delete this access token, it |
|
72
|
|
|
// is useless now... |
|
73
|
|
|
$this->tokenStorage->deleteAccessToken($this->userId, $accessToken); |
|
74
|
|
|
|
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
error_log('attempting to refresh access_token'); |
|
79
|
|
|
// deal with possibly revoked authorization! XXX |
|
80
|
|
|
try { |
|
81
|
|
|
$accessToken = $this->oauthClient->refreshAccessToken($accessToken); |
|
82
|
|
|
} catch (OAuthServerException $e) { |
|
83
|
|
|
error_log(sprintf('unable to use refresh_token %s', $e->getMessage())); |
|
84
|
|
|
|
|
85
|
|
|
// delete the access_token, the refresh_token could not be used |
|
86
|
|
|
$this->tokenStorage->deleteAccessToken($this->userId, $accessToken); |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// maybe delete old accesstoken here? XXX |
|
92
|
|
|
error_log('access_token refreshed'); |
|
93
|
|
|
$refreshedToken = true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// add Authorization header to the request headers |
|
97
|
|
|
$requestHeaders['Authorization'] = sprintf('Bearer %s', $accessToken->getToken()); |
|
98
|
|
|
|
|
99
|
|
|
$response = $this->oauthClient->getHttpClient()->get($requestUri, $requestHeaders); |
|
100
|
|
|
if (401 === $response->getStatusCode()) { |
|
101
|
|
|
error_log('access_token appears to be invalid, delete access_token'); |
|
102
|
|
|
// this indicates an invalid access_token |
|
103
|
|
|
$this->tokenStorage->deleteAccessToken($this->userId, $accessToken); |
|
104
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
error_log('access_token was valid, call succeeded'); |
|
109
|
|
|
|
|
110
|
|
|
if ($refreshedToken) { |
|
111
|
|
|
error_log('access_token was refreshed, so store it now for future use'); |
|
112
|
|
|
// if we refreshed the token, and it was successful, i.e. not a 401, |
|
113
|
|
|
// update the stored AccessToken |
|
114
|
|
|
$this->tokenStorage->setAccessToken($this->userId, $accessToken); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $response; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function post($requestUri, array $postData = [], array $requestHeaders = []) |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.