Skip to content

Commit 4cfb288

Browse files
committed
Add support for Oauth 1.0
Added new Config model that cna pre-configure Oauth 1.0 auth, with package suggestion in composer.json
1 parent b6527a3 commit 4cfb288

File tree

4 files changed

+214
-2
lines changed

4 files changed

+214
-2
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
"symfony/console": "~2.2",
2323
"phpunit/phpunit": "^4.8",
2424
"symfony/var-dumper": "~2.6",
25-
"mathiasgrimm/arraypath": "~1.3"
25+
"mathiasgrimm/arraypath": "~1.3",
26+
"guzzlehttp/oauth-subscriber": "^0.3.0"
27+
},
28+
"suggest": {
29+
"guzzlehttp/oauth-subscriber": "For authenticating with Oauth1.0"
2630
},
2731

2832
"autoload": {

composer.lock

Lines changed: 52 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace DMS\Service\Meetup\Config;
4+
5+
use DMS\Service\Meetup\Exception\MissingPackageException;
6+
use GuzzleHttp\Subscriber\Oauth\Oauth1;
7+
8+
final class OAuth1Config implements ClientConfig
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $consumerKey;
14+
15+
/**
16+
* @var string
17+
*/
18+
protected $consumerSecret;
19+
20+
/**
21+
* @var string
22+
*/
23+
protected $token;
24+
25+
/**
26+
* @var string
27+
*/
28+
protected $tokenSecret;
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $privateKeyFile;
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $privateKeyPassphrase;
39+
40+
/**
41+
* @var string
42+
*/
43+
protected $signatureMethod;
44+
45+
/**
46+
* @var bool
47+
*/
48+
protected $shouldSignAllRequests = true;
49+
50+
/**
51+
* @param string $consumerKey
52+
* @param string $consumerSecret
53+
*/
54+
private function __construct($consumerKey, $consumerSecret)
55+
{
56+
if (!class_exists(Oauth1::class)) {
57+
MissingPackageException::forOAuth1();
58+
}
59+
60+
$this->consumerKey = $consumerKey;
61+
$this->consumerSecret = $consumerSecret;
62+
}
63+
64+
public static function withTwoLeggedOauth(string $consumerKey, string $consumerSecret): self
65+
{
66+
$instance = new static($consumerKey, $consumerSecret);
67+
$instance->tokenSecret = false;
68+
return $instance;
69+
}
70+
71+
public static function withToken(
72+
string $consumerKey,
73+
string $consumerSecret,
74+
string $token,
75+
string $tokenSecret
76+
): self {
77+
$instance = new static($consumerKey, $consumerSecret);
78+
$instance->token = $token;
79+
$instance->tokenSecret = $tokenSecret;
80+
return $instance;
81+
}
82+
83+
public static function withRSASH1(
84+
string $consumerKey,
85+
string $consumerSecret,
86+
string $privateKeyFile,
87+
string $privateKeyPassphrase
88+
): self {
89+
$instance = new static($consumerKey, $consumerSecret);
90+
$instance->privateKeyFile = $privateKeyFile;
91+
$instance->privateKeyPassphrase = $privateKeyPassphrase;
92+
$instance->signatureMethod = Oauth1::SIGNATURE_METHOD_RSA;
93+
return $instance;
94+
}
95+
96+
/**
97+
* @return callable[]
98+
*/
99+
public function getMiddleware(): array
100+
{
101+
return [
102+
'OAuth1' => new Oauth1($this->buildConfig()),
103+
];
104+
}
105+
106+
/**
107+
* @return string[]
108+
*/
109+
public function getClientConfig(): array
110+
{
111+
$config = [];
112+
113+
if ($this->shouldSignAllRequests) {
114+
$config['auth'] = 'oauth';
115+
}
116+
117+
return $config;
118+
}
119+
120+
private function buildConfig(): array
121+
{
122+
$config = [
123+
'consumer_key' => $this->consumerKey,
124+
'consumer_secret' => $this->consumerSecret,
125+
];
126+
127+
if ($this->signatureMethod !== null) {
128+
$config['signature_method'] = $this->signatureMethod;
129+
}
130+
131+
if ($this->signatureMethod === Oauth1::SIGNATURE_METHOD_RSA) {
132+
$config['private_key_file'] = $this->privateKeyFile;
133+
$config['private_key_passphrase'] = $this->privateKeyPassphrase;
134+
}
135+
136+
if ($this->token !== null) {
137+
$config['token'] = $this->token;
138+
}
139+
140+
if ($this->tokenSecret !== null) {
141+
$config['token_secret'] = $this->tokenSecret;
142+
}
143+
144+
return $config;
145+
}
146+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DMS\Service\Meetup\Exception;
4+
5+
final class MissingPackageException extends \Exception
6+
{
7+
public static function forOAuth1(): self
8+
{
9+
return new static("Please install the guzzlehttp/oauth-subscriber package to use this Auth method");
10+
}
11+
}

0 commit comments

Comments
 (0)