diff --git a/README.md b/README.md index a6c70dc..e99d3be 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ $server = new League\OAuth1\Client\Server\Twitter([ 'identifier' => 'your-identifier', 'secret' => 'your-secret', 'callback_uri' => "http://your-callback-uri/", + 'scope' => 'your-application-scope' // optional ('read', 'write'), defaults to 'read' ]); ``` diff --git a/src/Server/Server.php b/src/Server/Server.php index 9341beb..5048043 100644 --- a/src/Server/Server.php +++ b/src/Server/Server.php @@ -552,6 +552,17 @@ protected function additionalProtocolParameters() return []; } + /** + * Any additional required protocol parameters for an + * OAuth request to get temporary credentials. + * + * @return array + */ + protected function additionalTemporaryCredentialsProtocolParameters() + { + return []; + } + /** * Generate the OAuth protocol header for a temporary credentials * request, based on the URI. @@ -562,9 +573,13 @@ protected function additionalProtocolParameters() */ protected function temporaryCredentialsProtocolHeader($uri) { - $parameters = array_merge($this->baseProtocolParameters(), [ - 'oauth_callback' => $this->clientCredentials->getCallbackUri(), - ]); + $parameters = array_merge( + $this->baseProtocolParameters(), + $this->additionalTemporaryCredentialsProtocolParameters(), + [ + 'oauth_callback' => $this->clientCredentials->getCallbackUri(), + ] + ); $parameters['oauth_signature'] = $this->signature->sign($uri, $parameters, 'POST'); diff --git a/src/Server/Twitter.php b/src/Server/Twitter.php index b67b606..ce95fdf 100644 --- a/src/Server/Twitter.php +++ b/src/Server/Twitter.php @@ -3,9 +3,53 @@ namespace League\OAuth1\Client\Server; use League\OAuth1\Client\Credentials\TokenCredentials; +use League\OAuth1\Client\Signature\SignatureInterface; class Twitter extends Server { + /** + * Application scope. + * + * @var string + */ + protected $applicationScope; + + /** + * @inheritDoc + */ + public function __construct($clientCredentials, SignatureInterface $signature = null) + { + parent::__construct($clientCredentials, $signature); + + if (is_array($clientCredentials)) { + $this->parseConfiguration($clientCredentials); + } + } + + /** + * Set the application scope. + * + * @param string $applicationScope + * + * @return Twitter + */ + public function setApplicationScope($applicationScope) + { + $this->applicationScope = $applicationScope; + + return $this; + } + + /** + * Get application scope. + * + * @return string + */ + public function getApplicationScope() + { + return $this->applicationScope ?: 'read'; + } + /** * @inheritDoc */ @@ -97,4 +141,34 @@ public function userScreenName($data, TokenCredentials $tokenCredentials) { return $data['name']; } + + /** + * @inheritDoc + */ + protected function additionalTemporaryCredentialsProtocolParameters() + { + return [ + 'x_auth_access_type' => $this->getApplicationScope() + ]; + } + + /** + * Parse configuration array to set attributes. + * + * @param array $configuration + * + * @return void + */ + private function parseConfiguration(array $configuration = []) + { + $configToPropertyMap = [ + 'scope' => 'applicationScope', + ]; + + foreach ($configToPropertyMap as $config => $property) { + if (isset($configuration[$config])) { + $this->$property = $configuration[$config]; + } + } + } }