1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter\Components; |
4
|
|
|
use mpyw\Cowitter\Helpers\CredentialNormalizer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @property string $consumer_key |
8
|
|
|
* @property string $consumer_secret |
9
|
|
|
* @property string $token |
10
|
|
|
* @property string $token_secret |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Credential implements \ArrayAccess |
14
|
|
|
{ |
15
|
|
|
protected $consumer_key; |
16
|
|
|
protected $consumer_secret; |
17
|
|
|
protected $token = ''; |
18
|
|
|
protected $token_secret = ''; |
19
|
|
|
|
20
|
69 |
|
public function __construct(array $params = []) |
21
|
69 |
|
{ |
22
|
69 |
|
foreach ($params as $key => $value) { |
23
|
69 |
|
$this->{CredentialNormalizer::normalizeCredentialParamName($key)} = $value; |
24
|
|
|
} |
25
|
69 |
|
if ($this->consumer_key === null || $this->consumer_secret === null) { |
26
|
1 |
|
throw new \DomainException('"consumer_key" and "consumer_secret" are at least required.'); |
27
|
|
|
} |
28
|
68 |
|
} |
29
|
|
|
|
30
|
|
|
public function with(array $params = []) |
31
|
|
|
{ |
32
|
|
|
$clone = clone $this; |
33
|
|
|
foreach ($params as $key => $value) { |
34
|
|
|
$this->{CredentialNormalizer::normalizeCredentialParamName($key)} = $value; |
35
|
|
|
} |
36
|
|
|
return $clone; |
37
|
|
|
} |
38
|
|
|
|
39
|
18 |
|
public function toArray($assoc = false) |
40
|
18 |
|
{ |
41
|
18 |
|
$values = get_object_vars($this); |
42
|
18 |
|
return $assoc ? $values : array_values($values); |
43
|
|
|
} |
44
|
|
|
|
45
|
12 |
|
public function offsetGet($offset) |
46
|
12 |
|
{ |
47
|
12 |
|
return $this->{CredentialNormalizer::normalizeCredentialParamName($offset)}; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function offsetSet($offset, $value) |
51
|
|
|
{ |
52
|
|
|
throw new \BadMethodCallException('Unsupported operation.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function offsetUnset($offset) |
56
|
|
|
{ |
57
|
|
|
throw new \BadMethodCallException('Unsupported operation.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function offsetExists($offset) |
61
|
|
|
{ |
62
|
|
|
return isset($this->$offset); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function __get($key) |
66
|
2 |
|
{ |
67
|
2 |
|
return $this->{CredentialNormalizer::normalizeCredentialParamName($key)}; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function __isset($key) |
71
|
|
|
{ |
72
|
|
|
return isset($this->{CredentialNormalizer::normalizeCredentialParamName($key)}); |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
public function getAuthorizeUrl($force_login = false) |
76
|
4 |
|
{ |
77
|
4 |
|
$params = http_build_query([ |
78
|
4 |
|
'oauth_token' => $this->token, |
79
|
4 |
|
'force_login' => $force_login ? 1 : null, |
80
|
4 |
|
], '', '&'); |
81
|
4 |
|
return 'https://api.twitter.com/oauth/authorize?' . $params; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getAuthenticateUrl($force_login = false) |
85
|
|
|
{ |
86
|
|
|
$params = http_build_query([ |
87
|
|
|
'oauth_token' => $this->token, |
88
|
|
|
'force_login' => $force_login ? 1 : null, |
89
|
|
|
], '', '&'); |
90
|
|
|
return 'https://api.twitter.com/oauth/authenticate?' . $params; |
91
|
|
|
} |
92
|
|
|
|
93
|
46 |
|
public function getOAuthHeaders($url, $method, array $endpoint_params) |
94
|
46 |
|
{ |
95
|
46 |
|
return static::buildOAuthHeaders( |
96
|
46 |
|
$url, |
97
|
46 |
|
$method, |
98
|
|
|
[ |
99
|
46 |
|
'oauth_consumer_key' => $this->consumer_key, |
100
|
46 |
|
'oauth_token' => $this->token, |
101
|
|
|
], |
102
|
46 |
|
[$this->consumer_secret, $this->token_secret], |
103
|
46 |
|
$endpoint_params |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
public function getBasicHeaders() |
108
|
4 |
|
{ |
109
|
4 |
|
$header = base64_encode(implode(':', array_map('rawurlencode', [ |
110
|
4 |
|
$this->consumer_key, |
111
|
4 |
|
$this->consumer_secret, |
112
|
|
|
]))); |
113
|
4 |
|
return ['Authorization: Basic ' . $header]; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
public function getBearerHeaders() |
117
|
2 |
|
{ |
118
|
2 |
|
return ['Authorization: Bearer ' . $this->token]; |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
public function getOAuthHeadersForOAuthEcho() |
122
|
6 |
|
{ |
123
|
6 |
|
$url = 'https://api.twitter.com/1.1/account/verify_credentials.json'; |
124
|
6 |
|
$headers = $this->getOAuthHeaders($url, 'GET', []); |
125
|
|
|
return [ |
126
|
6 |
|
'X-Auth-Service-Provider: ' . $url, |
127
|
6 |
|
'X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/", ' . substr($headers[0], 21), |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
6 |
|
public function getOAuthHeadersForRequestToken($oauth_callback = null) |
132
|
6 |
|
{ |
133
|
6 |
|
return static::buildOAuthHeaders( |
134
|
6 |
|
'https://api.twitter.com/oauth/request_token', |
135
|
6 |
|
'POST', |
136
|
|
|
[ |
137
|
6 |
|
'oauth_consumer_key' => $this->consumer_key, |
138
|
6 |
|
] + ($oauth_callback === null ? [] : |
139
|
|
|
[ |
140
|
6 |
|
'oauth_callback' => $oauth_callback, |
141
|
|
|
]), |
142
|
6 |
|
[$this->consumer_secret, ''], |
143
|
6 |
|
[] |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
4 |
|
public function getOAuthHeadersForAccessToken($oauth_verifier) |
148
|
4 |
|
{ |
149
|
4 |
|
return static::buildOAuthHeaders( |
150
|
4 |
|
'https://api.twitter.com/oauth/access_token', |
151
|
4 |
|
'POST', |
152
|
|
|
[ |
153
|
4 |
|
'oauth_consumer_key' => $this->consumer_key, |
154
|
4 |
|
'oauth_token' => $this->token, |
155
|
4 |
|
'oauth_verifier' => $oauth_verifier, |
156
|
|
|
], |
157
|
4 |
|
[$this->consumer_secret, $this->token_secret], |
158
|
4 |
|
[] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
public function getXAuthHeadersForAccessToken($username, $password) |
163
|
2 |
|
{ |
164
|
2 |
|
return static::buildOAuthHeaders( |
165
|
2 |
|
'https://api.twitter.com/oauth/access_token', |
166
|
2 |
|
'POST', |
167
|
|
|
[ |
168
|
2 |
|
'oauth_consumer_key' => $this->consumer_key, |
169
|
|
|
], |
170
|
2 |
|
[$this->consumer_secret, ''], |
171
|
|
|
[ |
172
|
2 |
|
'x_auth_username' => $username, |
173
|
2 |
|
'x_auth_password' => $password, |
174
|
2 |
|
'x_auth_mode' => 'client_auth', |
175
|
|
|
] |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
56 |
|
protected static function buildOAuthHeaders($url, $method, array $oauth_params, array $key_params, array $endpoint_params) |
180
|
56 |
|
{ |
181
|
|
|
$oauth_params += [ |
182
|
56 |
|
'oauth_signature_method' => 'HMAC-SHA1', |
183
|
56 |
|
'oauth_timestamp' => time(), |
184
|
56 |
|
'oauth_version' => '1.0a', |
185
|
56 |
|
'oauth_nonce' => sha1(openssl_random_pseudo_bytes(32)), |
186
|
|
|
]; |
187
|
56 |
|
$base = $oauth_params + $endpoint_params; |
188
|
56 |
|
uksort($base, 'strnatcmp'); |
189
|
56 |
|
$oauth_params['oauth_signature'] = base64_encode(hash_hmac( |
190
|
56 |
|
'sha1', |
191
|
56 |
|
implode('&', array_map('rawurlencode', [ |
192
|
56 |
|
strtoupper($method), |
193
|
56 |
|
$url, |
194
|
56 |
|
http_build_query($base, '', '&', PHP_QUERY_RFC3986) |
195
|
|
|
])), |
196
|
56 |
|
implode('&', array_map('rawurlencode', $key_params)), |
197
|
56 |
|
true |
198
|
|
|
)); |
199
|
56 |
|
$tmp = []; |
200
|
56 |
|
foreach ($oauth_params as $key => $value) { |
201
|
56 |
|
$tmp[] = urlencode($key) . '="' . urlencode($value) . '"'; |
202
|
|
|
} |
203
|
56 |
|
return ['Authorization: OAuth ' . implode(', ', $tmp)]; |
204
|
|
|
} |
205
|
|
|
} |