openstreetmap-website/vendor/assets/ohauth/ohauth.js
2013-03-28 00:34:09 +00:00

86 lines
2.6 KiB
JavaScript

(function(context) {
var ohauth = {};
ohauth.qsString = function(obj) {
return Object.keys(obj).sort().map(function(key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(obj[key]);
}).join('&');
};
ohauth.sha = sha1();
ohauth.stringQs = function(str) {
return str.split('&').reduce(function(obj, pair){
var parts = pair.split('=');
obj[parts[0]] = (null === parts[1]) ?
'' : decodeURIComponent(parts[1]);
return obj;
}, {});
};
ohauth.rawxhr = function(method, url, data, headers, callback) {
var xhr = new XMLHttpRequest(), twoHundred = /^20\d$/;
xhr.onreadystatechange = function() {
if (4 == xhr.readyState && 0 !== xhr.status) {
if (twoHundred.test(xhr.status)) callback(null, xhr);
else return callback(xhr, null);
}
};
xhr.onerror = function(e) { return callback(e, null); };
xhr.open(method, url, true);
for (var h in headers) xhr.setRequestHeader(h, headers[h]);
xhr.send(data);
};
ohauth.xhr = function(method, url, auth, data, options, callback) {
var headers = (options && options.header) || {
'Content-Type': 'application/x-www-form-urlencoded'
};
headers.Authorization = 'OAuth ' + ohauth.authHeader(auth);
ohauth.rawxhr(method, url, auth, data, headers, callback);
};
ohauth.nonce = function() {
for (var o = ''; o.length < 6;) {
o += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'[Math.floor(Math.random() * 61)];
}
return o;
};
ohauth.authHeader = function(obj) {
return Object.keys(obj).sort().map(function(key) {
return encodeURIComponent(key) + '="' + encodeURIComponent(obj[key]) + '"';
}).join(', ');
};
ohauth.timestamp = function() { return ~~((+new Date()) / 1000); };
ohauth.percentEncode = function(s) {
return encodeURIComponent(s)
.replace(/\!/g, '%21').replace(/\'/g, '%27')
.replace(/\*/g, '%2A').replace(/\(/g, '%28').replace(/\)/g, '%29');
};
ohauth.baseString = function(method, url, params) {
if (params.oauth_signature) delete params.oauth_signature;
return [
method,
ohauth.percentEncode(url),
ohauth.percentEncode(ohauth.qsString(params))].join('&');
};
ohauth.signature = function(oauth_secret, token_secret, baseString) {
return ohauth.sha.b64_hmac_sha1(
ohauth.percentEncode(oauth_secret) + '&' +
ohauth.percentEncode(token_secret),
baseString);
};
context.ohauth = ohauth;
// export for npm/browserify compatibility
if (typeof module !== 'undefined') module.exports = ohauth;
})(this);