1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
<?php
namespace Dropbox;
class OAuth1Upgrader extends AuthBase
{
function createOAuth2AccessToken($oauth1AccessToken)
{
OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken);
$response = self::doPost($oauth1AccessToken, "1/oauth2/token_from_oauth1");
if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response);
$parts = RequestUtil::parseResponseJson($response->body);
if (!array_key_exists('token_type', $parts) || !is_string($parts['token_type'])) {
throw new Exception_BadResponse("Missing \"token_type\" field.");
}
$tokenType = $parts['token_type'];
if (!array_key_exists('access_token', $parts) || !is_string($parts['access_token'])) {
throw new Exception_BadResponse("Missing \"access_token\" field.");
}
$accessToken = $parts['access_token'];
if ($tokenType !== "Bearer" && $tokenType !== "bearer") {
throw new Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got "
. Util::q($tokenType));
}
return $accessToken;
}
function disableOAuth1AccessToken($oauth1AccessToken)
{
OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken);
$response = self::doPost($oauth1AccessToken, "1/disable_access_token");
if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response);
}
private function doPost($oauth1AccessToken, $path)
{
$signature = rawurlencode($this->appInfo->getSecret()) . "&" . rawurlencode($oauth1AccessToken->getSecret());
$authHeaderValue = "OAuth oauth_signature_method=\"PLAINTEXT\""
. ", oauth_consumer_key=\"" . rawurlencode($this->appInfo->getKey()) . "\""
. ", oauth_token=\"" . rawurlencode($oauth1AccessToken->getKey()) . "\""
. ", oauth_signature=\"" . $signature . "\"";
return RequestUtil::doPostWithSpecificAuth(
$this->clientIdentifier, $authHeaderValue, $this->userLocale,
$this->appInfo->getHost()->getApi(),
$path,
null);
}
}