1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth1; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OAuth1\Contracts\ConfigInterface; |
7
|
|
|
use OAuth1\Contracts\OAuth1ClientInterface; |
8
|
|
|
use OAuth1\Contracts\Signers\SignerInterface; |
9
|
|
|
use OAuth1\Flows\AccessTokenFlow; |
10
|
|
|
use OAuth1\Flows\AuthorizationFlow; |
11
|
|
|
use OAuth1\Flows\GrantedFlow; |
12
|
|
|
use OAuth1\Flows\RequestTokenFlow; |
13
|
|
|
use OAuth1\Signers\HmacSha1Signer; |
14
|
|
|
|
15
|
|
|
class OAuth1 implements OAuth1ClientInterface |
16
|
|
|
{ |
17
|
|
|
use GrantedFlow, |
18
|
|
|
AccessTokenFlow, |
19
|
|
|
RequestTokenFlow, |
20
|
|
|
AuthorizationFlow; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Http client instance. |
24
|
|
|
* |
25
|
|
|
* @return \OAuth1\Contracts\HttpClientInterface |
26
|
|
|
*/ |
27
|
|
|
protected $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Client configuration. |
31
|
|
|
* |
32
|
|
|
* @var \OAuth1\Contracts\ConfigInterface |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* OAuth signer instance. |
38
|
|
|
* |
39
|
|
|
* @return \OAuth1\Contracts\Signers\SignerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $signer; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new instance of Generic class. |
45
|
|
|
* |
46
|
|
|
* @param \OAuth1\Contracts\ConfigInterface|array $config |
47
|
|
|
* @param \OAuth1\Contracts\Signers\SignerInterface|null $signer |
48
|
|
|
*/ |
49
|
56 |
|
public function __construct($config, SignerInterface $signer = null) |
50
|
|
|
{ |
51
|
56 |
|
if (is_array($config)) { |
52
|
56 |
|
$config = Config::fromArray($config); |
53
|
56 |
|
} elseif (!$config instanceof ConfigInterface) { |
54
|
1 |
|
throw new InvalidArgumentException('OAuth1 client configuration must be a valid array or an instance of OAuth1\Config class.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
56 |
|
$this->config = $config; |
58
|
56 |
|
$this->signer = $signer; |
59
|
56 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get http client instance. |
63
|
|
|
* |
64
|
|
|
* @return \OAuth1\Contracts\HttpClientInterface |
65
|
|
|
*/ |
66
|
14 |
|
public function httpClient() |
67
|
|
|
{ |
68
|
14 |
|
if (is_null($this->httpClient)) { |
69
|
14 |
|
$this->httpClient = new HttpClient(); |
70
|
14 |
|
} |
71
|
|
|
|
72
|
14 |
|
return $this->httpClient; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get client configuration. |
77
|
|
|
* |
78
|
|
|
* @return \OAuth1\Contracts\ConfigInterface |
79
|
|
|
*/ |
80
|
43 |
|
public function config() |
81
|
|
|
{ |
82
|
43 |
|
return $this->config; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get signer. |
87
|
|
|
* |
88
|
|
|
* @return \OAuth1\Contracts\Signers\SignerInterface |
89
|
|
|
*/ |
90
|
19 |
|
public function signer() |
91
|
|
|
{ |
92
|
19 |
|
if (is_null($this->signer)) { |
93
|
19 |
|
$this->signer = new HmacSha1Signer($this->config()->consumerSecret()); |
94
|
19 |
|
} |
95
|
|
|
|
96
|
19 |
|
return $this->signer; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Generate random nonce. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
16 |
|
public function nonce() |
105
|
|
|
{ |
106
|
16 |
|
return md5(mt_rand()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get current timestamp. |
111
|
|
|
* |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
16 |
|
public function timestamp() |
115
|
|
|
{ |
116
|
16 |
|
return time(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get OAuth version. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
16 |
|
public function version() |
125
|
|
|
{ |
126
|
16 |
|
return '1.0'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get OAuth base protocol parameters. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
15 |
|
public function baseProtocolParameters() |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
15 |
|
'oauth_consumer_key' => $this->config()->consumerKey(), |
138
|
15 |
|
'oauth_nonce' => $this->nonce(), |
139
|
15 |
|
'oauth_signature_method' => $this->signer()->method(), |
140
|
15 |
|
'oauth_timestamp' => $this->timestamp(), |
141
|
15 |
|
'oauth_version' => $this->version(), |
142
|
15 |
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Build authorization headers. |
147
|
|
|
* |
148
|
|
|
* @param array $parameters |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
15 |
|
public function authorizationHeaders(array $parameters) |
153
|
|
|
{ |
154
|
15 |
|
$parameters = http_build_query($parameters, '', ', ', PHP_QUERY_RFC3986); |
155
|
|
|
|
156
|
15 |
|
return "OAuth $parameters"; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|