forked from nraboy/ng-cordova-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-cordova-oauth.min.js
3 lines (3 loc) · 27.7 KB
/
ng-cordova-oauth.min.js
1
2
3
// ng-cordova-oauth - v0.1.0 (2015-04-20)
// http://www.nraboy.com
!function(){angular.module("ngCordovaOauth",["ngCordovaOauthUtility"]).factory("$cordovaOauth",["$q","$http","$cordovaOauthUtility",function(a,b,c){return{adfs:function(c,d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var h=window.open(d+"/adfs/oauth2/authorize?response_type=code&client_id="+c+"&redirect_uri=http://localhost/callback&resource="+e,"_blank","location=no");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){var e=a.url.split("code=")[1];b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:d+"/adfs/oauth2/token",data:"client_id="+c+"&code="+e+"&redirect_uri=http://localhost/callback&grant_type=authorization_code"}).success(function(a){f.resolve(a)}).error(function(a,b){f.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)})}}),h.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},dropbox:function(b,c){var d=a.defer();if(window.cordova){var e=cordova.require("cordova/plugin_list").metadata;if(e.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var f="http://localhost/callback";void 0!==c&&c.hasOwnProperty("redirect_uri")&&(f=c.redirect_uri);var g=window.open("https://www.dropbox.com/1/oauth2/authorize?client_id="+b+"&redirect_uri="+f+"&response_type=token","_blank","location=no,clearsessioncache=yes,clearcache=yes");g.addEventListener("loadstart",function(a){if(0===a.url.indexOf(f)){g.removeEventListener("exit",function(a){}),g.close();for(var b=a.url.split("#")[1],c=b.split("&"),e=[],h=0;h<c.length;h++)e[c[h].split("=")[0]]=c[h].split("=")[1];void 0!==e.access_token&&null!==e.access_token?d.resolve({access_token:e.access_token,token_type:e.token_type,uid:e.uid}):d.reject("Problem authenticating")}}),g.addEventListener("exit",function(a){d.reject("The sign in flow was canceled")})}else d.reject("Could not find InAppBrowser plugin")}else d.reject("Cannot authenticate via a web browser");return d.promise},digitalOcean:function(c,d){var e=a.defer();if(window.cordova){var f=cordova.require("cordova/plugin_list").metadata;if(f.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var g=window.open("https://cloud.digitalocean.com/v1/oauth/authorize?client_id="+c+"&redirect_uri=http://localhost/callback&response_type=code&scope=read%20write","_blank","location=no,clearsessioncache=yes,clearcache=yes");g.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){var f=a.url.split("code=")[1];b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:"https://cloud.digitalocean.com/v1/oauth/token",data:"client_id="+c+"&client_secret="+d+"&redirect_uri=http://localhost/callback&grant_type=authorization_code&code="+f}).success(function(a){e.resolve(a)}).error(function(a,b){e.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){g.close()},10)})}}),g.addEventListener("exit",function(a){e.reject("The sign in flow was canceled")})}else e.reject("Could not find InAppBrowser plugin")}else e.reject("Cannot authenticate via a web browser");return e.promise},google:function(b,c,d){var e=a.defer();if(window.cordova){var f=cordova.require("cordova/plugin_list").metadata;if(f.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var g="http://localhost/callback";void 0!==d&&d.hasOwnProperty("redirect_uri")&&(g=d.redirect_uri);var h=window.open("https://accounts.google.com/o/oauth2/auth?client_id="+b+"&redirect_uri="+g+"&scope="+c.join(" ")+"&approval_prompt=force&response_type=token","_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf(g)){h.removeEventListener("exit",function(a){}),h.close();for(var b=a.url.split("#")[1],c=b.split("&"),d=[],f=0;f<c.length;f++)d[c[f].split("=")[0]]=c[f].split("=")[1];void 0!==d.access_token&&null!==d.access_token?e.resolve({access_token:d.access_token,token_type:d.token_type,expires_in:d.expires_in}):e.reject("Problem authenticating")}}),h.addEventListener("exit",function(a){e.reject("The sign in flow was canceled")})}else e.reject("Could not find InAppBrowser plugin")}else e.reject("Cannot authenticate via a web browser");return e.promise},github:function(c,d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var h=window.open("https://github.com/login/oauth/authorize?client_id="+c+"&redirect_uri=http://localhost/callback&scope="+e.join(","),"_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){0===a.url.indexOf("http://localhost/callback")&&(requestToken=a.url.split("code=")[1],b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b.defaults.headers.post.accept="application/json",b({method:"post",url:"https://github.com/login/oauth/access_token",data:"client_id="+c+"&client_secret="+d+"&redirect_uri=http://localhost/callback&code="+requestToken}).success(function(a){f.resolve(a)}).error(function(a,b){f.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)}))}),h.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},facebook:function(b,c,d){var e=a.defer();if(window.cordova){var f=cordova.require("cordova/plugin_list").metadata;if(f.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var g="http://localhost/callback";void 0!==d&&d.hasOwnProperty("redirect_uri")&&(g=d.redirect_uri);var h=window.open("https://www.facebook.com/v2.0/dialog/oauth?client_id="+b+"&redirect_uri="+g+"&response_type=token&scope="+c.join(","),"_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf(g)){h.removeEventListener("exit",function(a){}),h.close();for(var b=a.url.split("#")[1],c=b.split("&"),d=[],f=0;f<c.length;f++)d[c[f].split("=")[0]]=c[f].split("=")[1];void 0!==d.access_token&&null!==d.access_token?e.resolve({access_token:d.access_token,expires_in:d.expires_in}):e.reject("Problem authenticating")}}),h.addEventListener("exit",function(a){e.reject("The sign in flow was canceled")})}else e.reject("Could not find InAppBrowser plugin")}else e.reject("Cannot authenticate via a web browser");return e.promise},linkedin:function(c,d,e,f){var g=a.defer();if(window.cordova){var h=cordova.require("cordova/plugin_list").metadata;if(h.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var i=window.open("https://www.linkedin.com/uas/oauth2/authorization?client_id="+c+"&redirect_uri=http://localhost/callback&scope="+e.join(" ")+"&response_type=code&state="+f,"_blank","location=no,clearsessioncache=yes,clearcache=yes");i.addEventListener("loadstart",function(a){0===a.url.indexOf("http://localhost/callback")&&(requestToken=a.url.split("code=")[1],b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:"https://www.linkedin.com/uas/oauth2/accessToken",data:"client_id="+c+"&client_secret="+d+"&redirect_uri=http://localhost/callback&grant_type=authorization_code&code="+requestToken}).success(function(a){g.resolve(a)}).error(function(a,b){g.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){i.close()},10)}))}),i.addEventListener("exit",function(a){g.reject("The sign in flow was canceled")})}else g.reject("Could not find InAppBrowser plugin")}else g.reject("Cannot authenticate via a web browser");return g.promise},instagram:function(b,d,e){var f=a.defer(),g={code:"?",token:"#"};if(window.cordova){var h=cordova.require("cordova/plugin_list").metadata;if(h.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var i="http://localhost/callback",j="token";void 0!==e&&(e.hasOwnProperty("redirect_uri")&&(i=e.redirect_uri),e.hasOwnProperty("response_type")&&(j=e.response_type));var k=window.open("https://api.instagram.com/oauth/authorize/?client_id="+b+"&redirect_uri="+i+"&scope="+d.join(" ")+"&response_type="+j,"_blank","location=no,clearsessioncache=yes,clearcache=yes");k.addEventListener("loadstart",function(a){if(0===a.url.indexOf(i)){k.removeEventListener("exit",function(a){}),k.close();var b=a.url.split(g[j])[1],d=c.parseResponseParameters(b);void 0!==d.access_token&&null!==d.access_token?f.resolve({access_token:d.access_token}):void 0!==d.code&&null!==d.code?f.resolve({code:d.code}):f.reject("Problem authenticating")}}),k.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},box:function(c,d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var h=window.open("https://app.box.com/api/oauth2/authorize/?client_id="+c+"&redirect_uri=http://localhost/callback&state="+e+"&response_type=code","_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){0===a.url.indexOf("http://localhost/callback")&&(requestToken=a.url.split("code=")[1],b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:"https://app.box.com/api/oauth2/token",data:"client_id="+c+"&client_secret="+d+"&redirect_uri=http://localhost/callback&grant_type=authorization_code&code="+requestToken}).success(function(a){f.resolve(a)}).error(function(a,b){f.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)}))}),h.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},reddit:function(c,d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var h=window.open("https://ssl.reddit.com/api/v1/authorize?client_id="+c+"&redirect_uri=http://localhost/callback&duration=permanent&state=ngcordovaoauth&scope="+e.join(",")+"&response_type=code","_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){0===a.url.indexOf("http://localhost/callback")&&(requestToken=a.url.split("code=")[1],b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b.defaults.headers.post.Authorization="Basic "+btoa(c+":"+d),b({method:"post",url:"https://ssl.reddit.com/api/v1/access_token",data:"redirect_uri=http://localhost/callback&grant_type=authorization_code&code="+requestToken}).success(function(a){f.resolve(a)}).error(function(a,b){f.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)}))}),h.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},twitter:function(d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0)if("undefined"!=typeof jsSHA){var h={oauth_consumer_key:d,oauth_nonce:c.createNonce(10),oauth_signature_method:"HMAC-SHA1",oauth_timestamp:Math.round((new Date).getTime()/1e3),oauth_version:"1.0"},i=c.createSignature("POST","https://api.twitter.com/oauth/request_token",h,{oauth_callback:"http://localhost/callback"},e);b({method:"post",url:"https://api.twitter.com/oauth/request_token",headers:{Authorization:i.authorization_header,"Content-Type":"application/x-www-form-urlencoded"},data:"oauth_callback="+encodeURIComponent("http://localhost/callback")}).success(function(a){for(var d=a.split("&"),g={},i=0;i<d.length;i++)g[d[i].split("=")[0]]=d[i].split("=")[1];g.hasOwnProperty("oauth_token")===!1&&f.reject("Oauth request token was not received");var j=window.open("https://api.twitter.com/oauth/authenticate?oauth_token="+g.oauth_token,"_blank","location=no,clearsessioncache=yes,clearcache=yes");j.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){for(var d=a.url.split("?")[1],g=d.split("&"),i={},k=0;k<g.length;k++)i[g[k].split("=")[0]]=g[k].split("=")[1];i.hasOwnProperty("oauth_verifier")===!1&&f.reject("Browser authentication failed to complete. No oauth_verifier was returned"),delete h.oauth_signature,h.oauth_token=i.oauth_token;var l=c.createSignature("POST","https://api.twitter.com/oauth/access_token",h,{oauth_verifier:i.oauth_verifier},e);b({method:"post",url:"https://api.twitter.com/oauth/access_token",headers:{Authorization:l.authorization_header},params:{oauth_verifier:i.oauth_verifier}}).success(function(a){for(var b=a.split("&"),c={},d=0;d<b.length;d++)c[b[d].split("=")[0]]=b[d].split("=")[1];c.hasOwnProperty("oauth_token_secret")===!1&&f.reject("Oauth access token was not received"),f.resolve(c)}).error(function(a){f.reject(a)})["finally"](function(){setTimeout(function(){j.close()},10)})}}),j.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}).error(function(a){f.reject(a)})}else f.reject("Missing jsSHA JavaScript library");else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},meetup:function(b,c){var d=a.defer();if(window.cordova){var e=cordova.require("cordova/plugin_list").metadata;if(e.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var f="http://localhost/callback";void 0!==c&&c.hasOwnProperty("redirect_uri")&&(f=c.redirect_uri);var g=window.open("https://secure.meetup.com/oauth2/authorize/?client_id="+b+"&redirect_uri="+f+"&response_type=token","_blank","location=no,clearsessioncache=yes,clearcache=yes");g.addEventListener("loadstart",function(a){if(0===a.url.indexOf(f)){g.removeEventListener("exit",function(a){}),g.close();for(var b=a.url.split("#")[1],c=b.split("&"),e={},h=0;h<c.length;h++)e[c[h].split("=")[0]]=c[h].split("=")[1];void 0!==e.access_token&&null!==e.access_token?d.resolve(e):d.reject("Problem authenticating")}}),g.addEventListener("exit",function(a){d.reject("The sign in flow was canceled")})}else d.reject("Could not find InAppBrowser plugin")}else d.reject("Cannot authenticate via a web browser");return d.promise},salesforce:function(b,c){var d="http://localhost/callback",e=function(a,b,c){return a+"services/oauth2/authorize?display=touch&response_type=token&client_id="+escape(b)+"&redirect_uri="+escape(c)},f=function(a,b){return a.substr(0,b.length)===b},g=a.defer();if(window.cordova){var h=cordova.require("cordova/plugin_list").metadata;if(h.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var i=window.open(e(b,c,d),"_blank","location=no,clearsessioncache=yes,clearcache=yes");i.addEventListener("loadstart",function(a){if(f(a.url,d)){var b={},c=a.url.split("#")[1];if(c){var e=c.split("&");for(var h in e){var j=e[h].split("=");b[j[0]]=unescape(j[1])}}"undefined"==typeof b||"undefined"==typeof b.access_token?g.reject("Problem authenticating"):g.resolve(b),setTimeout(function(){i.close()},10)}}),i.addEventListener("exit",function(a){g.reject("The sign in flow was canceled")})}else g.reject("Could not find InAppBrowser plugin")}else g.reject("Cannot authenticate via a web browser");return g.promise},strava:function(c,d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var h=window.open("https://www.strava.com/oauth/authorize?client_id="+c+"&redirect_uri=http://localhost/callback&scope="+e.join(",")+"&response_type=code&approval_prompt=force","_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){0===a.url.indexOf("http://localhost/callback")&&(requestToken=a.url.split("code=")[1],b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:"https://www.strava.com/oauth/token",data:"client_id="+c+"&client_secret="+d+"&code="+requestToken}).success(function(a){f.resolve(a)}).error(function(a,b){f.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)}))}),h.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},withings:function(d,e){var f=a.defer();if(window.cordova){var g=cordova.require("cordova/plugin_list").metadata;if(g.hasOwnProperty("org.apache.cordova.inappbrowser")===!0)if("undefined"!=typeof jsSHA){var h=c.generateOauthParametersInstance(d);h.oauth_callback="http://localhost/callback";var i="https://oauth.withings.com/account/request_token",j=c.createSignature("GET",i,{},h,e);h.oauth_signature=j.signature;var k=c.generateUrlParameters(h);b({method:"get",url:i+"?"+k}).success(function(a){var g=c.parseResponseParameters(a);g.hasOwnProperty("oauth_token")===!1&&f.reject("Oauth request token was not received");var h=c.generateOauthParametersInstance(d);h.oauth_token=g.oauth_token;var i=g.oauth_token_secret,j="https://oauth.withings.com/account/authorize",k=c.createSignature("GET",j,{},h,e);h.oauth_signature=k.signature;var l=c.generateUrlParameters(h),m=window.open(j+"?"+l,"_blank","location=no,clearsessioncache=yes,clearcache=yes");m.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){var h=a.url.split("?")[1];g=c.parseResponseParameters(h),g.hasOwnProperty("oauth_verifier")===!1&&f.reject("Browser authentication failed to complete. No oauth_verifier was returned");var j=c.generateOauthParametersInstance(d);j.oauth_token=g.oauth_token;var k="https://oauth.withings.com/account/access_token",l=c.createSignature("GET",k,{},j,e,i);j.oauth_signature=l.signature;var n=c.generateUrlParameters(j);b({method:"get",url:k+"?"+n}).success(function(a){var b=c.parseResponseParameters(a);b.hasOwnProperty("oauth_token_secret")===!1&&f.reject("Oauth access token was not received"),f.resolve(b)}).error(function(a){f.reject(a)})["finally"](function(){setTimeout(function(){m.close()},10)})}}),m.addEventListener("exit",function(a){f.reject("The sign in flow was canceled")})}).error(function(a){f.reject(a)})}else f.reject("Missing jsSHA JavaScript library");else f.reject("Could not find InAppBrowser plugin")}else f.reject("Cannot authenticate via a web browser");return f.promise},foursquare:function(b,c){var d=a.defer();if(window.cordova){var e=cordova.require("cordova/plugin_list").metadata;if(e.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var f="http://localhost/callback";void 0!==c&&c.hasOwnProperty("redirect_uri")&&(f=c.redirect_uri);var g=window.open("https://foursquare.com/oauth2/authenticate?client_id="+b+"&redirect_uri="+f+"&response_type=token","_blank","location=no,clearsessioncache=yes,clearcache=yes");g.addEventListener("loadstart",function(a){if(0===a.url.indexOf(f)){g.removeEventListener("exit",function(a){}),g.close();for(var b=a.url.split("#")[1],c=b.split("&"),e=[],h=0;h<c.length;h++)e[c[h].split("=")[0]]=c[h].split("=")[1];if(void 0!==e.access_token&&null!==e.access_token){var i={access_token:e.access_token,expires_in:e.expires_in};d.resolve(i)}else d.reject("Problem authenticating")}}),g.addEventListener("exit",function(a){d.reject("The sign in flow was canceled")})}else d.reject("Could not find InAppBrowser plugin")}else d.reject("Cannot authenticate via a web browser");return d.promise},magento:function(d,e,f){var g=a.defer();if(window.cordova){var h=cordova.require("cordova/plugin_list").metadata;if(h.hasOwnProperty("org.apache.cordova.inappbrowser")===!0)if("undefined"!=typeof jsSHA){var i={oauth_callback:"http://localhost/callback",oauth_consumer_key:e,oauth_nonce:c.createNonce(5),oauth_signature_method:"HMAC-SHA1",oauth_timestamp:Math.round((new Date).getTime()/1e3),oauth_version:"1.0"},j=c.createSignature("POST",d+"/oauth/initiate",i,{oauth_callback:"http://localhost/callback"},f);b.defaults.headers.post.Authorization=j.authorization_header,b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:d+"/oauth/initiate",data:"oauth_callback=http://localhost/callback"}).success(function(a){for(var e=a.split("&"),h={},j=0;j<e.length;j++)h[e[j].split("=")[0]]=e[j].split("=")[1];h.hasOwnProperty("oauth_token")===!1&&g.reject("Oauth request token was not received");var k=h.oauth_token_secret,l=window.open(d+"/oauth/authorize?oauth_token="+h.oauth_token,"_blank","location=no,clearsessioncache=yes,clearcache=yes");l.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){for(var e=a.url.split("?")[1],h=e.split("&"),j={},m=0;m<h.length;m++)j[h[m].split("=")[0]]=h[m].split("=")[1];j.hasOwnProperty("oauth_verifier")===!1&&g.reject("Browser authentication failed to complete. No oauth_verifier was returned"),delete i.oauth_signature,delete i.oauth_callback,i.oauth_token=j.oauth_token,i.oauth_nonce=c.createNonce(5),i.oauth_verifier=j.oauth_verifier;var n=c.createSignature("POST",d+"/oauth/token",i,{},f,k);b.defaults.headers.post.Authorization=n.authorization_header,b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:d+"/oauth/token"}).success(function(a){for(var b=a.split("&"),c={},d=0;d<b.length;d++)c[b[d].split("=")[0]]=b[d].split("=")[1];c.hasOwnProperty("oauth_token_secret")===!1&&g.reject("Oauth access token was not received"),g.resolve(c)}).error(function(a){g.reject(a)})["finally"](function(){setTimeout(function(){l.close()},10)})}}),l.addEventListener("exit",function(a){g.reject("The sign in flow was canceled")})}).error(function(a){g.reject(a)})}else g.reject("Missing jsSHA JavaScript library");else g.reject("Could not find InAppBrowser plugin")}else g.reject("Cannot authenticate via a web browser");return g.promise},vkontakte:function(b,c){var d=a.defer();if(window.cordova){var e=cordova.require("cordova/plugin_list").metadata;if(e.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var f=window.open("https://oauth.vk.com/authorize?client_id="+b+"&redirect_uri=http://oauth.vk.com/blank.html&response_type=token&scope="+c.join(",")+"&display=touch&response_type=token","_blank","location=no,clearsessioncache=yes,clearcache=yes");f.addEventListener("loadstart",function(a){var b=a.url.split("#");if("https://oauth.vk.com/blank.html"==b[0]||"http://oauth.vk.com/blank.html"==b[0]){f.removeEventListener("exit",function(a){}),f.close();for(var c=a.url.split("#")[1],e=c.split("&"),g=[],h=0;h<e.length;h++)g[e[h].split("=")[0]]=e[h].split("=")[1];if(void 0!==g.access_token&&null!==g.access_token){var i={access_token:g.access_token,expires_in:g.expires_in};void 0!==g.email&&null!==g.email&&(i.email=g.email),d.resolve(i)}else d.reject("Problem authenticating")}}),f.addEventListener("exit",function(a){d.reject("The sign in flow was canceled")})}else d.reject("Could not find InAppBrowser plugin")}else d.reject("Cannot authenticate via a web browser");return d.promise},imgur:function(b,c){var d=a.defer();if(window.cordova){var e=cordova.require("cordova/plugin_list").metadata;if(e.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var f="http://localhost/callback";void 0!==c&&c.hasOwnProperty("redirect_uri")&&(f=c.redirect_uri);var g=window.open("https://api.imgur.com/oauth2/authorize?client_id="+b+"&response_type=token","_blank","location=no,clearsessioncache=yes,clearcache=yes");g.addEventListener("loadstart",function(a){if(0===a.url.indexOf(f)){g.removeEventListener("exit",function(a){}),g.close();for(var b=a.url.split("#")[1],c=b.split("&"),e=[],h=0;h<c.length;h++)e[c[h].split("=")[0]]=c[h].split("=")[1];void 0!==e.access_token&&null!==e.access_token?d.resolve({access_token:e.access_token,expires_in:e.expires_in,account_username:e.account_username}):d.reject("Problem authenticating")}}),g.addEventListener("exit",function(a){d.reject("The sign in flow was canceled")})}else d.reject("Could not find InAppBrowser plugin")}else d.reject("Cannot authenticate via a web browser");return d.promise},spotify:function(b,c,d){var e=a.defer();if(window.cordova){var f=cordova.require("cordova/plugin_list").metadata;if(f.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var g="http://localhost/callback";void 0!==d&&d.hasOwnProperty("redirect_uri")&&(g=d.redirect_uri);var h=window.open("https://accounts.spotify.com/authorize?client_id="+b+"&redirect_uri="+g+"&response_type=token&scope="+c.join(" "),"_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf(g)){h.removeEventListener("exit",function(a){}),h.close();for(var b=a.url.split("#")[1],c=b.split("&"),d=[],f=0;f<c.length;f++)d[c[f].split("=")[0]]=c[f].split("=")[1];void 0!==d.access_token&&null!==d.access_token?e.resolve({access_token:d.access_token,expires_in:d.expires_in,account_username:d.account_username}):e.reject("Problem authenticating")}}),h.addEventListener("exit",function(a){e.reject("The sign in flow was canceled")})}else e.reject("Could not find InAppBrowser plugin")}else e.reject("Cannot authenticate via a web browser");return e.promise},uber:function(b,c,d){var e=a.defer();if(window.cordova){var f=cordova.require("cordova/plugin_list").metadata;if(f.hasOwnProperty("org.apache.cordova.inappbrowser")===!0){var g="http://localhost/callback";void 0!==d&&d.hasOwnProperty("redirect_uri")&&(g=d.redirect_uri);var h=window.open("https://login.uber.com/oauth/authorize?client_id="+b+"&redirect_uri="+g+"&response_type=token&scope="+c.join(" "),"_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf(g)){h.removeEventListener("exit",function(a){}),h.close();for(var b=a.url.split("#")[1],c=b.split("&"),d=[],f=0;f<c.length;f++)d[c[f].split("=")[0]]=c[f].split("=")[1];void 0!==d.access_token&&null!==d.access_token?e.resolve({access_token:d.access_token,token_type:d.token_type,expires_in:d.expires_in,scope:d.scope}):e.reject("Problem authenticating")}}),h.addEventListener("exit",function(a){e.reject("The sign in flow was canceled")})}else e.reject("Could not find InAppBrowser plugin")}else e.reject("Cannot authenticate via a web browser");return e.promise}}}]),angular.module("ngCordovaOauthUtility",[]).factory("$cordovaOauthUtility",["$q",function(a){return{createSignature:function(a,b,c,d,e,f){if("undefined"!=typeof jsSHA){for(var g=angular.copy(c),h=Object.keys(d),i=0;i<h.length;i++)g[h[i]]=encodeURIComponent(d[h[i]]);var j=a+"&"+encodeURIComponent(b)+"&",k=Object.keys(g).sort();for(i=0;i<k.length;i++)j+=encodeURIComponent(i==k.length-1?k[i]+"="+g[k[i]]:k[i]+"="+g[k[i]]+"&");var l=new jsSHA(j,"TEXT"),m="";f&&(m=encodeURIComponent(f)),c.oauth_signature=encodeURIComponent(l.getHMAC(encodeURIComponent(e)+"&"+m,"TEXT","SHA-1","B64"));var n=Object.keys(c),o="OAuth ";for(i=0;i<n.length;i++)o+=i==n.length-1?n[i]+'="'+c[n[i]]+'"':n[i]+'="'+c[n[i]]+'",';return{signature_base_string:j,authorization_header:o,signature:c.oauth_signature}}return"Missing jsSHA JavaScript library"},createNonce:function(a){for(var b="",c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",d=0;a>d;d++)b+=c.charAt(Math.floor(Math.random()*c.length));return b},generateUrlParameters:function(a){var b=Object.keys(a);b.sort();for(var c="",d="",e=0;e<b.length;e++)c+=d+b[e]+"="+a[b[e]],d="&";return c},parseResponseParameters:function(a){if(a.split){for(var b=a.split("&"),c={},d=0;d<b.length;d++)c[b[d].split("=")[0]]=b[d].split("=")[1];return c}return{}},generateOauthParametersInstance:function(a){var b=new jsSHA(Math.round((new Date).getTime()/1e3),"TEXT"),c={oauth_consumer_key:a,oauth_nonce:b.getHash("SHA-1","HEX"),oauth_signature_method:"HMAC-SHA1",oauth_timestamp:Math.round((new Date).getTime()/1e3),oauth_version:"1.0"};return c}}}])}();