-
Notifications
You must be signed in to change notification settings - Fork 16
/
oauthTest.js
184 lines (172 loc) · 7.81 KB
/
oauthTest.js
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
/*
* Copyright 2008 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Here are some unit tests for the software in oauth.js.
// The test data were copied from http://oauth.pbwiki.com/TestCases
function testOAuth() {
testEncode();
testGetParameters();
testGetBaseString();
testGetSignature();
}
var ENCODING // From http://wiki.oauth.net/TestCases
= [ ["abcABC123", "abcABC123"]
, ["-._~" , "-._~"]
, ["%" , "%25"]
, ["+" , "%2B"]
, ["&=*" , "%26%3D%2A"]
, ["!'()" , "%21%27%28%29"]
, ["\n" , "%0A"]
, [" " , "%20"]
];
function testEncode() {
for (var i = 0; i < ENCODING.length; ++i) {
var input = ENCODING[i][0];
var expected = ENCODING[i][1];
var actual = OAuth.percentEncode(input);
if (expected != actual) {
alert("OAuth.percentEncode(" + input + ") = " + actual);
}
}
}
function testGetParameters() {
var list = OAuth.getParameterList(null);
if (list == null || !(list instanceof Array) || list.length != 0) {
alert("getParameterList(null) = " + list);
}
list = OAuth.getParameterList('');
if (list == null || !(list instanceof Array) || list.length != 0) {
alert("getParameterList('') = " + list);
}
var map = OAuth.getParameterMap(null);
if (map == null || (map instanceof Array) || typeof map != "object") {
alert("getParameterMap(null) = " + map);
}
var actual = OAuth.getParameter({x: 'a', y: 'b'}, 'x');
if (actual != 'a') {
alert("getParameter({}, 'x') = " + actual);
}
actual = OAuth.getParameter([['x', 'a'], ['y', 'b'], ['x', 'c']], 'x');
if (actual != 'a') {
alert("getParameter([], 'x') = " + actual);
}
var expected = 'OAuth realm="R",oauth_token="T",oauth_w%40%21rd="%23%40%2A%21"';
actual = OAuth.getAuthorizationHeader('R', [['a', 'b'], ['oauth_token', 'T'], ['oauth_w@!rd', '#@*!']]);
if (actual == null || actual != expected) {
alert("getAuthorizationHeader\n" + expected + " != \n" + actual);
}
actual = OAuth.getAuthorizationHeader('R', {a: 'b', oauth_token: 'T', 'oauth_w@!rd': '#@*!'});
if (actual == null || actual != expected) {
alert("getAuthorizationHeader\n" + expected + " != \n" + actual);
}
var message = {action: 'http://localhost', parameters: {}};
OAuth.completeRequest(message, {consumerKey: 'CK', token: 'T'});
assertMemberEquals(message, 'method', "GET");
map = message.parameters;
assertMemberEquals(map, 'oauth_consumer_key', 'CK');
assertMemberEquals(map, 'oauth_token', 'T');
assertMemberEquals(map, 'oauth_version', '1.0');
assertMemberNotNull(map, 'oauth_timestamp');
assertMemberNotNull(map, 'oauth_nonce');
}
function assertMemberEquals(map, name, expected) {
var actual = map[name];
if (actual != expected) {
alert(name + '=' + actual + ' (not ' + expected + ')');
}
}
function assertMemberNotNull(map, name) {
var actual = map[name];
if (!actual) {
alert(name + '=' + actual);
}
}
var OAUTH_A_BASE_STRING = "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&"
+ "file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal";
var BASES = //
// label, HTTP method, action, parameters, expected
{ "simple" : ["GET", "http://example.com/", {n: "v"}, "GET&http%3A%2F%2Fexample.com%2F&n%3Dv" ]
, "no path" : ["GET", "http://example.com" , {n: "v"}, "GET&http%3A%2F%2Fexample.com%2F&n%3Dv" ]
, "sorting" : ["GET", "http://example.com/", [["n", "AB"], ["n", "{}"]], "GET&http%3A%2F%2Fexample.com%2F&n%3D%257B%257D%26n%3DAB" ]
// , "username in URL": ["GET", "http://U:[email protected]" , null, "GET&http%3A%2F%2FU%3APW%40example.com%2F&" ]
, "capitalized URL": ["GET", "HtTp://Example.CoM/A/b/C" , null, "GET&http%3A%2F%2Fexample.com%2FA%2Fb%2FC&" ]
, "@ in URL" : ["GET", "http://example.com/A@B/C" , null, "GET&http%3A%2F%2Fexample.com%2FA%40B%2FC&" ]
, "punctuated URL" : ["GET", "http://example.com/a;b,c#d", null, "GET&http%3A%2F%2Fexample.com%2Fa%3Bb%2Cc&" ]
, "OAuth A request": ["POST", "https://photos.example.net/request_token",
{ oauth_version: "1.0", oauth_consumer_key: "dpf43f3p2l4k3l03"
, oauth_timestamp: "1191242090", oauth_nonce: "hsu94j3884jdopsl"
, oauth_signature_method: "PLAINTEXT", oauth_signature: "ignored"
}
, "POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&"
+ "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0" ]
, "OAuth A access" : ["GET", "http://photos.example.net/photos",
{ file: "vacation.jpg", size: "original"
, oauth_version: "1.0", oauth_consumer_key: "dpf43f3p2l4k3l03", oauth_token: "nnch734d00sl2jdk"
, oauth_timestamp: "1191242096", oauth_nonce: "kllo9940pd9333jh"
, oauth_signature: "ignored", oauth_signature_method: "HMAC-SHA1"
}
, OAUTH_A_BASE_STRING ]
};
function testGetBaseString() {
for (var label in BASES) {
try {
var base = BASES[label];
var b = 0;
var method = base[b++];
var action = base[b++];
var parameters = base[b++];
var expected = base[b++];
var actual = OAuth.SignatureMethod.getBaseString({method: method, action: action, parameters: parameters});
if (expected != actual) {
alert(label + "\n" + actual + " (actual)\n" + expected + " (expected)");
}
} catch(e) {
alert(e);
}
}
// alert("tested OAuth.SignatureMethod.getBaseString");
}
var SIGNATURES =
// label, method, consumer secret, token secret, base string, expected
{ "HMAC-SHA1.a" : [ "HMAC-SHA1", "cs", null, "bs", "egQqG5AJep5sJ7anhXju1unge2I=" ]
, "HMAC-SHA1.b" : [ "HMAC-SHA1", "cs", "ts", "bs", "VZVjXceV7JgPq/dOTnNmEfO0Fv8=" ]
, "OAuth A access" : [ "HMAC-SHA1", "kd94hf93k423kf44",
"pfkkdhi9sl3r4s00", OAUTH_A_BASE_STRING,
"tR3+Ty81lMeYAr/Fid0kMTYa/WM=" ]
, "PLAINTEXT" : [ "PLAINTEXT", "cs", "ts", "bs", "cs&ts" ]
, "OAuth A request": [ "PLAINTEXT", "kd94hf93k423kf44", null, null, "kd94hf93k423kf44&" ]
};
function testGetSignature() {
for (label in SIGNATURES) {
try {
var signature = SIGNATURES[label];
var s = 0;
var methodName = signature[s++];
var consumerSecret = signature[s++];
var tokenSecret = signature[s++];
var baseString = signature[s++];
var expected = signature[s++];
var signer = OAuth.SignatureMethod.newMethod(methodName,
{consumerSecret: consumerSecret, tokenSecret: tokenSecret});
var actual = signer.getSignature(baseString);
if (expected != actual) {
alert(label + "\n" + actual + " (actual)\n" + expected + " (expected)");
}
} catch(e) {
alert(label + ": " + e);
}
}
// alert("tested OAuth.SignatureMethod.getSignature");
}