Skip to content

Commit 6247ffb

Browse files
authored
Merge pull request #138 from Diegslapasteque/feature/twitter-application-access-level
Add 'x_auth_access_type' parameter for Twitter to change the access level of an application
2 parents 88d0557 + 8bf4d9a commit 6247ffb

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ $server = new League\OAuth1\Client\Server\Twitter([
137137
'identifier' => 'your-identifier',
138138
'secret' => 'your-secret',
139139
'callback_uri' => "http://your-callback-uri/",
140+
'scope' => 'your-application-scope' // optional ('read', 'write'), defaults to 'read'
140141
]);
141142
```
142143

src/Server/Server.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,17 @@ protected function additionalProtocolParameters()
552552
return [];
553553
}
554554

555+
/**
556+
* Any additional required protocol parameters for an
557+
* OAuth request to get temporary credentials.
558+
*
559+
* @return array
560+
*/
561+
protected function additionalTemporaryCredentialsProtocolParameters()
562+
{
563+
return [];
564+
}
565+
555566
/**
556567
* Generate the OAuth protocol header for a temporary credentials
557568
* request, based on the URI.
@@ -562,9 +573,13 @@ protected function additionalProtocolParameters()
562573
*/
563574
protected function temporaryCredentialsProtocolHeader($uri)
564575
{
565-
$parameters = array_merge($this->baseProtocolParameters(), [
566-
'oauth_callback' => $this->clientCredentials->getCallbackUri(),
567-
]);
576+
$parameters = array_merge(
577+
$this->baseProtocolParameters(),
578+
$this->additionalTemporaryCredentialsProtocolParameters(),
579+
[
580+
'oauth_callback' => $this->clientCredentials->getCallbackUri(),
581+
]
582+
);
568583

569584
$parameters['oauth_signature'] = $this->signature->sign($uri, $parameters, 'POST');
570585

src/Server/Twitter.php

+74
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,53 @@
33
namespace League\OAuth1\Client\Server;
44

55
use League\OAuth1\Client\Credentials\TokenCredentials;
6+
use League\OAuth1\Client\Signature\SignatureInterface;
67

78
class Twitter extends Server
89
{
10+
/**
11+
* Application scope.
12+
*
13+
* @var string
14+
*/
15+
protected $applicationScope;
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function __construct($clientCredentials, SignatureInterface $signature = null)
21+
{
22+
parent::__construct($clientCredentials, $signature);
23+
24+
if (is_array($clientCredentials)) {
25+
$this->parseConfiguration($clientCredentials);
26+
}
27+
}
28+
29+
/**
30+
* Set the application scope.
31+
*
32+
* @param string $applicationScope
33+
*
34+
* @return Twitter
35+
*/
36+
public function setApplicationScope($applicationScope)
37+
{
38+
$this->applicationScope = $applicationScope;
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* Get application scope.
45+
*
46+
* @return string
47+
*/
48+
public function getApplicationScope()
49+
{
50+
return $this->applicationScope ?: 'read';
51+
}
52+
953
/**
1054
* @inheritDoc
1155
*/
@@ -97,4 +141,34 @@ public function userScreenName($data, TokenCredentials $tokenCredentials)
97141
{
98142
return $data['name'];
99143
}
144+
145+
/**
146+
* @inheritDoc
147+
*/
148+
protected function additionalTemporaryCredentialsProtocolParameters()
149+
{
150+
return [
151+
'x_auth_access_type' => $this->getApplicationScope()
152+
];
153+
}
154+
155+
/**
156+
* Parse configuration array to set attributes.
157+
*
158+
* @param array $configuration
159+
*
160+
* @return void
161+
*/
162+
private function parseConfiguration(array $configuration = [])
163+
{
164+
$configToPropertyMap = [
165+
'scope' => 'applicationScope',
166+
];
167+
168+
foreach ($configToPropertyMap as $config => $property) {
169+
if (isset($configuration[$config])) {
170+
$this->$property = $configuration[$config];
171+
}
172+
}
173+
}
100174
}

0 commit comments

Comments
 (0)