From 0e782d4b67a1cbae76a866b51541428be4817743 Mon Sep 17 00:00:00 2001 From: Zack LeBlanc Date: Tue, 21 Jul 2015 17:17:34 -0500 Subject: [PATCH] Window event callbacks --- dist/angular-oauth1-client.js | 18 +++++++++++++++--- dist/angular-oauth1-client.min.js | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dist/angular-oauth1-client.js b/dist/angular-oauth1-client.js index cf2a231..7ec4044 100644 --- a/dist/angular-oauth1-client.js +++ b/dist/angular-oauth1-client.js @@ -1,3 +1,5 @@ +/*! angular-oauth1-client - v0.1.2 - 2015-07-21 +* Copyright (c) 2015 Sean Fisher; Licensed MIT */ /*! angular-oauth1-client - v0.1.2 - 2015-07-09 * Copyright (c) 2015 Sean Fisher; Licensed MIT */ /*! angular-oauth1-client - v0.1.2 - 2015-07-07 @@ -285,11 +287,16 @@ angular.module('oauth1Client', ['LocalStorageModule']) return deferred.promise; } - function getAuthorizationToken(oauth_token, callback_url) { + function getAuthorizationToken(oauth_token, callback_url, afterWindowOpen, beforeWindowClose) { var deffered = $q.defer(); var auth_window = window.open(authorizeEndpoint + "?oauth_token=" + oauth_token + "&oauth_callback=" + callback_url, '_blank', 'location=no,clearcache=yes'); + var urlsVisited = 0; auth_window.addEventListener('loadstart', function(event) { + urlsVisited += 1; if((event.url).startsWith(callback_url)) { + if(angular.isFunction(beforeWindowClose)){ + beforeWindowClose(); + } auth_window.close(); deffered.resolve({ @@ -298,6 +305,11 @@ angular.module('oauth1Client', ['LocalStorageModule']) wp_scope: getURLParameter(event.url, 'wp_scope') }); } + if(urlsVisited == 1) { + if(angular.isFunction(afterWindowOpen)){ + afterWindowOpen(); + } + } }); return deffered.promise; } @@ -342,7 +354,7 @@ angular.module('oauth1Client', ['LocalStorageModule']) } return { - authorize: function(storage) { + authorize: function(afterWindowOpen, beforeWindowClose) { var deffered = $q.defer(); var oauthSigner = getOAuthSigner({ @@ -355,7 +367,7 @@ angular.module('oauth1Client', ['LocalStorageModule']) getRequestToken(oauthSigner, oauthCallback) .then(function(request_data) { - return getAuthorizationToken(request_data.oauth_token, oauthCallback); + return getAuthorizationToken(request_data.oauth_token, oauthCallback, afterWindowOpen, beforeWindowClose); }) .then(function(authorization_data) { oauthSigner = getOAuthSigner({ diff --git a/dist/angular-oauth1-client.min.js b/dist/angular-oauth1-client.min.js index 2529e89..53c86bd 100644 --- a/dist/angular-oauth1-client.min.js +++ b/dist/angular-oauth1-client.min.js @@ -1,3 +1,3 @@ -/*! angular-oauth1-client - v0.1.2 - 2015-07-09 +/*! angular-oauth1-client - v0.1.2 - 2015-07-21 * Copyright (c) 2015 Sean Fisher; Licensed MIT */ -!function(a,b,c){"use strict";b.module("oauth1Client",["LocalStorageModule"]).service("oauthPersistence",function(a,b){var c=this,d="oauth_token",e="oauth_token_secret";c.storeAccessToken=function(c){var f=b.defer();return a.set(d,c.oauth_token),a.set(e,c.oauth_token_secret),f.resolve(),f.promise},c.clearAccessToken=function(){a.remove(d),a.remove(e)},c.accessIsInStorage=function(b,c){a.get(d)&&a.get(e)?b():c()},c.getTokenAndSecret=function(b){b(a.get(d),a.get(e))}}).factory("oauth1Signer",[function(){function a(a){for(var b="",c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",d=0;a>d;d++)b+=c.charAt(Math.floor(Math.random()*c.length));return b}return{create:function(b){return _.extend({token:null,tokenSecret:"",version:"1.0",signatureMethod:"HMAC-SHA1",method:"GET",timestamp:Math.floor(Date.now()/1e3),nonce:a(32),callbackUrl:null,verifier:null,oauthParameters:function(){var a,b;return a=this,b={oauth_consumer_key:a.consumerKey,oauth_nonce:a.nonce,oauth_timestamp:a.timestamp,oauth_signature_method:a.signatureMethod},a.token&&(b.oauth_token=a.token),a.version&&(b.oauth_version=a.version),a.callbackUrl&&(b.oauth_callback=a.callbackUrl),a.verifier&&(b.oauth_verifier=a.verifier),b},queryStringFields:function(){var a,b,c;return a=this,b=a.oauthParameters(),c=a.fields,_.each(_.keys(c),function(a){return b[a]=c[a]}),b},queryString:function(){var a,b,c;a=this,b=a.queryStringFields(),c=_.keys(b).sort();var d=_.map(c,function(c){return c+"="+a.percentEncode(b[c])}).join("&");return d},urlEncoded:function(a){return _.map(_.keys(a),function(b){return b+"="+encodeURIComponent(a[b])}).join("&")},headerEncoded:function(a){return _.map(_.keys(a),function(b){return b+'="'+encodeURIComponent(a[b])+'"'}).join(", ")},urlEncodedFields:function(){var a;return a=this,a.urlEncoded(a.fields)},authorizationHeader:function(){var a,b;return a=this,b=a.oauthParameters(),b.oauth_signature=a.base64Signature(),a.headerEncoded(b)},urlAndFields:function(){var a,b;return a=this,b=a.urlEncodedFields(),b?a.url+"?"+b:a.url},parameterEncoded:function(a){var b=this,c=_.map(a,function(a){return b.percentEncode(a)}).join("&");return c},baseString:function(){var a;return a=this,a.parameterEncoded([a.method,a.url,a.queryString()])},hmacKey:function(){var a;return a=this,a.parameterEncoded([a.consumerSecret,a.tokenSecret])},hmac:function(a){var b,c;if(b=a&&a.hasOwnProperty("encoding")&&void 0!==a.encoding?a.encoding:"binary",c=this,"undefined"!=typeof process){var d,e;return d=require("crypto"),e=d.createHmac("sha1",c.hmacKey()),e.update(c.baseString()),e.digest(b)}var f;return f=CryptoJS.HmacSHA1(c.baseString(),c.hmacKey()),"base64"===b?f.toString(CryptoJS.enc.Base64):f},base64Signature:function(){var a;return a=this,a.hmac({encoding:"base64"})},signature:function(){var a;return a=this,a.percentEncode(a.base64Signature())},signedUrl:function(){var a;return a=this,a.url+"?"+a.queryString()+"&oauth_signature="+a.signature()},curl:function(){var a;return a=this,"GET"===a.method()?"curl '"+a.url+"?"+a.queryString()+"&oauth_signature="+a.signature()+"'":"POST"===a.method()||"PUT"===a.method()?a.body()?"curl -X "+a.method()+" '"+a.urlAndFields()+"' -d '"+a.body()+"' -H 'Authorization: "+a.authorizationHeader()+"' -H 'Content-Type: "+a.bodyEncoding()+"'":"curl -X "+a.method()+" '"+a.url+"' -d '"+a.queryString()+"&oauth_signature="+a.signature()+"'":"curl -X DELETE '"+a.url+"?"+a.queryString()+"&oauth_signature="+a.signature()+"'"},percentEncode:function(a){return encodeURIComponent(a).replace(/\*/g,"%2A")}},b)}}}]).provider("oauth1Client",function(){function b(a,b){return decodeURIComponent((new RegExp("[?|&]?"+b+"=([^&;]+?)(&|#|;|$)").exec(a)||[,""])[1].replace(/\+/g,"%20"))||null}var c,d,e,f,g,h;this.config=function(a){c=a.consumerKey,d=a.consumerSecret,e=a.requestEndpoint,f=a.authorizeEndpoint,g=a.accessEndpoint,h=a.oauthCallback},"function"!=typeof String.prototype.startsWith&&(String.prototype.startsWith=function(a){return 0==this.indexOf(a)}),this.$get=["$q","$http","oauth1Signer","oauth1Headers","oauth1AuthorizedHttp","oauthPersistence",function(i,j,k,l,m,n){function o(a){return k.create(a)}function p(a,c){var d=i.defer();return j.get(a.signedUrl()).success(function(a,c,e,f){d.resolve({oauth_token:b(a,"oauth_token"),oauth_token_secret:b(a,"oauth_token_secret"),oauth_callback_confirmed:b(a,"oauth_callback_confirmed")})}).error(function(a,b,c,e){d.reject("Error: "+a)}),d.promise}function q(c,d){var e=i.defer(),g=a.open(f+"?oauth_token="+c+"&oauth_callback="+d,"_blank","location=no,clearcache=yes");return g.addEventListener("loadstart",function(a){a.url.startsWith(d)&&(g.close(),e.resolve({returned_oauth_token:b(a.url,"oauth_token"),oauth_verifier:b(a.url,"verifier"),wp_scope:b(a.url,"wp_scope")}))}),e.promise}function r(a){var c=i.defer();return j.post(a.signedUrl()).success(function(a,d,e,f){c.resolve({oauth_token:b(a,"oauth_token"),oauth_token_secret:b(a,"oauth_token_secret")})}).error(function(a,b,c,d){alert("Error: "+a)}),c.promise}function s(a,b){n.accessIsInStorage(a,b)}function t(a){n.getTokenAndSecret(function(b,f){var g=o({url:e,consumerKey:c,consumerSecret:d,token:b,tokenSecret:f});l.create(g),a(m.create(g))})}return{authorize:function(a){var b=i.defer();return s(function(){t(function(a){b.resolve(a)})},function(){var a=o({url:e,consumerKey:c,consumerSecret:d,callback:h});a.oauthParameters();p(a,h).then(function(a){return q(a.oauth_token,h)}).then(function(b){return a=o({url:g,consumerKey:c,token:b.returned_oauth_token,callback:h,verifier:b.oauth_verifier}),r(a)}).then(function(a){n.storeAccessToken(a).then(function(){t(function(a){b.resolve(a)})})},function(a){b.resolve({error:"error"})})}),b.promise}}}]}).service("oauth1AuthorizedHttp",["$http","$q",function(a,b){return{create:function(c){this.oauth1Signer=c;var d=this;return function(c){d.oauth1Signer.method=c.method||"GET",d.oauth1Signer.url=c.url,a.defaults.headers.common.Authorization="OAuth "+d.oauth1Signer.authorizationHeader(),a.defaults.headers.common["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";var e=b.defer();return a(c).success(function(a,b,c,d){e.resolve(a,b,c,d)}).error(function(a,b,c,d){e.reject(a,b,c,d)}),e.promise}}}}]).service("oauth1Headers",function(a){return{create:function(a){this.oauth1Signer=a},getHeaders:function(a,b){var c=this;return c.oauth1Signer?(c.oauth1Signer.method=b,c.oauth1Signer.url=a,{Authorization:"OAuth "+c.oauth1Signer.authorizationHeader(),"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}):{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}},removeAuthorizationHeader:function(){a.defaults.headers.common.Authorization=c}}})}(window,window.angular); \ No newline at end of file +!function(a,b,c){"use strict";b.module("oauth1Client",["LocalStorageModule"]).service("oauthPersistence",function(a,b){var c=this,d="oauth_token",e="oauth_token_secret";c.storeAccessToken=function(c){var f=b.defer();return a.set(d,c.oauth_token),a.set(e,c.oauth_token_secret),f.resolve(),f.promise},c.clearAccessToken=function(){a.remove(d),a.remove(e)},c.accessIsInStorage=function(b,c){a.get(d)&&a.get(e)?b():c()},c.getTokenAndSecret=function(b){b(a.get(d),a.get(e))}}).factory("oauth1Signer",[function(){function a(a){for(var b="",c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",d=0;a>d;d++)b+=c.charAt(Math.floor(Math.random()*c.length));return b}return{create:function(b){return _.extend({token:null,tokenSecret:"",version:"1.0",signatureMethod:"HMAC-SHA1",method:"GET",timestamp:Math.floor(Date.now()/1e3),nonce:a(32),callbackUrl:null,verifier:null,oauthParameters:function(){var a,b;return a=this,b={oauth_consumer_key:a.consumerKey,oauth_nonce:a.nonce,oauth_timestamp:a.timestamp,oauth_signature_method:a.signatureMethod},a.token&&(b.oauth_token=a.token),a.version&&(b.oauth_version=a.version),a.callbackUrl&&(b.oauth_callback=a.callbackUrl),a.verifier&&(b.oauth_verifier=a.verifier),b},queryStringFields:function(){var a,b,c;return a=this,b=a.oauthParameters(),c=a.fields,_.each(_.keys(c),function(a){return b[a]=c[a]}),b},queryString:function(){var a,b,c;a=this,b=a.queryStringFields(),c=_.keys(b).sort();var d=_.map(c,function(c){return c+"="+a.percentEncode(b[c])}).join("&");return d},urlEncoded:function(a){return _.map(_.keys(a),function(b){return b+"="+encodeURIComponent(a[b])}).join("&")},headerEncoded:function(a){return _.map(_.keys(a),function(b){return b+'="'+encodeURIComponent(a[b])+'"'}).join(", ")},urlEncodedFields:function(){var a;return a=this,a.urlEncoded(a.fields)},authorizationHeader:function(){var a,b;return a=this,b=a.oauthParameters(),b.oauth_signature=a.base64Signature(),a.headerEncoded(b)},urlAndFields:function(){var a,b;return a=this,b=a.urlEncodedFields(),b?a.url+"?"+b:a.url},parameterEncoded:function(a){var b=this,c=_.map(a,function(a){return b.percentEncode(a)}).join("&");return c},baseString:function(){var a;return a=this,a.parameterEncoded([a.method,a.url,a.queryString()])},hmacKey:function(){var a;return a=this,a.parameterEncoded([a.consumerSecret,a.tokenSecret])},hmac:function(a){var b,c;if(b=a&&a.hasOwnProperty("encoding")&&void 0!==a.encoding?a.encoding:"binary",c=this,"undefined"!=typeof process){var d,e;return d=require("crypto"),e=d.createHmac("sha1",c.hmacKey()),e.update(c.baseString()),e.digest(b)}var f;return f=CryptoJS.HmacSHA1(c.baseString(),c.hmacKey()),"base64"===b?f.toString(CryptoJS.enc.Base64):f},base64Signature:function(){var a;return a=this,a.hmac({encoding:"base64"})},signature:function(){var a;return a=this,a.percentEncode(a.base64Signature())},signedUrl:function(){var a;return a=this,a.url+"?"+a.queryString()+"&oauth_signature="+a.signature()},curl:function(){var a;return a=this,"GET"===a.method()?"curl '"+a.url+"?"+a.queryString()+"&oauth_signature="+a.signature()+"'":"POST"===a.method()||"PUT"===a.method()?a.body()?"curl -X "+a.method()+" '"+a.urlAndFields()+"' -d '"+a.body()+"' -H 'Authorization: "+a.authorizationHeader()+"' -H 'Content-Type: "+a.bodyEncoding()+"'":"curl -X "+a.method()+" '"+a.url+"' -d '"+a.queryString()+"&oauth_signature="+a.signature()+"'":"curl -X DELETE '"+a.url+"?"+a.queryString()+"&oauth_signature="+a.signature()+"'"},percentEncode:function(a){return encodeURIComponent(a).replace(/\*/g,"%2A")}},b)}}}]).provider("oauth1Client",function(){function c(a,b){return decodeURIComponent((new RegExp("[?|&]?"+b+"=([^&;]+?)(&|#|;|$)").exec(a)||[,""])[1].replace(/\+/g,"%20"))||null}var d,e,f,g,h,i;this.config=function(a){d=a.consumerKey,e=a.consumerSecret,f=a.requestEndpoint,g=a.authorizeEndpoint,h=a.accessEndpoint,i=a.oauthCallback},"function"!=typeof String.prototype.startsWith&&(String.prototype.startsWith=function(a){return 0==this.indexOf(a)}),this.$get=["$q","$http","oauth1Signer","oauth1Headers","oauth1AuthorizedHttp","oauthPersistence",function(j,k,l,m,n,o){function p(a){return l.create(a)}function q(a,b){var d=j.defer();return k.get(a.signedUrl()).success(function(a,b,e,f){d.resolve({oauth_token:c(a,"oauth_token"),oauth_token_secret:c(a,"oauth_token_secret"),oauth_callback_confirmed:c(a,"oauth_callback_confirmed")})}).error(function(a,b,c,e){d.reject("Error: "+a)}),d.promise}function r(d,e,f,h){var i=j.defer(),k=a.open(g+"?oauth_token="+d+"&oauth_callback="+e,"_blank","location=no,clearcache=yes"),l=0;return k.addEventListener("loadstart",function(a){l+=1,a.url.startsWith(e)&&(b.isFunction(h)&&h(),k.close(),i.resolve({returned_oauth_token:c(a.url,"oauth_token"),oauth_verifier:c(a.url,"verifier"),wp_scope:c(a.url,"wp_scope")})),1==l&&b.isFunction(f)&&f()}),i.promise}function s(a){var b=j.defer();return k.post(a.signedUrl()).success(function(a,d,e,f){b.resolve({oauth_token:c(a,"oauth_token"),oauth_token_secret:c(a,"oauth_token_secret")})}).error(function(a,b,c,d){alert("Error: "+a)}),b.promise}function t(a,b){var c=b.oauth_token,g=b.oauth_token_secret,h=p({url:f,consumerKey:d,consumerSecret:e,token:c,tokenSecret:g});m.create(h),a(n.create(h))}return{authorize:function(a,b){var c=j.defer(),g=p({url:f,consumerKey:d,consumerSecret:e,callback:i});g.oauthParameters();return q(g,i).then(function(c){return r(c.oauth_token,i,a,b)}).then(function(a){return g=p({url:h,consumerKey:d,token:a.returned_oauth_token,callback:i,verifier:a.oauth_verifier}),s(g)}).then(function(a){o.storeAccessToken(a).then(function(){t(function(a){c.resolve(a)},a)})},function(a){c.resolve({error:"error"})}),c.promise}}}]}).service("oauth1AuthorizedHttp",["$http","$q",function(a,b){return{create:function(c){this.oauth1Signer=c;var d=this;return function(c){d.oauth1Signer.method=c.method||"GET",d.oauth1Signer.url=c.url,a.defaults.headers.common.Authorization="OAuth "+d.oauth1Signer.authorizationHeader(),a.defaults.headers.common["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";var e=b.defer();return a(c).success(function(a,b,c,f){a.authToken=d.oauth1Signer.token,e.resolve(a,b,c,f)}).error(function(a,b,c,d){alert("here"),e.reject(a,b,c,d)}),e.promise}}}}]).service("oauth1Headers",function(a){return{create:function(a){this.oauth1Signer=a},getHeaders:function(a,b){var c=this;return c.oauth1Signer?(c.oauth1Signer.method=b,c.oauth1Signer.url=a,{Authorization:"OAuth "+c.oauth1Signer.authorizationHeader(),"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}):{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}},removeAuthorizationHeader:function(){a.defaults.headers.common.Authorization=c}}})}(window,window.angular); \ No newline at end of file