Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'x_auth_access_type' parameter for Twitter to change the access level of an application #138

Merged
merged 1 commit into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);
```

Expand Down
21 changes: 18 additions & 3 deletions src/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');

Expand Down
74 changes: 74 additions & 0 deletions src/Server/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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];
}
}
}
}