| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The base class for the two auth options. |
| 5 |
*/ |
| 6 |
class Dropbox_WebAuthBase extends Dropbox_AuthBase |
| 7 |
{ |
| 8 |
protected function _getAuthorizeUrl($redirectUri, $state) |
| 9 |
{ |
| 10 |
return Dropbox_RequestUtil::buildUrlForGetOrPut( |
| 11 |
$this->userLocale, |
| 12 |
$this->appInfo->getHost()->getWeb(), |
| 13 |
"1/oauth2/authorize", |
| 14 |
array( |
| 15 |
"client_id" => $this->appInfo->getKey(), |
| 16 |
"response_type" => "code", |
| 17 |
"redirect_uri" => $redirectUri, |
| 18 |
"state" => $state, |
| 19 |
)); |
| 20 |
} |
| 21 |
|
| 22 |
protected function _finish($code, $originalRedirectUri) |
| 23 |
{ |
| 24 |
// This endpoint requires "Basic" auth. |
| 25 |
$clientCredentials = $this->appInfo->getKey().":".$this->appInfo->getSecret(); |
| 26 |
$authHeaderValue = "Basic ".base64_encode($clientCredentials); |
| 27 |
|
| 28 |
$response = Dropbox_RequestUtil::doPostWithSpecificAuth( |
| 29 |
$this->clientIdentifier, $authHeaderValue, $this->userLocale, |
| 30 |
$this->appInfo->getHost()->getApi(), |
| 31 |
"1/oauth2/token", |
| 32 |
array( |
| 33 |
"grant_type" => "authorization_code", |
| 34 |
"code" => $code, |
| 35 |
"redirect_uri" => $originalRedirectUri, |
| 36 |
)); |
| 37 |
|
| 38 |
if ($response->statusCode !== 200) { |
| 39 |
throw Dropbox_RequestUtil::unexpectedStatus($response); |
| 40 |
} |
| 41 |
|
| 42 |
$parts = Dropbox_RequestUtil::parseResponseJson($response->body); |
| 43 |
|
| 44 |
if (!array_key_exists('token_type', $parts) or !is_string($parts['token_type'])) { |
| 45 |
throw new Dropbox_Exception_BadResponse("Missing \"token_type\" field."); |
| 46 |
} |
| 47 |
$tokenType = $parts['token_type']; |
| 48 |
if (!array_key_exists('access_token', $parts) or !is_string($parts['access_token'])) { |
| 49 |
throw new Dropbox_Exception_BadResponse("Missing \"access_token\" field."); |
| 50 |
} |
| 51 |
$accessToken = $parts['access_token']; |
| 52 |
if (!array_key_exists('uid', $parts) or !is_string($parts['uid'])) { |
| 53 |
throw new Dropbox_Exception_BadResponse("Missing \"uid\" string field."); |
| 54 |
} |
| 55 |
$userId = $parts['uid']; |
| 56 |
|
| 57 |
if ($tokenType !== "Bearer" && $tokenType !== "bearer") { |
| 58 |
throw new Dropbox_Exception_BadResponse("Unknown \"token_type\"; expecting \"Bearer\", got " |
| 59 |
.Dropbox_Client::q($tokenType)); |
| 60 |
} |
| 61 |
|
| 62 |
return array($accessToken, $userId); |
| 63 |
} |
| 64 |
} |
| 65 |
|