diff --git a/dist/ng-cordova-oauth.js b/dist/ng-cordova-oauth.js index ac84837..3fde0fc 100644 --- a/dist/ng-cordova-oauth.js +++ b/dist/ng-cordova-oauth.js @@ -681,7 +681,17 @@ function google($q, $http, $cordovaOauthUtility) { */ function oauthGoogle(clientId, appScope, options) { - var deferred = $q.defer(); + var browserRef, deferred = $q.defer(); + + // ------------- local funcions ---------------- + function cleanUp() { + if (browserRef) { + browserRef.removeEventListener("exit",function(event){}); + browserRef.removeEventListener("loadstart",function(event){}); + browserRef.removeEventListener("loadstop",function(event){}); + browserRef.close(); + } + } if(window.cordova) { @@ -716,7 +726,7 @@ function google($q, $http, $cordovaOauthUtility) { if (prompt_options.length===0) prompt_options.push('none'); - var browserRef = window.cordova.InAppBrowser.open( + browserRef = window.cordova.InAppBrowser.open( 'https://accounts.google.com/o/oauth2/auth?' + 'client_id=' + clientId + '&redirect_uri=' + redirect_uri @@ -727,15 +737,18 @@ function google($q, $http, $cordovaOauthUtility) { , '_blank' - , 'location=no' + , 'location=no,hidden=yes' + ',clearsessioncache=' + clear_session + ',clearcache=' + clear_cache ); + //this hooks into browser new pages to test if the proccess of authentication have reached its end (loading redirect_uri) + //so that it can grab redirect_uri query string to capture token + //also, closes the browser in order to get back to app browserRef.addEventListener("loadstart", function(event) { - if((event.url).indexOf(redirect_uri) === 0) { - browserRef.removeEventListener("exit",function(event){}); - browserRef.close(); + if((event.url).indexOf(redirect_uri) === 0) { //if new URL begins with redirect_uri + cleanUp(); + //generates object from query string var callbackResponse = (event.url).split("#")[1]; var responseParameters = (callbackResponse).split("&"); var parameterMap = []; @@ -750,6 +763,27 @@ function google($q, $http, $cordovaOauthUtility) { } }); + browserRef.addEventListener("loaderror", function(event) { + if((event.url).indexOf(redirect_uri) === 0) { + return; // nothing to do here, this error is expected. 'loadstart' event will treat it. + } + //something went wrong, so, let's cleanup + cleanUp(); + + + deferred.reject("Problem authenticating (loaderror)"); //and get back to the app with an error + console.log("inAppBrowser loaderror => event: ", JSON.stringify(event) ); + + + }); + browserRef.addEventListener("loadstop", function(event) { + + browserRef.show(); //just show the browser on first page loaded + //navigator.notification.activityStop(); + + browserRef.removeEventListener("loadstop",function(event){}); //remove itself + }); + browserRef.addEventListener('exit', function(event) { deferred.reject("The sign in flow was canceled"); }); diff --git a/dist/ng-cordova-oauth.min.js b/dist/ng-cordova-oauth.min.js deleted file mode 100644 index 0ea9bbf..0000000 --- a/dist/ng-cordova-oauth.min.js +++ /dev/null @@ -1,4 +0,0 @@ -// ng-cordova-oauth - v0.2.3 (2016-02-10) -// http://www.nraboy.com -function adfs(a,b,c){function d(d,e,f){var g=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var h=window.cordova.InAppBrowser.open(e+"/adfs/oauth2/authorize?response_type=code&client_id="+d+"&redirect_uri=http://localhost/callback&resource="+f,"_blank","location=no");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){var c=a.url.split("code=")[1];b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:e+"/adfs/oauth2/token",data:"client_id="+d+"&code="+c+"&redirect_uri=http://localhost/callback&grant_type=authorization_code"}).success(function(a){g.resolve(a)}).error(function(a,b){g.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)})}}),h.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}return{signin:d}}function azureAD(a,b,c){function d(d,e,f){var g=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var h=window.cordova.InAppBrowser.open("https://login.microsoftonline.com/"+e+"/oauth2/authorize?response_type=code&client_id="+d+"&redirect_uri=http://localhost/callback","_blank","location=no,clearsessioncache=yes,clearcache=yes");h.addEventListener("loadstart",function(a){if(0===a.url.indexOf("http://localhost/callback")){var c=a.url.split("code=")[1];console.log(c),b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:"https://login.microsoftonline.com/"+e+"/oauth2/token",data:"client_id="+d+"&code="+c+"&redirect_uri=http://localhost/callback&grant_type=authorization_code&resource="+f}).success(function(a){g.resolve(a)}).error(function(a,b){g.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){h.close()},10)})}}),h.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}return{signin:d}}function box(a,b,c){function d(d,e,f,g){var h=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var i="http://localhost/callback";void 0!==g&&g.hasOwnProperty("redirect_uri")&&(i=g.redirect_uri);var j=window.cordova.InAppBrowser.open("https://app.box.com/api/oauth2/authorize/?client_id="+d+"&redirect_uri="+i+"&state="+f+"&response_type=code","_blank","location=no,clearsessioncache=yes,clearcache=yes");j.addEventListener("loadstart",function(a){0===a.url.indexOf(i)&&(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="+d+"&client_secret="+e+"&redirect_uri="+i+"&grant_type=authorization_code&code="+requestToken}).success(function(a){h.resolve(a)}).error(function(a,b){h.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){j.close()},10)}))}),j.addEventListener("exit",function(a){h.reject("The sign in flow was canceled")})}else h.reject("Could not find InAppBrowser plugin");else h.reject("Cannot authenticate via a web browser");return h.promise}return{signin:d}}function digitalOcean(a,b,c){function d(d,e,f){var g=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var h="http://localhost/callback";void 0!==f&&f.hasOwnProperty("redirect_uri")&&(h=f.redirect_uri);var i=window.cordova.InAppBrowser.open("https://cloud.digitalocean.com/v1/oauth/authorize?client_id="+d+"&redirect_uri="+h+"&response_type=code&scope=read%20write","_blank","location=no,clearsessioncache=yes,clearcache=yes");i.addEventListener("loadstart",function(a){if(0===a.url.indexOf(h)){var c=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="+d+"&client_secret="+e+"&redirect_uri="+h+"&grant_type=authorization_code&code="+c}).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}return{signin:d}}function dribble(a,b,c){function d(d,e,f,g,h){var i=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var j="http://localhost/callback",k="https://dribbble.com/oauth/authorize",l="https://dribbble.com/oauth/token";void 0!==g&&g.hasOwnProperty("redirect_uri")&&(j=g.redirect_uri),void 0===h&&(h=c.createNonce(5));var m=f.join(",").replace(/,/g,"+"),n=window.cordova.InAppBrowser.open(k+"?client_id="+d+"&redirect_uri="+j+"&scope="+m+"&state="+h,"_blank","location=no,clearsessioncache=yes,clearcache=yes");n.addEventListener("loadstart",function(a){if(0===a.url.indexOf(j)){var c=a.url.split("code=")[1],f=c.split("&")[0];b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:l,data:"client_id="+d+"&redirect_uri="+j+"&client_secret="+e+"&code="+f}).success(function(a){i.resolve(a)}).error(function(a,b){i.reject("Problem authenticating ")})["finally"](function(){setTimeout(function(){n.close()},10)})}}),n.addEventListener("exit",function(a){i.reject("The sign in flow was canceled")})}else i.reject("Could not find InAppBrowser plugin");else i.reject("Cannot authenticate via a web browser");return i.promise}return{signin:d}}function dropbox(a,b,c){function d(b,d){var e=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var f="http://localhost/callback";void 0!==d&&d.hasOwnProperty("redirect_uri")&&(f=d.redirect_uri);var g=window.cordova.InAppBrowser.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("&"),d=[],h=0;h0&&(j="&scope"+d.join("+"));var k=window.cordova.InAppBrowser.open("https://api.instagram.com/oauth/authorize/?client_id="+b+"&redirect_uri="+h+j+"&response_type="+i,"_blank","location=no,clearsessioncache=yes,clearcache=yes");k.addEventListener("loadstart",function(a){if(0===a.url.indexOf(h)){k.removeEventListener("exit",function(a){}),k.close();var b=a.url.split(g[i])[1],d=c.parseResponseParameters(b);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}return{signin:d}}function jawbone(a,b,c){function d(d,e,f,g){var h=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var i="http://localhost/callback";void 0!==g&&g.hasOwnProperty("redirect_uri")&&(i=g.redirect_uri);var j=window.cordova.InAppBrowser.open("https://jawbone.com/auth/oauth2/auth?client_id="+d+"&redirect_uri="+i+"&response_type=code&scope="+f.join(" "),"_blank","location=no,clearsessioncache=yes,clearcache=yes");j.addEventListener("loadstart",function(a){if(0===a.url.indexOf(i)){var c=a.url.split("code=")[1];b.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded",b({method:"post",url:"https://jawbone.com/auth/oauth2/token",data:"client_id="+d+"&client_secret="+e+"&grant_type=authorization_code&code="+c}).success(function(a){h.resolve(a)}).error(function(a,b){h.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){j.close()},10)})}}),j.addEventListener("exit",function(a){h.reject("The sign in flow was canceled")})}else h.reject("Could not find InAppBrowser plugin");else h.reject("Cannot authenticate via a web browser");return h.promise}return{signin:d}}function cordovaOauth(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M){return{azureAD:d.signin,adfs:e.signin,dropbox:f.signin,digitalOcean:g.signin,google:h.signin,github:i.signin,facebook:j.signin,linkedin:k.signin,instagram:l.signin,box:m.signin,reddit:n.signin,slack:o.signin,twitter:p.signin,meetup:q.signin,salesforce:r.signin,strava:s.signin,withings:t.signin,foursquare:u.signin,magento:v.signin,vkontakte:w.signin,odnoklassniki:x.signin,imgur:y.signin,spotify:z.signin,uber:A.signin,windowsLive:B.signin,yammer:C.signin,venmo:D.signin,stripe:E.signin,rally:F.signin,familySearch:G.signin,envato:H.signin,weibo:I.signin,jawbone:J.signin,untappd:K.signin,dribble:L.signin,pocket:M.signin}}function linkedin(a,b,c){function d(d,e,f,g,h){var i=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled()){var j="http://localhost/callback";void 0!==h&&h.hasOwnProperty("redirect_uri")&&(j=h.redirect_uri);var k=window.cordova.InAppBrowser.open("https://www.linkedin.com/uas/oauth2/authorization?client_id="+d+"&redirect_uri="+j+"&scope="+f.join(" ")+"&response_type=code&state="+g,"_blank","location=no,clearsessioncache=yes,clearcache=yes");k.addEventListener("loadstart",function(a){0===a.url.indexOf(j)&&(requestToken=a.url.split("code=")[1].split("&")[0],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="+d+"&client_secret="+e+"&redirect_uri="+j+"&grant_type=authorization_code&code="+requestToken}).success(function(a){i.resolve(a)}).error(function(a,b){i.reject("Problem authenticating")})["finally"](function(){setTimeout(function(){k.close()},10)}))}),k.addEventListener("exit",function(a){i.reject("The sign in flow was canceled")})}else i.reject("Could not find InAppBrowser plugin");else i.reject("Cannot authenticate via a web browser");return i.promise}return{signin:d}}function magento(a,b,c){function d(d,e,f){var g=a.defer();if(window.cordova)if(c.isInAppBrowserInstalled())if("undefined"!=typeof jsSHA){var h={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"},i=c.createSignature("POST",d+"/oauth/initiate",h,{oauth_callback:"http://localhost/callback"},f);b.defaults.headers.post.Authorization=i.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("&"),i={},j=0;jd;d++)b+=c.charAt(Math.floor(Math.random()*c.length));return b}function e(a){var b=Object.keys(a);b.sort();for(var c="",d="",e=0;e