|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dropbox\Authentication; |
|
3
|
|
|
|
|
4
|
|
|
use Dropbox\DropboxApp; |
|
5
|
|
|
use Dropbox\DropboxClient; |
|
6
|
|
|
use Dropbox\DropboxRequest; |
|
7
|
|
|
use Dropbox\Security\RandomStringGeneratorInterface; |
|
8
|
|
|
|
|
9
|
|
|
class OAuth2Client |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The Base URL |
|
14
|
|
|
* |
|
15
|
|
|
* @const string |
|
16
|
|
|
*/ |
|
17
|
|
|
const BASE_URL = "https://dropbox.com"; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Auth Token URL |
|
21
|
|
|
* |
|
22
|
|
|
* @const string |
|
23
|
|
|
*/ |
|
24
|
|
|
const AUTH_TOKEN_URL = "https://api.dropboxapi.com/oauth2/token"; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The Dropbox App |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Dropbox\DropboxApp |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $app; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The Dropbox Client |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Dropbox\DropboxClient |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $client; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Random String Generator |
|
42
|
|
|
* |
|
43
|
|
|
* @var \Dropbox\Security\RandomStringGeneratorInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $randStrGenerator; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Create a new DropboxApp instance |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Dropbox\DropboxApp $app |
|
51
|
|
|
* @param \Dropbox\DropboxClient $client |
|
52
|
|
|
* @param \Dropbox\Security\RandomStringGeneratorInterface $randStrGenerator |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(DropboxApp $app, DropboxClient $client, RandomStringGeneratorInterface $randStrGenerator = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->app = $app; |
|
57
|
|
|
$this->client = $client; |
|
58
|
|
|
$this->randStrGenerator = $randStrGenerator; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Build URL |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $endpoint |
|
65
|
|
|
* @param array $params Query Params |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function buildUrl($endpoint = '', array $params = []) |
|
70
|
|
|
{ |
|
71
|
|
|
$queryParams = http_build_query($params); |
|
72
|
|
|
return static::BASE_URL . $endpoint . '?' . $queryParams; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the Dropbox App |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Dropbox\DropboxApp |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getApp() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->app; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the Dropbox Client |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Dropbox\DropboxClient |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getClient() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->client; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the OAuth2 Authorization URL |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $redirectUri Callback URL to redirect user after authorization. |
|
99
|
|
|
* If null is passed, redirect_uri will be omitted |
|
100
|
|
|
* from the url and the code will be presented directly |
|
101
|
|
|
* to the user. |
|
102
|
|
|
* @param string $state CSRF Token |
|
103
|
|
|
* @param array $params Additional Params |
|
104
|
|
|
* |
|
105
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#oauth2-authorize |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getAuthorizationUrl($redirectUri = null, $state = null, array $params = []) |
|
110
|
|
|
{ |
|
111
|
|
|
//Request Parameters |
|
112
|
|
|
$params = array_merge([ |
|
113
|
|
|
'client_id' => $this->getApp()->getClientId(), |
|
114
|
|
|
'response_type' => 'code', |
|
115
|
|
|
'state' => $state, |
|
116
|
|
|
], $params); |
|
117
|
|
|
|
|
118
|
|
|
if (!is_null($redirectUri)) { |
|
119
|
|
|
$params['redirect_uri'] = $redirectUri; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this->buildUrl('/oauth2/authorize', $params); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get Access Token |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $code Authorization Code |
|
129
|
|
|
* @param string $redirectUri Redirect URI used while getAuthorizationUrl |
|
130
|
|
|
* @param string $grant_type Grant Type ['authorization_code'] |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getAccessToken($code, $redirectUri = null, $grant_type = 'authorization_code') |
|
135
|
|
|
{ |
|
136
|
|
|
//Request Params |
|
137
|
|
|
$params = [ |
|
138
|
|
|
'code' => $code, |
|
139
|
|
|
'grant_type' => $grant_type, |
|
140
|
|
|
'client_id' => $this->getApp()->getClientId(), |
|
141
|
|
|
'client_secret' => $this->getApp()->getClientSecret(), |
|
142
|
|
|
'redirect_uri' => $redirectUri |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
$params = http_build_query($params); |
|
146
|
|
|
|
|
147
|
|
|
$apiUrl = static::AUTH_TOKEN_URL; |
|
148
|
|
|
$uri = $apiUrl . "?" . $params; |
|
149
|
|
|
|
|
150
|
|
|
//Send Request through the DropboxClient |
|
151
|
|
|
//Fetch the Response (DropboxRawResponse) |
|
152
|
|
|
$response = $this->getClient() |
|
153
|
|
|
->getHttpClient() |
|
154
|
|
|
->send($uri, "POST", null); |
|
155
|
|
|
|
|
156
|
|
|
//Fetch Response Body |
|
157
|
|
|
$body = $response->getBody(); |
|
158
|
|
|
|
|
159
|
|
|
//Decode the Response body to associative array |
|
160
|
|
|
//and return |
|
161
|
|
|
return json_decode((string) $body, true); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Disables the access token |
|
166
|
|
|
* |
|
167
|
|
|
* @return void |
|
168
|
|
|
*/ |
|
169
|
|
|
public function revokeAccessToken() |
|
170
|
|
|
{ |
|
171
|
|
|
//Access Token |
|
172
|
|
|
$accessToken = $this->getApp()->getAccessToken(); |
|
173
|
|
|
|
|
174
|
|
|
//Request |
|
175
|
|
|
$request = new DropboxRequest("POST", "/auth/token/revoke", $accessToken); |
|
176
|
|
|
// Do not validate the response |
|
177
|
|
|
// since the /token/revoke endpoint |
|
178
|
|
|
// doesn't return anything in the response. |
|
179
|
|
|
// See: https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
|
180
|
|
|
$request->setParams(['validateResponse' => false]); |
|
181
|
|
|
|
|
182
|
|
|
//Revoke Access Token |
|
183
|
|
|
$this->getClient()->sendRequest($request); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|