Exception
5 years ago
WebAuthException
5 years ago
certs
5 years ago
AppInfo.php
5 years ago
AppInfoLoadException.php
5 years ago
ArrayEntryStore.php
5 years ago
AuthBase.php
5 years ago
AuthInfo.php
5 years ago
AuthInfoLoadException.php
5 years ago
Checker.php
5 years ago
Client.php
5 years ago
Curl.php
5 years ago
CurlStreamRelay.php
5 years ago
DeserializeException.php
5 years ago
DropboxMetadataHeaderCatcher.php
5 years ago
Exception.php
5 years ago
Host.php
5 years ago
HttpResponse.php
5 years ago
OAuth1AccessToken.php
5 years ago
OAuth1Upgrader.php
5 years ago
Path.php
5 years ago
RequestUtil.php
5 years ago
RootCertificates.php
5 years ago
SSLTester.php
5 years ago
Security.php
5 years ago
StreamReadException.php
5 years ago
Util.php
5 years ago
ValueStore.php
5 years ago
WebAuth.php
5 years ago
WebAuthBase.php
5 years ago
WebAuthNoRedirect.php
5 years ago
WriteMode.php
5 years ago
autoload.php
5 years ago
strict.php
5 years ago
OAuth1Upgrader.php
143 lines
| 1 | <?php |
| 2 | namespace Dropbox; |
| 3 | |
| 4 | /** |
| 5 | * Lets you convert OAuth 1 access tokens to OAuth 2 access tokens. First call {@link |
| 6 | * OAuth1AccessTokenUpgrader::createOAuth2AccessToken()} to get an OAuth 2 access token. |
| 7 | * If that succeeds, call {@link OAuth1AccessTokenUpgrader::disableOAuth1AccessToken()} |
| 8 | * to disable the OAuth 1 access token. |
| 9 | * |
| 10 | * <code> |
| 11 | * use \Dropbox as dbx; |
| 12 | * $appInfo = dbx\AppInfo::loadFromJsonFile(...); |
| 13 | * $clientIdentifier = "my-app/1.0"; |
| 14 | * $oauth1AccessToken = dbx\OAuth1AccessToken(...); |
| 15 | * |
| 16 | * $upgrader = new dbx\OAuth1AccessTokenUpgrader($appInfo, $clientIdentifier, ...); |
| 17 | * $oauth2AccessToken = $upgrader->getOAuth2AccessToken($oauth1AccessToken); |
| 18 | * $upgrader->disableOAuth1AccessToken($oauth1AccessToken); |
| 19 | * </code> |
| 20 | */ |
| 21 | class OAuth1Upgrader extends AuthBase |
| 22 | { |
| 23 | /** |
| 24 | * Given an existing active OAuth 1 access token, make a Dropbox API call to get a new OAuth 2 |
| 25 | * access token that represents the same user and app. |
| 26 | * |
| 27 | * See <a href="https://www.dropbox.com/developers/core/docs#oa1-from-oa1">/oauth2/token_from_oauth1</a>. |
| 28 | * |
| 29 | * @param OAuth1AccessToken $oauth1AccessToken |
| 30 | * |
| 31 | * @return string |
| 32 | * The OAuth 2 access token. |
| 33 | * |
| 34 | * @throws Exception |
| 35 | */ |
| 36 | // function createOAuth2AccessToken($oauth1AccessToken) |
| 37 | // { |
| 38 | // OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken); |
| 39 | |
| 40 | // $response = self::doPost($oauth1AccessToken, "1/oauth2/token_from_oauth1"); |
| 41 | |
| 42 | // if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response); |
| 43 | |
| 44 | // $parts = RequestUtil::parseResponseJson($response->body); |
| 45 | |
| 46 | // if (!array_key_exists('token_type', $parts) || !is_string($parts['token_type'])) { |
| 47 | // throw new Exception_BadResponse("Missing \"token_type\" field."); |
| 48 | // } |
| 49 | // $tokenType = $parts['token_type']; |
| 50 | // if (!array_key_exists('access_token', $parts) || !is_string($parts['access_token'])) { |
| 51 | // throw new Exception_BadResponse("Missing \"access_token\" field."); |
| 52 | // } |
| 53 | // $accessToken = $parts['access_token']; |
| 54 | |
| 55 | // if ($tokenType !== "Bearer" && $tokenType !== "bearer") { |
| 56 | // throw new Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got " |
| 57 | // . Util::q($tokenType)); |
| 58 | // } |
| 59 | |
| 60 | // return $accessToken; |
| 61 | // } |
| 62 | |
| 63 | /*Dropbox api 2*/ |
| 64 | function createOAuth2AccessToken($oauth1AccessToken) |
| 65 | { |
| 66 | OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken); |
| 67 | |
| 68 | $response = self::doPost($oauth1AccessToken, "2/auth/token/from_oauth1"); |
| 69 | |
| 70 | if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response); |
| 71 | |
| 72 | $parts = RequestUtil::parseResponseJson($response->body); |
| 73 | |
| 74 | if (!array_key_exists('token_type', $parts) || !is_string($parts['token_type'])) { |
| 75 | throw new Exception_BadResponse("Missing \"token_type\" field."); |
| 76 | } |
| 77 | $tokenType = $parts['token_type']; |
| 78 | if (!array_key_exists('access_token', $parts) || !is_string($parts['access_token'])) { |
| 79 | throw new Exception_BadResponse("Missing \"access_token\" field."); |
| 80 | } |
| 81 | $accessToken = $parts['access_token']; |
| 82 | |
| 83 | if ($tokenType !== "Bearer" && $tokenType !== "bearer") { |
| 84 | throw new Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got " |
| 85 | . Util::q($tokenType)); |
| 86 | } |
| 87 | |
| 88 | return $accessToken; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Make a Dropbox API call to disable the given OAuth 1 access token. |
| 93 | * |
| 94 | * See <a href="https://www.dropbox.com/developers/core/docs#disable-token">/disable_access_token</a>. |
| 95 | * |
| 96 | * @param OAuth1AccessToken $oauth1AccessToken |
| 97 | * |
| 98 | * @throws Exception |
| 99 | */ |
| 100 | // function disableOAuth1AccessToken($oauth1AccessToken) |
| 101 | // { |
| 102 | // OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken); |
| 103 | |
| 104 | // $response = self::doPost($oauth1AccessToken, "1/disable_access_token"); |
| 105 | |
| 106 | // if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response); |
| 107 | // } |
| 108 | |
| 109 | /*Dropbox api 2*/ |
| 110 | function disableOAuth1AccessToken($oauth1AccessToken) |
| 111 | { |
| 112 | OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken); |
| 113 | |
| 114 | $response = self::doPost($oauth1AccessToken, "2/auth/token/revoke"); |
| 115 | |
| 116 | if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param OAuth1AccessToken $oauth1AccessToken |
| 121 | * @param string $path |
| 122 | * |
| 123 | * @return HttpResponse |
| 124 | * |
| 125 | * @throws Exception |
| 126 | */ |
| 127 | private function doPost($oauth1AccessToken, $path) |
| 128 | { |
| 129 | // Construct the OAuth 1 header. |
| 130 | $signature = rawurlencode($this->appInfo->getSecret()) . "&" . rawurlencode($oauth1AccessToken->getSecret()); |
| 131 | $authHeaderValue = "OAuth oauth_signature_method=\"PLAINTEXT\"" |
| 132 | . ", oauth_consumer_key=\"" . rawurlencode($this->appInfo->getKey()) . "\"" |
| 133 | . ", oauth_token=\"" . rawurlencode($oauth1AccessToken->getKey()) . "\"" |
| 134 | . ", oauth_signature=\"" . $signature . "\""; |
| 135 | |
| 136 | return RequestUtil::doPostWithSpecificAuth( |
| 137 | $this->clientIdentifier, $authHeaderValue, $this->userLocale, |
| 138 | $this->appInfo->getHost()->getApi(), |
| 139 | $path, |
| 140 | null); |
| 141 | } |
| 142 | } |
| 143 |