Ejemplo n.º 1
0
 protected function _finish($code, $originalRedirectUri)
 {
     // This endpoint requires "Basic" auth.
     $clientCredentials = $this->appInfo->getKey() . ":" . $this->appInfo->getSecret();
     $authHeaderValue = "Basic " . base64_encode($clientCredentials);
     $response = Dropbox_RequestUtil::doPostWithSpecificAuth($this->clientIdentifier, $authHeaderValue, $this->userLocale, $this->appInfo->getHost()->getApi(), "1/oauth2/token", array("grant_type" => "authorization_code", "code" => $code, "redirect_uri" => $originalRedirectUri));
     if ($response->statusCode !== 200) {
         throw Dropbox_RequestUtil::unexpectedStatus($response);
     }
     $parts = Dropbox_RequestUtil::parseResponseJson($response->body);
     if (!array_key_exists('token_type', $parts) or !is_string($parts['token_type'])) {
         throw new Dropbox_Exception_BadResponse("Missing \"token_type\" field.");
     }
     $tokenType = $parts['token_type'];
     if (!array_key_exists('access_token', $parts) or !is_string($parts['access_token'])) {
         throw new Dropbox_Exception_BadResponse("Missing \"access_token\" field.");
     }
     $accessToken = $parts['access_token'];
     if (!array_key_exists('uid', $parts) or !is_string($parts['uid'])) {
         throw new Dropbox_Exception_BadResponse("Missing \"uid\" string field.");
     }
     $userId = $parts['uid'];
     if ($tokenType !== "Bearer" && $tokenType !== "bearer") {
         throw new Dropbox_Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got  " . Dropbox_Client::q($tokenType));
     }
     return array($accessToken, $userId);
 }
Ejemplo n.º 2
0
 /**
  * Constructor.
  *
  * @param Dropbox_AppInfo $appInfo
  *                                          See {@link getAppInfo()}
  * @param string          $clientIdentifier
  *                                          See {@link getClientIdentifier()}
  * @param null|string     $userLocale
  *                                          See {@link getUserLocale()}
  */
 public function __construct($appInfo, $clientIdentifier, $userLocale = null)
 {
     Dropbox_AppInfo::checkArg("appInfo", $appInfo);
     Dropbox_Client::checkClientIdentifierArg("clientIdentifier", $clientIdentifier);
     Dropbox_Checker::argStringNonEmptyOrNull("userLocale", $userLocale);
     $this->appInfo = $appInfo;
     $this->clientIdentifier = $clientIdentifier;
     $this->userLocale = $userLocale;
 }
Ejemplo n.º 3
0
 /**
  * Given an existing active OAuth 1 access token, make a Dropbox API call to get a new OAuth 2
  * access token that represents the same user and app.
  *
  * See <a href="https://www.dropbox.com/developers/core/docs#oa1-from-oa1">/oauth2/token_from_oauth1</a>.
  *
  * @param Dropbox_OAuth1AccessToken $oauth1AccessToken
  *
  * @return string
  *                The OAuth 2 access token.
  *
  * @throws Dropbox_Exception
  */
 public function createOAuth2AccessToken($oauth1AccessToken)
 {
     Dropbox_OAuth1AccessToken::checkArg("oauth1AccessToken", $oauth1AccessToken);
     /** @handled static fix */
     $response = self::doPost($oauth1AccessToken, "1/oauth2/token_from_oauth1");
     if ($response->statusCode !== 200) {
         throw Dropbox_RequestUtil::unexpectedStatus($response);
     }
     $parts = Dropbox_RequestUtil::parseResponseJson($response->body);
     if (!array_key_exists('token_type', $parts) or !is_string($parts['token_type'])) {
         throw new Dropbox_Exception_BadResponse("Missing \"token_type\" field.");
     }
     $tokenType = $parts['token_type'];
     if (!array_key_exists('access_token', $parts) or !is_string($parts['access_token'])) {
         throw new Dropbox_Exception_BadResponse("Missing \"access_token\" field.");
     }
     $accessToken = $parts['access_token'];
     if ($tokenType !== "Bearer" && $tokenType !== "bearer") {
         throw new Dropbox_Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got  " . Dropbox_Client::q($tokenType));
     }
     return $accessToken;
 }
Ejemplo n.º 4
0
 /**
  * Call this after the user has visited the authorize URL ({@link start()}), approved your app,
  * and was redirected to your redirect URI.
  *
  * See <a href="https://www.dropbox.com/developers/core/docs#oa2-token">/oauth2/token</a>.
  *
  * @param array $queryParams
  *                           The query parameters on the GET request to your redirect URI.
  *
  * @return array
  *               A <code>list(string $accessToken, string $userId, string $urlState)</code>, where
  *               <code>$accessToken</code> can be used to construct a {@link Client}, <code>$userId</code>
  *               is the user ID of the user's Dropbox account, and <code>$urlState</code> is the
  *               value you originally passed in to {@link start()}.
  *
  * @throws Dropbox_Exception
  *                                              Thrown if there's an error getting the access token from Dropbox.
  * @throws Dropbox_WebAuthException_BadRequest
  * @throws Dropbox_WebAuthException_BadState
  * @throws Dropbox_WebAuthException_Csrf
  * @throws Dropbox_WebAuthException_NotApproved
  * @throws Dropbox_WebAuthException_Provider
  *
  *
  */
 public function finish($queryParams)
 {
     Dropbox_Checker::argArray("queryParams", $queryParams);
     $csrfTokenFromSession = $this->csrfTokenStore->get();
     Dropbox_Checker::argStringOrNull("this->csrfTokenStore->get()", $csrfTokenFromSession);
     // Check well-formedness of request.
     if (!isset($queryParams['state'])) {
         throw new Dropbox_WebAuthException_BadRequest("Missing query parameter 'state'.");
     }
     $state = $queryParams['state'];
     Dropbox_Checker::argString("queryParams['state']", $state);
     $error = null;
     $errorDescription = null;
     if (isset($queryParams['error'])) {
         $error = $queryParams['error'];
         Dropbox_Checker::argString("queryParams['error']", $error);
         if (isset($queryParams['error_description'])) {
             $errorDescription = $queryParams['error_description'];
             Dropbox_Checker::argString("queryParams['error_description']", $errorDescription);
         }
     }
     $code = null;
     if (isset($queryParams['code'])) {
         $code = $queryParams['code'];
         Dropbox_Checker::argString("queryParams['code']", $code);
     }
     if ($code !== null && $error !== null) {
         throw new Dropbox_WebAuthException_BadRequest("Query parameters 'code' and 'error' are both set;" . " only one must be set.");
     }
     if ($code === null && $error === null) {
         throw new Dropbox_WebAuthException_BadRequest("Neither query parameter 'code' or 'error' is set.");
     }
     // Check CSRF token
     if ($csrfTokenFromSession === null) {
         throw new Dropbox_WebAuthException_BadState();
     }
     $splitPos = strpos($state, "|");
     if ($splitPos === false) {
         $givenCsrfToken = $state;
         $urlState = null;
     } else {
         $givenCsrfToken = substr($state, 0, $splitPos);
         $urlState = substr($state, $splitPos + 1);
     }
     if (!Dropbox_Security::stringEquals($csrfTokenFromSession, $givenCsrfToken)) {
         throw new Dropbox_WebAuthException_Csrf("Expected " . Dropbox_Client::q($csrfTokenFromSession) . ", got " . Dropbox_Client::q($givenCsrfToken) . ".");
     }
     $this->csrfTokenStore->clear();
     // Check for error identifier
     if ($error !== null) {
         if ($error === 'access_denied') {
             // When the user clicks "Deny".
             if ($errorDescription === null) {
                 throw new Dropbox_WebAuthException_NotApproved("No additional description from Dropbox.");
             } else {
                 throw new Dropbox_WebAuthException_NotApproved("Additional description from Dropbox: {$errorDescription}");
             }
         } else {
             // All other errors.
             $fullMessage = $error;
             if ($errorDescription !== null) {
                 $fullMessage .= ": ";
                 $fullMessage .= $errorDescription;
             }
             throw new Dropbox_WebAuthException_Provider($fullMessage);
         }
     }
     // If everything went ok, make the network call to get an access token.
     list($accessToken, $userId) = $this->_finish($code, $this->redirectUri);
     return array($accessToken, $userId, $urlState);
 }