Skip to content

Commit

Permalink
Merge pull request #138 from Diegslapasteque/feature/twitter-applicat…
Browse files Browse the repository at this point in the history
…ion-access-level

Add 'x_auth_access_type' parameter for Twitter to change the access level of an application
  • Loading branch information
bencorlett authored Aug 3, 2021
2 parents 88d0557 + 8bf4d9a commit 6247ffb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
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];
}
}
}
}

0 comments on commit 6247ffb

Please sign in to comment.