| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/array", |
| 5 |
"dojo/_base/xhr", |
| 6 |
"dojo/dom", |
| 7 |
"dojox/encoding/digests/SHA1" |
| 8 |
], function(dojo, lang, array, xhr, dom, SHA1){ |
| 9 |
dojo.getObject("io.OAuth", true, dojox); |
| 10 |
|
| 11 |
dojox.io.OAuth = new (function(){ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
var encode = this.encode = function(s){ |
| 30 |
if(!("" + s).length){ return ""; } |
| 31 |
return encodeURIComponent(s) |
| 32 |
.replace(/\!/g, "%21") |
| 33 |
.replace(/\*/g, "%2A") |
| 34 |
.replace(/\'/g, "%27") |
| 35 |
.replace(/\(/g, "%28") |
| 36 |
.replace(/\)/g, "%29"); |
| 37 |
}; |
| 38 |
|
| 39 |
var decode = this.decode = function(str){ |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
var a=[], list=str.split("&"); |
| 44 |
for(var i=0, l=list.length; i<l; i++){ |
| 45 |
var item=list[i]; |
| 46 |
if(list[i]==""){ continue; } |
| 47 |
if(list[i].indexOf("=")>-1){ |
| 48 |
var tmp=list[i].split("="); |
| 49 |
a.push([ decodeURIComponent(tmp[0]), decodeURIComponent(tmp[1]) ]); |
| 50 |
} else { |
| 51 |
a.push([ decodeURIComponent(list[i]), null ]); |
| 52 |
} |
| 53 |
} |
| 54 |
return a; |
| 55 |
}; |
| 56 |
|
| 57 |
function parseUrl(url){ |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
var keys = [ |
| 62 |
"source","protocol","authority","userInfo", |
| 63 |
"user","password","host","port", |
| 64 |
"relative","path","directory", |
| 65 |
"file","query","anchor" |
| 66 |
], |
| 67 |
parser=/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, |
| 68 |
match=parser.exec(url), |
| 69 |
map = {}, |
| 70 |
i=keys.length; |
| 71 |
|
| 72 |
|
| 73 |
while(i--){ map[keys[i]] = match[i] || ""; } |
| 74 |
|
| 75 |
|
| 76 |
var p=map.protocol.toLowerCase(), |
| 77 |
a=map.authority.toLowerCase(), |
| 78 |
b=(p=="http"&&map.port==80)||(p=="https"&&map.port==443); |
| 79 |
if(b){ |
| 80 |
if(a.lastIndexOf(":")>-1){ |
| 81 |
a=a.substring(0, a.lastIndexOf(":")); |
| 82 |
} |
| 83 |
} |
| 84 |
var path=map.path||"/"; |
| 85 |
map.url=p+"://"+a+path; |
| 86 |
|
| 87 |
|
| 88 |
return map; |
| 89 |
} |
| 90 |
|
| 91 |
var tab="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; |
| 92 |
function nonce(length){ |
| 93 |
var s="", tl=tab.length; |
| 94 |
for(var i=0; i<length; i++){ |
| 95 |
s+=tab.charAt(Math.floor(Math.random()*tl)); |
| 96 |
} |
| 97 |
return s; |
| 98 |
} |
| 99 |
function timestamp(){ |
| 100 |
return Math.floor(new Date().valueOf()/1000)-2; |
| 101 |
} |
| 102 |
function signature(data, key, type){ |
| 103 |
if(type && type!="PLAINTEXT" && type!="HMAC-SHA1"){ |
| 104 |
throw new Error("dojox.io.OAuth: the only supported signature encodings are PLAINTEXT and HMAC-SHA1."); |
| 105 |
} |
| 106 |
|
| 107 |
if(type=="PLAINTEXT"){ |
| 108 |
return key; |
| 109 |
} else { |
| 110 |
|
| 111 |
return SHA1._hmac(data, key); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
function key(args){ |
| 116 |
|
| 117 |
|
| 118 |
return encode(args.consumer.secret) |
| 119 |
+ "&" |
| 120 |
+ (args.token && args.token.secret ? encode(args.token.secret) : ""); |
| 121 |
} |
| 122 |
|
| 123 |
function addOAuth(args, oaa){ |
| 124 |
|
| 125 |
|
| 126 |
var o = { |
| 127 |
oauth_consumer_key: oaa.consumer.key, |
| 128 |
oauth_nonce: nonce(16), |
| 129 |
oauth_signature_method: oaa.sig_method || "HMAC-SHA1", |
| 130 |
oauth_timestamp: timestamp(), |
| 131 |
oauth_version: "1.0" |
| 132 |
} |
| 133 |
if(oaa.token){ |
| 134 |
o.oauth_token = oaa.token.key; |
| 135 |
} |
| 136 |
args.content = dojo.mixin(args.content||{}, o); |
| 137 |
} |
| 138 |
|
| 139 |
function convertArgs(args){ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
var miArgs = [{}], formObject; |
| 146 |
|
| 147 |
if(args.form){ |
| 148 |
if(!args.content){ args.content = {}; } |
| 149 |
var form = dojo.byId(args.form); |
| 150 |
var actnNode = form.getAttributeNode("action"); |
| 151 |
args.url = args.url || (actnNode ? actnNode.value : null); |
| 152 |
formObject = dojo.formToObject(form); |
| 153 |
delete args.form; |
| 154 |
} |
| 155 |
if(formObject){ miArgs.push(formObject); } |
| 156 |
if(args.content){ miArgs.push(args.content); } |
| 157 |
|
| 158 |
|
| 159 |
var map = parseUrl(args.url); |
| 160 |
if(map.query){ |
| 161 |
var tmp = dojo.queryToObject(map.query); |
| 162 |
|
| 163 |
for(var p in tmp){ tmp[p] = encodeURIComponent(tmp[p]); } |
| 164 |
miArgs.push(tmp); |
| 165 |
} |
| 166 |
args._url = map.url; |
| 167 |
|
| 168 |
|
| 169 |
var a = []; |
| 170 |
for(var i=0, l=miArgs.length; i<l; i++){ |
| 171 |
var item=miArgs[i]; |
| 172 |
for(var p in item){ |
| 173 |
if(dojo.isArray(item[p])){ |
| 174 |
|
| 175 |
for(var j=0, jl=item.length; j<jl; j++){ |
| 176 |
a.push([ p, item[j] ]); |
| 177 |
} |
| 178 |
} else { |
| 179 |
a.push([ p, item[p] ]); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
args._parameters = a; |
| 185 |
return args; |
| 186 |
} |
| 187 |
|
| 188 |
function baseString(method, args, oaa){ |
| 189 |
|
| 190 |
addOAuth(args, oaa); |
| 191 |
convertArgs(args); |
| 192 |
|
| 193 |
var a = args._parameters; |
| 194 |
|
| 195 |
|
| 196 |
a.sort(function(a,b){ |
| 197 |
if(a[0]>b[0]){ return 1; } |
| 198 |
if(a[0]<b[0]){ return -1; } |
| 199 |
if(a[1]>b[1]){ return 1; } |
| 200 |
if(a[1]<b[1]){ return -1; } |
| 201 |
return 0; |
| 202 |
}); |
| 203 |
|
| 204 |
|
| 205 |
var s = dojo.map(a, function(item){ |
| 206 |
return encode(item[0]) + "=" + encode((""+item[1]).length ? item[1] : ""); |
| 207 |
}).join("&"); |
| 208 |
|
| 209 |
var baseString = method.toUpperCase() |
| 210 |
+ "&" + encode(args._url) |
| 211 |
+ "&" + encode(s); |
| 212 |
return baseString; |
| 213 |
} |
| 214 |
|
| 215 |
function sign(method, args, oaa){ |
| 216 |
|
| 217 |
var k = key(oaa), |
| 218 |
message = baseString(method, args, oaa), |
| 219 |
s = signature(message, k, oaa.sig_method || "HMAC-SHA1"); |
| 220 |
args.content["oauth_signature"] = s; |
| 221 |
return args; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
this.sign = function(method, args, oaa){ |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
return sign(method, args, oaa); |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
this.xhr = function(method, args, oaa, hasBody){ |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
sign(method, args, oaa); |
| 283 |
return xhr(method, args, hasBody); |
| 284 |
}; |
| 285 |
|
| 286 |
this.xhrGet = function(args, oaa){ |
| 287 |
return this.xhr("GET", args, oaa); |
| 288 |
}; |
| 289 |
this.xhrPost = this.xhrRawPost = function(args, oaa){ |
| 290 |
return this.xhr("POST", args, oaa, true); |
| 291 |
}; |
| 292 |
this.xhrPut = this.xhrRawPut = function(args, oaa){ |
| 293 |
return this.xhr("PUT", args, oaa, true); |
| 294 |
}; |
| 295 |
this.xhrDelete = function(args, oaa){ |
| 296 |
return this.xhr("DELETE", args, oaa); |
| 297 |
}; |
| 298 |
})(); |
| 299 |
|
| 300 |
return dojox.io.OAuth; |
| 301 |
|
| 302 |
}); |