1 | <?php |
||
9 | class DropboxAuthHelper |
||
10 | { |
||
11 | /** |
||
12 | * Access token type online param will returns a short lived access token that |
||
13 | * require the user to reauthenticate once expired. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | CONST TOKEN_ACCESS_TYPE_ONLINE = 'online'; |
||
18 | |||
19 | /** |
||
20 | * Access token type offline returns short lived access token and a refresh token that can be used |
||
21 | * to generate a new access token once the original is expired |
||
22 | * |
||
23 | */ |
||
24 | CONST TOKEN_ACCESS_TYPE_OFFLINE = 'offline'; |
||
25 | |||
26 | /** |
||
27 | * Grant type used for fetching new access token |
||
28 | */ |
||
29 | CONST GRANT_TYPE_AUTHORIZATION_CODE = 'authorization_code'; |
||
30 | |||
31 | /** |
||
32 | * Grant type for fetching a new token based on an existing refresh token |
||
33 | */ |
||
34 | CONST GRANT_TYPE_REFRESH_TOKEN = 'refresh_token'; |
||
35 | |||
36 | /** |
||
37 | * The length of CSRF string |
||
38 | * |
||
39 | * @const int |
||
40 | */ |
||
41 | const CSRF_LENGTH = 32; |
||
42 | |||
43 | /** |
||
44 | * OAuth2 Client |
||
45 | * |
||
46 | * @var \Kunnu\Dropbox\Authentication\OAuth2Client |
||
47 | */ |
||
48 | protected $oAuth2Client; |
||
49 | |||
50 | /** |
||
51 | * Random String Generator |
||
52 | * |
||
53 | * @var \Kunnu\Dropbox\Security\RandomStringGeneratorInterface |
||
54 | */ |
||
55 | protected $randomStringGenerator; |
||
56 | |||
57 | /** |
||
58 | * Persistent Data Store |
||
59 | * |
||
60 | * @var \Kunnu\Dropbox\Store\PersistentDataStoreInterface |
||
61 | */ |
||
62 | protected $persistentDataStore; |
||
63 | |||
64 | /** |
||
65 | * Additional User Provided State |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $urlState = null; |
||
70 | |||
71 | /** |
||
72 | * The selected access token type to be used when authenticating. |
||
73 | * |
||
74 | * If omitted (null), Dropbox response will default to returning a long-lived access_token |
||
75 | * if they are allowed in the app console. |
||
76 | * If long-lived access tokens are disabled in the app console, this parameter defaults to online. |
||
77 | * |
||
78 | * @var string $accessTokeenType |
||
79 | */ |
||
80 | protected $accessTokenType = null; |
||
81 | |||
82 | /** |
||
83 | * Create a new DropboxAuthHelper instance |
||
84 | * |
||
85 | * @param \Kunnu\Dropbox\Authentication\OAuth2Client $oAuth2Client |
||
86 | * @param \Kunnu\Dropbox\Security\RandomStringGeneratorInterface $randomStringGenerator |
||
87 | * @param \Kunnu\Dropbox\Store\PersistentDataStoreInterface $persistentDataStore |
||
88 | */ |
||
89 | public function __construct( |
||
98 | |||
99 | /** |
||
100 | * Get OAuth2Client |
||
101 | * |
||
102 | * @return \Kunnu\Dropbox\Authentication\OAuth2Client |
||
103 | */ |
||
104 | public function getOAuth2Client() |
||
108 | |||
109 | /** |
||
110 | * Get the Random String Generator |
||
111 | * |
||
112 | * @return \Kunnu\Dropbox\Security\RandomStringGeneratorInterface |
||
113 | */ |
||
114 | public function getRandomStringGenerator() |
||
118 | |||
119 | /** |
||
120 | * Get the Persistent Data Store |
||
121 | * |
||
122 | * @return \Kunnu\Dropbox\Store\PersistentDataStoreInterface |
||
123 | */ |
||
124 | public function getPersistentDataStore() |
||
128 | |||
129 | /** |
||
130 | * Get CSRF Token |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | protected function getCsrfToken() |
||
140 | |||
141 | /** |
||
142 | * Get Authorization URL |
||
143 | * |
||
144 | * @param string $redirectUri Callback URL to redirect to after authorization |
||
145 | * @param array $params Additional Params |
||
146 | * @param string $urlState Additional User Provided State Data |
||
147 | * |
||
148 | * @link https://www.dropbox.com/developers/documentation/http/documentation#oauth2-authorize |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getAuthUrl($redirectUri = null, array $params = [], $urlState = null) |
||
185 | |||
186 | /** |
||
187 | * Decode State to get the CSRF Token and the URL State |
||
188 | * |
||
189 | * @param string $state State |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function decodeState($state) |
||
207 | |||
208 | /** |
||
209 | * Validate CSRF Token |
||
210 | * @param string $csrfToken CSRF Token |
||
211 | * |
||
212 | * @throws DropboxClientException |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | protected function validateCSRFToken($csrfToken) |
||
233 | |||
234 | /** |
||
235 | * Get Access Token |
||
236 | * |
||
237 | * @param string $code Authorization Code or refresh token (when grantType is DropboxAuthHelper::GRANT_TYPE_REFRESH_TOKEN |
||
238 | * @param string $state CSRF & URL State |
||
239 | * @param string $redirectUri Redirect URI used while getAuthUrl |
||
240 | * @param string $grantType Use either DropboxAuthHelper::GRANT_TYPE_AUTHORIZATION_CODE for new token |
||
241 | * or DropboxAuthHelper::GRANT_TYPE_REFRESH_TOKEN for refreshing offline token. |
||
242 | * |
||
243 | * @return \Kunnu\Dropbox\Models\AccessToken |
||
244 | */ |
||
245 | public function getAccessToken($code, $state = null, $redirectUri = null, $grantType = DropboxAuthHelper::GRANT_TYPE_AUTHORIZATION_CODE) |
||
270 | |||
271 | /** |
||
272 | * Revoke Access Token |
||
273 | * |
||
274 | * @return void |
||
275 | */ |
||
276 | public function revokeAccessToken() |
||
280 | |||
281 | /** |
||
282 | * Get URL State |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getUrlState() |
||
290 | |||
291 | /** |
||
292 | * Set the access token type to be either |
||
293 | * DropboxAuthHelper::TOKEN_ACCESS_TYPE_ONLINE (default) or DropboxAuthHelper::TOKEN_ACCESS_TYPE_OFFLINE |
||
294 | * |
||
295 | * @param $tokenType string |
||
296 | */ |
||
297 | public function setAccessTokenType($tokenType = self::TOKEN_ACCESS_TYPE_ONLINE) { |
||
303 | } |
||
304 |