-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsoauth.js
184 lines (142 loc) · 5.85 KB
/
jsoauth.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
// I've found this script easier to debug with than the netflix js lib (http://oauth.googlecode.com/svn/code/javascript/oauth.js), which wasn't working out of the box for me
// License: anything not credited to someone else is licensed under Yahoo! BSD: http://gist.github.com/375593
// Source: http://github.com/erikeldridge/jsoauth
// Dependencies:
// - phpjs' parse_url http://phpjs.org/functions/parse_url:485
var jsoauth = (function() {
// @credit oauth js lib: http://code.google.com/p/oauth/source/browse/#svn/code/javascript
var percentEncode = function(str) {
if (str == null) {
return "";
}
str = encodeURIComponent(str);
// Now replace the values which urlencodeComponent doesn't do
// urlencodeComponent ignores: - _ . ! ~ * ' ( )
// OAuth dictates the only ones you can ignore are: - _ . ~
// Source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:urlencodeComponent
str = str.replace("!", "%21", "g");
str = str.replace("*", "%2A", "g");
str = str.replace("'", "%27", "g");
str = str.replace("(", "%28", "g");
str = str.replace(")", "%29", "g");
return str;
},
// @ref http://oauth.net/core/1.0/#rfc.section.9.1.2
constructReqURL = function(inputURL) {
var parsedURL;
try {
// parse_url http://phpjs.org/functions/parse_url:485
parsedURL = parse_url(inputURL);
return parsedURL['scheme'] + '://' + parsedURL['host'] + '/' + parsedURL['path'];
} catch(e) {
//incorrectly formatted input url
if (alert) {
alert(e);
}
}
},
// @pre params already collected & sorted
// @ref http://oauth.net/core/1.0/#rfc.section.9.1.1
concatenateParams = function(params) {
return params.join("&");
},
// @ref http://oauth.net/core/1.0/#rfc.section.9.1.1
// @ref http://oauth.net/core/1.0/#rfc.section.A.5.1
normalizeReqParams = function(params) {
params = params.sort();
params = concatenateParams(params);
params = percentEncode(params);
return params;
},
// @ref http://oauth.net/core/1.0/#rfc.section.9.1.3
concatenateReqElems = function(args) {
return args.reqMethod + '&' + args.reqURL + '&' + args.params;
},
// @credit http://kentbrewster.com/oauth-baby-steps/
// nonce is just a unique string
createNonce = function() {
var nonce = '';
for (var i = 0; i < 10; i++) {
nonce += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
return nonce;
},
// @ref http://oauth.net/core/1.0/#rfc.section.9.2
generateSig = function(args) {
var text = args.baseStr;
var key = percentEncode(args.secret) + '&' + percentEncode(args.tokenSecret);
var raw_signature = Crypto.HMAC(Crypto.SHA1, text, key, {
asBytes: true
});
var sig = Crypto.util.bytesToBase64(raw_signature);
return sig;
};
function toHeaderString(params) {
var pairs = [];
for (var i = 0; i < params.length; i++) {
var pair = params[i].split('=');
pairs.push(pair[0] + '="' + pair[1] + '"');
}
return pairs.join(',');
}
return {
consumerKey: '',
consumerSecret: '',
reqMethod: 'GET',
tokenSecret: '',
//must be uppercase @ref http://oauth.net/core/1.0/#rfc.section.9.1.3
sigMethod: 'HMAC-SHA1',
oauthVersion: '1.0',
callbackURL: '',
//handy for debugging
params: null,
normalReqParams: null,
baseStr: null,
signature: null,
//utils
toHeaderString: toHeaderString,
sign: function(args) {
var timestamp = Math.floor(new Date().getTime() / 1000);
// flush so multiple calls to obj don't conflict
this.normalReqParams = null;
this.baseStr = null;
this.signature = null;
this.params = [];
// basic oauth params formatted as strings in array so we can sort easily
this.params.push('oauth_consumer_key=' + this.consumerKey);
this.params.push('oauth_nonce=' + createNonce());
this.params.push('oauth_signature_method=' + this.sigMethod);
this.params.push('oauth_timestamp=' + timestamp);
this.params.push('oauth_version=' + this.oauthVersion);
// if params passed in (as array of 'key=val' strings), add them
if (args.params) {
for (var i = 0; i < args.params.length; i++) {
this.params.push(args.params[i]);
}
}
// elems for base str
this.normalReqParams = normalizeReqParams(this.params);
reqURL = constructReqURL(args.URL);
// create base str
this.baseStr = concatenateReqElems({
'reqMethod': this.reqMethod,
'reqURL': percentEncode(reqURL),
'params': this.normalReqParams
});
this.signature = generateSig({
'baseStr': this.baseStr,
'secret': this.consumerSecret,
'tokenSecret': this.tokenSecret
});
this.params.push('oauth_signature=' + percentEncode(this.signature));
//maintain correct alpha sort - very important as it affects the signature
//@ref http://oauth.net/core/1.0/#rfc.section.9.1.1
//@ref http://oauth.net/core/1.0/#9.2.1
var signedURL = args.URL + '?' + concatenateParams(this.params.sort());
return {
queryString: concatenateParams(this.params),
headerString: toHeaderString(this.params)
};
}
};
} ());