(function(t,e){"function"==typeof define&&define.amd?define(["underscore"],e):t.SimpleOAuth=e(t._)})(this,function(t){function e(t){for(var e,t=t||16,r="",n=0,s=i.length;t>n;n++)e=Math.floor(Math.random()*s),r+=i.substring(e,e+1);return r}function r(){var t=new Date;return""+Math.floor(t.getTime()/1e3)}function n(t,e,r,n){function s(t,e,r,n){return 20>t?e&r|~e&n:40>t?e^r^n:60>t?e&r|e&n|r&n:e^r^n}function i(t){return 20>t?1518500249:40>t?1859775393:60>t?-1894007588:-899497514}function u(t,e){var r=(65535&t)+(65535&e),n=(t>>16)+(e>>16)+(r>>16);return n<<16|65535&r}function o(t,e){return t<>>32-e}function a(t,e){t[e>>5]|=128<<24-e%32,t[(e+64>>9<<4)+15]=e;for(var r=[80],n=1732584193,a=-271733879,h=-1732584194,c=271733878,p=-1009589776,f=0;t.length>f;f+=16){for(var m=n,l=a,d=h,g=c,_=p,b=0;80>b;b++){r[b]=16>b?t[f+b]:o(r[b-3]^r[b-8]^r[b-14]^r[b-16],1);var v=u(u(o(n,5),s(b,a,h,c)),u(u(p,r[b]),i(b)));p=c,c=h,h=o(a,30),a=n,n=v}n=u(n,m),a=u(a,l),h=u(h,d),c=u(c,g),p=u(p,_)}return[n,a,h,c,p]}function h(t){for(var e=[],r=(1<s;s+=n)e[s>>5]|=(t.charCodeAt(s/8)&r)<<32-n-s%32;return e}function c(t,e){var r=h(t);r.length>16&&(r=a(r,t.length*n));for(var s=[16],i=[16],u=0;16>u;u++)s[u]=909522486^r[u],i[u]=1549556828^r[u];var o=a(s.concat(h(e)),512+e.length*n);return a(i.concat(o),672)}function p(t){for(var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n="",s=0;4*t.length>s;s+=3)for(var i=(255&t[s>>2]>>8*(3-s%4))<<16|(255&t[s+1>>2]>>8*(3-(s+1)%4))<<8|255&t[s+2>>2]>>8*(3-(s+2)%4),u=0;4>u;u++)n+=8*s+6*u>32*t.length?r:e.charAt(63&i>>6*(3-u));return n}function f(t,e){return p(c(t,e))}return r||(r="="),n||(n=8),f(t,e)}var s=["callback","consumer_key","nonce","signature_method","timestamp","token","verifier","version"],i="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";SimpleOAuth={};var u=function(e,r,n,s){var s=s||{};this.method=e.toUpperCase(),this.uri=URI.parseUri(r),this.uri.fragment="",this.uri.normalize(),this.params=n,this.options=t.extend(u.default_options(),s)};u.default_options=function(){return{nonce:e(),signature_method:"HMAC-SHA1",timestamp:r(),version:"1.0"}},u.escape=function(t){return encodeURIComponent(t).replace(/\!/g,"%21").replace(/\*/g,"%2A").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29")},u.unescape=function(t){return decodeURIComponent(t)},t.extend(u.prototype,{url:function(){var e=new o(t.clone(this.uri));return e.query=null,e.build()},build:function(t){var e,t=t||"header";return"header"==t?e="OAuth "+this.normalized_header_attributes():"query"==t&&(e=this.normalized_query_attributes()),e},signed_attributes:function(){var e=t.clone(this.attributes());return e.oauth_signature=this.signature(),e},normalized_header_attributes:function(){return t.map(t.sortBy(t.pairs(this.signed_attributes()),function(t){return t[0]}),function(t){return t[0]+'="'+u.escape(t[1])+'"'}).join(", ")},normalized_query_attributes:function(){return t.map(t.sortBy(t.pairs(this.signed_attributes()),function(t){return t[0]}),function(t){return t[0]+"="+u.escape(t[1])}).join("&")},attributes:function(){var e={},r=this.options;return t.each(s,function(t){r[t]&&(e["oauth_"+t]=r[t])}),e},signature:function(){return this.hmac_sha1_signature()},hmac_sha1_signature:function(){return n(this.secret(),this.signature_base())},secret:function(){var e=t.pick(this.options,"consumer_secret","token_secret");return e.consumer_secret=e.consumer_secret||"",e.token_secret=e.token_secret||"",t.map(e,function(t){return u.escape(t)}).join("&")},signature_base:function(){return t.map([this.method,this.url(),this.normalized_params()],function(t){return u.escape(t)}).join("&")},normalized_params:function(){return t.map(t.map(this.signature_params(),function(e){return t.map(e,function(t){return u.escape(t)})}).sort(),function(t){return t.join("=")}).join("&")},signature_params:function(){return t.pairs(this.attributes()).concat(t.pairs(this.params),this.url_params())},url_params:function(){var e=[];return t.each(URI.parseQuery(this.uri.query||""),function(r,n){e.push(t(t.flatten([r]).sort()).chain().map(function(t){return[n,t]}).value())}),e.flatten?e.flatten(!0):t.flatten(e,!0)}}),SimpleOAuth.Header=u;var o=function(e){switch(t.extend(this,e),this.default_port="",this.scheme){case"http":this.default_port="80";break;case"https":this.default_port="443"}};t.extend(o.prototype,{normalize:function(){this.path&&""==this.path&&(this.path="/"),this.scheme&&this.scheme!=this.scheme.toLowerCase()&&(this.scheme=this.scheme.toLowerCase()),this.host&&this.host!=this.host.toLowerCase()&&(this.host=this.host.toLowerCase())},build:function(){var t="";return this.scheme&&(t+=this.scheme,t+=":"),this.opaque?t+=this.opaque:(this.host&&(t+="//"),this.userinfo&&(t+=this.userinfo,t+="@"),this.host&&(t+=this.host),this.port&&this.port!=this.default_port&&(t+=":",t+=this.port),t+=this.path,this.query&&(t+="?",t+=this.query)),this.fragment&&(t+="#",t+=this.fragment),t}}),URI={parseQuery:function(e){var r,n=(e||"").split("&"),s={};for(r=n.shift();r;r=n.shift()){var i=r.split("="),u="";i[1]&&(u=decodeURIComponent(i[1])),s[i[0]]?s[i[0]]instanceof Array?t.isArray(s[i[0]])?s[i[0]].push(u):s[i[0]]=[s[i[0]],u]:s[i[0]]=[s[i[0]],u]:s[i[0]]=u}return s},queryString:function(e){return t.map(e,function(t,e){return e+"="+encodeURIComponent(t)}).join("&")},parseUri:function(t){var e=a.parse(t);return e.queryKey={},e.query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g,function(t,r,n){r&&(e.queryKey[r]=n)}),new o(e)}};var a={};return a.protocol_expression=/^[a-z][a-z0-9.+-]*$/i,a.parse=function(t){var e,r={};return r.source=t,r.fragment="",e=t.indexOf("#"),e>-1&&(r.fragment=t.substring(e+1)||"",t=t.substring(0,e)),e=t.indexOf("?"),r.query="",e>-1&&(r.query=t.substring(e+1)||"",t=t.substring(0,e)),"//"===t.substring(0,2)?(r.scheme=null,t=t.substring(2),t=a.parseAuthority(t,r)):(e=t.indexOf(":"),e>-1&&(r.scheme=t.substring(0,e)||"",r.scheme&&!r.scheme.match(a.protocol_expression)?r.scheme=void 0:"file"===r.scheme?t=t.substring(e+3):"//"===t.substring(e+1,e+3)?(t=t.substring(e+3),t=a.parseAuthority(t,r)):(t=t.substring(e+1),r.urn=!0))),r.path=t,r.path.length>0&&(r.directory="",r.file=""),r.relative=r.path+(r.query.length>0?"?"+r.query:""),r},a.parseHost=function(t,e){var r,n,s=t.indexOf("/");return-1===s&&(s=t.length),"["===t.charAt(0)?(r=t.indexOf("]"),e.host=t.substring(1,r)||"",e.port=t.substring(r+2,s)||"","/"===e.port&&(e.port="")):t.indexOf(":")!==t.lastIndexOf(":")?(e.host=t.substring(0,s)||"",e.port=""):(n=t.substring(0,s).split(":"),e.host=n[0]||"",e.port=n[1]||""),e.host&&"/"!==t.substring(s).charAt(0)&&(s++,t="/"+t),e.authority=(e.host||"")+(e.port?":"+e.port:""),t.substring(s)||"/"},a.parseAuthority=function(t,e){return t=a.parseUserinfo(t,e),a.parseHost(t,e)},a.parseUserinfo=function(t,e){var r,n=t.indexOf("/"),s=n>-1?t.lastIndexOf("@",n):t.indexOf("@");return s>-1&&(-1===n||n>s)?(r=t.substring(0,s).split(":"),e.user=r[0]?decodeURIComponent(r[0]):"",r.shift(),e.password=r[0]?decodeURIComponent(r.join(":")):"",e.userinfo=e.user+"@"+e.password,t=t.substring(s+1)):(e.userinfo="",e.user="",e.password=""),t},SimpleOAuth});