/
OauthTest.php
210 lines (194 loc) · 5.65 KB
/
OauthTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Network\Http\Auth;
use Cake\Network\Http\Auth\Oauth;
use Cake\Network\Http\Request;
use Cake\TestSuite\TestCase;
/**
* Oauth test.
*/
class OauthTest extends TestCase {
/**
* @expectedException Cake\Error\Exception
*/
public function testExceptionUnknownSigningMethod() {
$auth = new Oauth();
$creds = [
'consumerSecret' => 'it is secret',
'consumerKey' => 'a key',
'token' => 'a token value',
'tokenSecret' => 'also secret',
'method' => 'silly goose',
];
$request = new Request();
$auth->authentication($request, $creds);
}
/**
* Test plain-text signing.
*
* @return void
*/
public function testPlainTextSigning() {
$auth = new Oauth();
$creds = [
'consumerSecret' => 'it is secret',
'consumerKey' => 'a key',
'token' => 'a token value',
'tokenSecret' => 'also secret',
'method' => 'plaintext',
];
$request = new Request();
$auth->authentication($request, $creds);
$result = $request->header('Authorization');
$this->assertContains('OAuth', $result);
$this->assertContains('oauth_version="1.0"', $result);
$this->assertContains('oauth_token="a%20token%20value"', $result);
$this->assertContains('oauth_consumer_key="a%20key"', $result);
$this->assertContains('oauth_signature_method="PLAINTEXT"', $result);
$this->assertContains('oauth_signature="it%20is%20secret%26also%20secret"', $result);
$this->assertContains('oauth_timestamp=', $result);
$this->assertContains('oauth_nonce=', $result);
}
/**
* Test that baseString() normalizes the URL.
*
* @return void
*/
public function testBaseStringNormalizeUrl() {
$request = new Request();
$request->url('HTTP://exAmple.com:80/parts/foo');
$auth = new Oauth();
$creds = [];
$result = $auth->baseString($request, $creds);
$this->assertContains('GET&', $result, 'method was missing.');
$this->assertContains('http%3A%2F%2Fexample.com%2Fparts%2Ffoo', $result);
}
/**
* Test that the query string is stripped from the normalized host.
*
* @return void
*/
public function testBaseStringWithQueryString() {
$request = new Request();
$request->url('http://example.com/search?q=pogo&cat=2');
$auth = new Oauth();
$values = [
'oauth_version' => '1.0',
'oauth_nonce' => uniqid(),
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => 'token',
'oauth_consumer_key' => 'consumer-key',
];
$result = $auth->baseString($request, $values);
$this->assertContains('GET&', $result, 'method was missing.');
$this->assertContains(
'http%3A%2F%2Fexample.com%2Fsearch&',
$result
);
$this->assertContains(
'cat%3D2%26oauth_consumer_key%3Dconsumer-key' .
'%26oauth_nonce%3D' . $values['oauth_nonce'] .
'%26oauth_signature_method%3DHMAC-SHA1' .
'%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
'%26oauth_token%3Dtoken' .
'%26oauth_version%3D1.0' .
'%26q%3Dpogo',
$result
);
}
/**
* Ensure that post data is sorted and encoded.
*
* Keys with array values have to be serialized using
* a more standard HTTP approach. PHP flavoured HTTP
* is not part of the Oauth spec.
*
* See Normalize Request Parameters (section 9.1.1)
* http://wiki.oauth.net/w/page/12238556/TestCases
*
* @return void
*/
public function testBaseStringWithPostData() {
$request = new Request();
$request->url('http://example.com/search?q=pogo')
->method(Request::METHOD_POST)
->body([
'address' => 'post',
'tags' => ['oauth', 'cake'],
'zed' => 'last'
]);
$auth = new Oauth();
$values = [
'oauth_version' => '1.0',
'oauth_nonce' => uniqid(),
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => 'token',
'oauth_consumer_key' => 'consumer-key',
];
$result = $auth->baseString($request, $values);
$this->assertContains('POST&', $result, 'method was missing.');
$this->assertContains(
'http%3A%2F%2Fexample.com%2Fsearch&',
$result
);
$this->assertContains(
'&address%3Dpost' .
'%26oauth_consumer_key%3Dconsumer-key' .
'%26oauth_nonce%3D' . $values['oauth_nonce'] .
'%26oauth_signature_method%3DHMAC-SHA1' .
'%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
'%26oauth_token%3Dtoken' .
'%26oauth_version%3D1.0' .
'%26q%3Dpogo' .
'%26tags%3Dcake' .
'%26tags%3Doauth' .
'%26zed%3Dlast',
$result
);
}
/**
* Test HMAC-SHA1 signing
*
* Hash result + parameters taken from
* http://wiki.oauth.net/w/page/12238556/TestCases
*
* @return void
*/
public function testHmacSigning() {
$request = new Request();
$request->url('http://photos.example.net/photos')
->body([
'file' => 'vacation.jpg',
'size' => 'original'
]);
$options = [
'consumerKey' => 'dpf43f3p2l4k3l03',
'consumerSecret' => 'kd94hf93k423kf44',
'tokenSecret' => 'pfkkdhi9sl3r4s00',
'token' => 'nnch734d00sl2jdk',
'nonce' => 'kllo9940pd9333jh',
'timestamp' => '1191242096'
];
$auth = new Oauth();
$auth->authentication($request, $options);
$result = $request->header('Authorization');
$expected = 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=';
$this->assertContains(
'oauth_signature="' . $expected . '"',
urldecode($result)
);
}
}