root / trunk / web / dojo / dojox / io / OAuth.js @ 13
History | View | Annotate | Download (4.04 KB)
| 1 | 9 | andrej.cim | /*
|
|---|---|---|---|
| 2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
| 3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
| 4 | see: http://dojotoolkit.org/license for details
|
||
| 5 | */
|
||
| 6 | |||
| 7 | |||
| 8 | if(!dojo._hasResource["dojox.io.OAuth"]){ |
||
| 9 | dojo._hasResource["dojox.io.OAuth"]=true; |
||
| 10 | dojo.provide("dojox.io.OAuth");
|
||
| 11 | dojo.require("dojox.encoding.digests.SHA1");
|
||
| 12 | dojox.io.OAuth=new (function(){ |
||
| 13 | var _1=this.encode=function(s){ |
||
| 14 | if(!s){
|
||
| 15 | return ""; |
||
| 16 | } |
||
| 17 | return encodeURIComponent(s).replace(/\!/g,"%21").replace(/\*/g,"%2A").replace(/\'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29"); |
||
| 18 | }; |
||
| 19 | var _2=this.decode=function(_3){ |
||
| 20 | var a=[],_4=_3.split("&"); |
||
| 21 | for(var i=0,l=_4.length;i<l;i++){ |
||
| 22 | var _5=_4[i];
|
||
| 23 | if(_4[i]==""){ |
||
| 24 | continue;
|
||
| 25 | } |
||
| 26 | if(_4[i].indexOf("=")>-1){ |
||
| 27 | var _6=_4[i].split("="); |
||
| 28 | a.push([decodeURIComponent(_6[0]),decodeURIComponent(_6[1])]); |
||
| 29 | }else{
|
||
| 30 | a.push([decodeURIComponent(_4[i]),null]);
|
||
| 31 | } |
||
| 32 | } |
||
| 33 | return a;
|
||
| 34 | }; |
||
| 35 | function _7(_8){ |
||
| 36 | var _9=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],_a=/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,_b=_a.exec(_8),_c={},i=_9.length; |
||
| 37 | while(i--){
|
||
| 38 | _c[_9[i]]=_b[i]||"";
|
||
| 39 | } |
||
| 40 | var p=_c.protocol.toLowerCase(),a=_c.authority.toLowerCase(),b=(p=="http"&&_c.port==80)||(p=="https"&&_c.port==443); |
||
| 41 | if(b){
|
||
| 42 | if(a.lastIndexOf(":")>-1){ |
||
| 43 | a=a.substring(0,a.lastIndexOf(":")); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | var _d=_c.path||"/"; |
||
| 47 | _c.url=p+"://"+a+_d;
|
||
| 48 | return _c;
|
||
| 49 | }; |
||
| 50 | var _e="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; |
||
| 51 | function _f(_10){ |
||
| 52 | var s="",tl=_e.length; |
||
| 53 | for(var i=0;i<_10;i++){ |
||
| 54 | s+=_e.charAt(Math.floor(Math.random()*tl)); |
||
| 55 | } |
||
| 56 | return s;
|
||
| 57 | }; |
||
| 58 | function _11(){ |
||
| 59 | return Math.floor(new Date().valueOf()/1000)-2; |
||
| 60 | }; |
||
| 61 | function _12(_13,key,_14){ |
||
| 62 | if(_14&&_14!="PLAINTEXT"&&_14!="HMAC-SHA1"){ |
||
| 63 | throw new Error("dojox.io.OAuth: the only supported signature encodings are PLAINTEXT and HMAC-SHA1."); |
||
| 64 | } |
||
| 65 | if(_14=="PLAINTEXT"){ |
||
| 66 | return key;
|
||
| 67 | }else{
|
||
| 68 | return dojox.encoding.digests.SHA1._hmac(_13,key);
|
||
| 69 | } |
||
| 70 | }; |
||
| 71 | function key(_15){ |
||
| 72 | return _1(_15.consumer.secret)+"&"+(_15.token&&_15.token.secret?_1(_15.token.secret):""); |
||
| 73 | }; |
||
| 74 | function _16(_17,oaa){ |
||
| 75 | var o={oauth_consumer_key:oaa.consumer.key,oauth_nonce:_f(16),oauth_signature_method:oaa.sig_method||"HMAC-SHA1",oauth_timestamp:_11(),oauth_version:"1.0"}; |
||
| 76 | if(oaa.token){
|
||
| 77 | o.oauth_token=oaa.token.key; |
||
| 78 | } |
||
| 79 | _17.content=dojo.mixin(_17.content||{},o);
|
||
| 80 | }; |
||
| 81 | function _18(_19){ |
||
| 82 | var _1a=[{}],_1b;
|
||
| 83 | if(_19.form){
|
||
| 84 | if(!_19.content){
|
||
| 85 | _19.content={};
|
||
| 86 | } |
||
| 87 | var _1c=dojo.byId(_19.form);
|
||
| 88 | var _1d=_1c.getAttributeNode("action"); |
||
| 89 | _19.url=_19.url||(_1d?_1d.value:null);
|
||
| 90 | _1b=dojo.formToObject(_1c); |
||
| 91 | delete _19.form;
|
||
| 92 | } |
||
| 93 | if(_1b){
|
||
| 94 | _1a.push(_1b); |
||
| 95 | } |
||
| 96 | if(_19.content){
|
||
| 97 | _1a.push(_19.content); |
||
| 98 | } |
||
| 99 | var map=_7(_19.url);
|
||
| 100 | if(map.query){
|
||
| 101 | var tmp=dojo.queryToObject(map.query);
|
||
| 102 | for(var p in tmp){ |
||
| 103 | tmp[p]=encodeURIComponent(tmp[p]); |
||
| 104 | } |
||
| 105 | _1a.push(tmp); |
||
| 106 | } |
||
| 107 | _19._url=map.url; |
||
| 108 | var a=[];
|
||
| 109 | for(var i=0,l=_1a.length;i<l;i++){ |
||
| 110 | var _1e=_1a[i];
|
||
| 111 | for(var p in _1e){ |
||
| 112 | if(dojo.isArray(_1e[p])){
|
||
| 113 | for(var j=0,jl=_1e.length;j<jl;j++){ |
||
| 114 | a.push([p,_1e[j]]); |
||
| 115 | } |
||
| 116 | }else{
|
||
| 117 | a.push([p,_1e[p]]); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | _19._parameters=a; |
||
| 122 | return _19;
|
||
| 123 | }; |
||
| 124 | function _1f(_20,_21,oaa){ |
||
| 125 | _16(_21,oaa); |
||
| 126 | _18(_21); |
||
| 127 | var a=_21._parameters;
|
||
| 128 | a.sort(function(a,b){
|
||
| 129 | if(a[0]>b[0]){ |
||
| 130 | return 1; |
||
| 131 | } |
||
| 132 | if(a[0]<b[0]){ |
||
| 133 | return -1; |
||
| 134 | } |
||
| 135 | if(a[1]>b[1]){ |
||
| 136 | return 1; |
||
| 137 | } |
||
| 138 | if(a[1]<b[1]){ |
||
| 139 | return -1; |
||
| 140 | } |
||
| 141 | return 0; |
||
| 142 | }); |
||
| 143 | var s=dojo.map(a,function(_22){ |
||
| 144 | return _1(_22[0])+"="+_1(_22[1]||""); |
||
| 145 | }).join("&");
|
||
| 146 | var _23=_20.toUpperCase()+"&"+_1(_21._url)+"&"+_1(s); |
||
| 147 | return _23;
|
||
| 148 | }; |
||
| 149 | function _24(_25,_26,oaa){ |
||
| 150 | var k=key(oaa),_27=_1f(_25,_26,oaa),s=_12(_27,k,oaa.sig_method||"HMAC-SHA1"); |
||
| 151 | _26.content["oauth_signature"]=s;
|
||
| 152 | return _26;
|
||
| 153 | }; |
||
| 154 | this.sign=function(_28,_29,oaa){ |
||
| 155 | return _24(_28,_29,oaa);
|
||
| 156 | }; |
||
| 157 | this.xhr=function(_2a,_2b,oaa,_2c){ |
||
| 158 | _24(_2a,_2b,oaa); |
||
| 159 | return dojo.xhr(_2a,_2b,_2c);
|
||
| 160 | }; |
||
| 161 | this.xhrGet=function(_2d,oaa){ |
||
| 162 | return this.xhr("GET",_2d,oaa); |
||
| 163 | }; |
||
| 164 | this.xhrPost=this.xhrRawPost=function(_2e,oaa){ |
||
| 165 | return this.xhr("POST",_2e,oaa,true); |
||
| 166 | }; |
||
| 167 | this.xhrPut=this.xhrRawPut=function(_2f,oaa){ |
||
| 168 | return this.xhr("PUT",_2f,oaa,true); |
||
| 169 | }; |
||
| 170 | this.xhrDelete=function(_30,oaa){ |
||
| 171 | return this.xhr("DELETE",_30,oaa); |
||
| 172 | }; |
||
| 173 | })(); |
||
| 174 | } |