Exception
8 years ago
WebAuthException
8 years ago
certs
8 years ago
AppInfo.php
8 years ago
AppInfoLoadException.php
8 years ago
ArrayEntryStore.php
8 years ago
AuthBase.php
8 years ago
AuthInfo.php
8 years ago
AuthInfoLoadException.php
8 years ago
Checker.php
8 years ago
Client.php
8 years ago
Curl.php
8 years ago
CurlStreamRelay.php
8 years ago
DeserializeException.php
8 years ago
DropboxMetadataHeaderCatcher.php
8 years ago
Exception.php
8 years ago
Host.php
8 years ago
HttpResponse.php
8 years ago
OAuth1AccessToken.php
8 years ago
OAuth1Upgrader.php
8 years ago
Path.php
8 years ago
RequestUtil.php
8 years ago
RootCertificates.php
8 years ago
SSLTester.php
8 years ago
Security.php
8 years ago
StreamReadException.php
8 years ago
Util.php
8 years ago
ValueStore.php
8 years ago
WebAuth.php
3 years ago
WebAuthBase.php
3 years ago
WebAuthNoRedirect.php
8 years ago
WriteMode.php
8 years ago
autoload.php
8 years ago
strict.php
8 years ago
WebAuthBase.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Dropbox; |
| 4 | |
| 5 | /** |
| 6 | * The base class for the two auth options. |
| 7 | */ |
| 8 | class WebAuthBase extends AuthBase |
| 9 | { |
| 10 | protected function _getRefreshedAccessToken($key, $secrect, $refreshToken) // phpcs:ignore |
| 11 | { |
| 12 | $array = array(); |
| 13 | $ch = curl_init(); |
| 14 | curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/oauth2/token'); |
| 15 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 16 | curl_setopt($ch, CURLOPT_POST, 1); |
| 17 | curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&refresh_token=" . $refreshToken); |
| 18 | curl_setopt($ch, CURLOPT_USERPWD, $key . ':' . $secrect); |
| 19 | $headers = array(); |
| 20 | $headers[] = 'Content-Type: application/x-www-form-urlencoded'; |
| 21 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 22 | $response = json_decode(curl_exec($ch), true); |
| 23 | |
| 24 | if (curl_errno($ch)) { |
| 25 | $array = ['status' => 400, 'access_token' => null]; |
| 26 | } elseif (isset($response['access_token'])) { |
| 27 | $array = ['status' => 200, 'access_token' => $response['access_token']]; |
| 28 | } |
| 29 | curl_close($ch); |
| 30 | |
| 31 | return $array; |
| 32 | } |
| 33 | |
| 34 | /* Dropbox api 2*/ |
| 35 | protected function _getAuthorizeUrl($redirectUri, $state) // phpcs:ignore |
| 36 | { |
| 37 | return RequestUtil::buildUrlForGetOrPut( |
| 38 | $this->userLocale, |
| 39 | $this->appInfo->getHost()->getWeb(), |
| 40 | "oauth2/authorize", |
| 41 | array( |
| 42 | "token_access_type" => "offline", |
| 43 | "client_id" => $this->appInfo->getKey(), |
| 44 | "response_type" => "code", |
| 45 | "redirect_uri" => $redirectUri, |
| 46 | "state" => $state, |
| 47 | ) |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | protected function _finish($code, $originalRedirectUri) // phpcs:ignore |
| 52 | { |
| 53 | // This endpoint requires "Basic" auth. |
| 54 | $clientCredentials = $this->appInfo->getKey() . ":" . $this->appInfo->getSecret(); |
| 55 | $authHeaderValue = "Basic " . base64_encode($clientCredentials); |
| 56 | |
| 57 | $response = RequestUtil::doPostWithSpecificAuth( |
| 58 | $this->clientIdentifier, |
| 59 | $authHeaderValue, |
| 60 | $this->userLocale, |
| 61 | $this->appInfo->getHost()->getApi(), |
| 62 | "oauth2/token", |
| 63 | array( |
| 64 | "grant_type" => "authorization_code", |
| 65 | "code" => $code, |
| 66 | "redirect_uri" => $originalRedirectUri, |
| 67 | ) |
| 68 | ); |
| 69 | |
| 70 | if ($response->statusCode !== 200) { |
| 71 | throw RequestUtil::unexpectedStatus($response); |
| 72 | } |
| 73 | |
| 74 | $parts = RequestUtil::parseResponseJson($response->body); |
| 75 | |
| 76 | if (!array_key_exists('token_type', $parts) || !is_string($parts['token_type'])) { |
| 77 | throw new Exception_BadResponse("Missing \"token_type\" field."); |
| 78 | } |
| 79 | $tokenType = $parts['token_type']; |
| 80 | if (!array_key_exists('access_token', $parts) || !is_string($parts['access_token'])) { |
| 81 | throw new Exception_BadResponse("Missing \"access_token\" field."); |
| 82 | } |
| 83 | $accessToken = $parts['access_token']; |
| 84 | if (!array_key_exists('uid', $parts) || !is_string($parts['uid'])) { |
| 85 | throw new Exception_BadResponse("Missing \"uid\" string field."); |
| 86 | } |
| 87 | $refreshToken = $parts['refresh_token']; |
| 88 | if (!array_key_exists('refresh_token', $parts) || !is_string($parts['refresh_token'])) { |
| 89 | throw new Exception_BadResponse("Missing \"refresh_token\" string field."); |
| 90 | } |
| 91 | $userId = $parts['uid']; |
| 92 | |
| 93 | if ($tokenType !== "Bearer" && $tokenType !== "bearer") { |
| 94 | throw new Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got " |
| 95 | . Util::q($tokenType)); |
| 96 | } |
| 97 | |
| 98 | return array($accessToken, $refreshToken, $userId); |
| 99 | } |
| 100 | } |
| 101 |