From 06c129733dad2aa3a78ea34b98188afbe4e5ab63 Mon Sep 17 00:00:00 2001 From: joamag Date: Tue, 17 Jul 2018 13:21:56 +0100 Subject: [PATCH] fixed issue with scope setting --- src/js/base/auth.js | 6 ++++-- src/js/ripe-min.js | 4 ++-- src/js/ripe.js | 6 ++++-- src/python/ripe_demo/static/js/ripe.js | 6 ++++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/js/base/auth.js b/src/js/base/auth.js index e6c6209b2..c3eadea80 100644 --- a/src/js/base/auth.js +++ b/src/js/base/auth.js @@ -85,9 +85,11 @@ ripe.Ripe.prototype.oauth = function(options, callback) { var clientId = options.clientId || localStorage.getItem("oauth_client_id"); var clientSecret = options.clientSecret || localStorage.getItem("oauth_client_secret"); var redirectUri = options.redirectUri || localStorage.getItem("oauth_redirect_uri"); - var scope = options.scope || localStorage.getItem("oauth_scope") || []; + var scope = options.scope || (localStorage.getItem("oauth_scope") || "").split(",") || []; var oauthToken = options.oauthToken || localStorage.getItem("oauth_token"); + scope && localStorage.setItem("oauth_scope", scope.join(",")); + if (oauthToken && clientId && clientSecret && redirectUri) { return this.oauthLogin( oauthToken, @@ -125,7 +127,7 @@ ripe.Ripe.prototype.oauth = function(options, callback) { function(result) { if (result) { localStorage.setItem("oauth_token", result.access_token); - localStorage.setItem("oauth_scope", result.scope); + result.scope && localStorage.setItem("oauth_scope", result.scope.join(",")); this.oauth(callback); } else { this.oauth( diff --git a/src/js/ripe-min.js b/src/js/ripe-min.js index 924dfac2c..d192edbc6 100644 --- a/src/js/ripe-min.js +++ b/src/js/ripe-min.js @@ -76,8 +76,8 @@ if(!localStorage.getItem("oauth_token")){return false;} return true;};ripe.Ripe.prototype.isOAuthCode=function(){var query=window.location.search||"";var unpacked=this._unpackQuery(query);var code=unpacked.code;return Boolean(code);};ripe.Ripe.prototype.isOAuthPending=function(){if(this.isAuth()){return false;} return this.isOAuth()||this.isOAuthCode();};ripe.Ripe.prototype.auth=function(username,password,options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;this.signin(username,password,options,function(result){this.sid=result.sid;this.trigger("auth");callback&&callback(result);}.bind(this));};ripe.Ripe.prototype.unauth=function(options,callback){this.sid=null;localStorage.removeItem("oauth_token");localStorage.removeItem("oauth_scope");localStorage.removeItem("oauth_client_id");localStorage.removeItem("oauth_client_secret");localStorage.removeItem("oauth_redirect_uri");this.trigger("unauth");callback&&callback();};ripe.Ripe.prototype.oauth=function(options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;options=options||{};if(!window.localStorage){throw new Error("No support for local storage available");} if(options.force){this.unauth();} -var query=window.location.search||"";var unpacked=this._unpackQuery(query);var code=typeof options.code==="undefined"?unpacked.code:options.code;var clientId=options.clientId||localStorage.getItem("oauth_client_id");var clientSecret=options.clientSecret||localStorage.getItem("oauth_client_secret");var redirectUri=options.redirectUri||localStorage.getItem("oauth_redirect_uri");var scope=options.scope||localStorage.getItem("oauth_scope")||[];var oauthToken=options.oauthToken||localStorage.getItem("oauth_token");if(oauthToken&&clientId&&clientSecret&&redirectUri){return this.oauthLogin(oauthToken,options,function(result){if(result){this.sid=result.sid;this.trigger("auth");callback&&callback(result);}else{this.oauth({clientId:clientId,clientSecret:clientSecret,redirectUri:redirectUri,scope:scope,code:null,force:true},callback);}}.bind(this));} -if(code&&clientId&&clientSecret&&redirectUri){return this.oauthAccessToken(code,{clientId:clientId,clientSecret:clientSecret,redirectUri:redirectUri},function(result){if(result){localStorage.setItem("oauth_token",result.access_token);localStorage.setItem("oauth_scope",result.scope);this.oauth(callback);}else{this.oauth({clientId:clientId,clientSecret:clientSecret,redirectUri:redirectUri,scope:scope,code:null,force:true},callback);}}.bind(this));} +var query=window.location.search||"";var unpacked=this._unpackQuery(query);var code=typeof options.code==="undefined"?unpacked.code:options.code;var clientId=options.clientId||localStorage.getItem("oauth_client_id");var clientSecret=options.clientSecret||localStorage.getItem("oauth_client_secret");var redirectUri=options.redirectUri||localStorage.getItem("oauth_redirect_uri");var scope=options.scope||(localStorage.getItem("oauth_scope")||"").split(",")||[];var oauthToken=options.oauthToken||localStorage.getItem("oauth_token");scope&&localStorage.setItem("oauth_scope",scope.join(","));if(oauthToken&&clientId&&clientSecret&&redirectUri){return this.oauthLogin(oauthToken,options,function(result){if(result){this.sid=result.sid;this.trigger("auth");callback&&callback(result);}else{this.oauth({clientId:clientId,clientSecret:clientSecret,redirectUri:redirectUri,scope:scope,code:null,force:true},callback);}}.bind(this));} +if(code&&clientId&&clientSecret&&redirectUri){return this.oauthAccessToken(code,{clientId:clientId,clientSecret:clientSecret,redirectUri:redirectUri},function(result){if(result){localStorage.setItem("oauth_token",result.access_token);result.scope&&localStorage.setItem("oauth_scope",result.scope.join(","));this.oauth(callback);}else{this.oauth({clientId:clientId,clientSecret:clientSecret,redirectUri:redirectUri,scope:scope,code:null,force:true},callback);}}.bind(this));} return this.oauthRedirect(options,callback);};ripe.Ripe.prototype.oauthRedirect=function(options,callback){var location=window.location;var currentUrl=location.protocol+"//"+location.host+"/"+location.pathname.split("/")[1];var clientId=options.clientId||this.clientId;var clientSecret=options.clientSecret||this.clientSecret;var redirectUri=options.redirectUri||currentUrl;localStorage.setItem("oauth_client_id",clientId);localStorage.setItem("oauth_client_secret",clientSecret);localStorage.setItem("oauth_redirect_uri",redirectUri);var url=this.webUrl+"admin/oauth/authorize";var params={client_id:clientId,redirect_uri:redirectUri,response_type:options.responseType||"code",scope:(options.scope||[]).join(" ")};var data=this._buildQuery(params);url=url+"?"+data;document.location=url;};if(typeof require!=="undefined"){var base=require("../base");var ripe=base.ripe;} ripe.Ripe.prototype.oauthAccessToken=function(code,options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;var url=this.url+"admin/oauth/access_token";options=Object.assign(options,{url:url,method:"POST",params:{code:code,client_id:options.clientId||this.clientId,client_secret:options.clientSecret||this.clientSecret,redirect_uri:options.redirectUri||this.redirectUri,grant_type:options.grantType||this.grantType||"authorization_code"}});options=this._build(options);return this._cacheURL(options.url,options,function(result){callback&&callback(result);});};ripe.Ripe.prototype.oauthLogin=function(accessToken,options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;var url=this.url+"admin/oauth/login";options=Object.assign(options,{url:url,method:"POST",params:{access_token:accessToken}});return this._cacheURL(options.url,options,function(result){callback&&callback(result);});};if(typeof require!=="undefined"){var base=require("../base");var ripe=base.ripe;} ripe.Ripe.prototype.getOrders=function(options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;var url=this.url+"orders";options=Object.assign(options,{url:url,method:"GET",auth:true});options=this._build(options);return this._cacheURL(options.url,options,function(result){callback&&callback(result);});};ripe.Ripe.prototype.getOrder=function(number,options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;var url=this.url+"orders/"+String(number);options=Object.assign(options,{url:url,method:"GET",auth:true});options=this._build(options);return this._cacheURL(options.url,options,function(result){callback&&callback(result);});};ripe.Ripe.prototype.createOrder=function(number,options,callback){return this.setOrderStatus(number,"create",options,callback);};ripe.Ripe.prototype.produceOrder=function(number,options,callback){return this.setOrderStatus(number,"produce",options,callback);};ripe.Ripe.prototype.readyOrder=function(number,options,callback){return this.setOrderStatus(number,"ready",options,callback);};ripe.Ripe.prototype.sendOrder=function(number,trackingNumber,trackingUrl,options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;options=Object.assign(options,{params:{tracking_number:trackingNumber,tracking_url:trackingUrl}});return this.setOrderStatus(number,"send",options,callback);};ripe.Ripe.prototype.receiveOrder=function(number,options,callback){return this.setOrderStatus(number,"receive",options,callback);};ripe.Ripe.prototype.returnOrder=function(number,options,callback){return this.setOrderStatus(number,"return",options,callback);};ripe.Ripe.prototype.cancelOrder=function(number,options,callback){return this.setOrderStatus(number,"cancel",options,callback);};ripe.Ripe.prototype.setOrderStatus=function(number,status,options,callback){callback=typeof options==="function"?options:callback;options=typeof options==="function"?{}:options;var url=this.url+"orders/"+String(number)+"/"+status;options=Object.assign(options,{url:url,auth:true,method:"PUT"});options=this._build(options);return this._cacheURL(options.url,options,function(result){callback&&callback(result);});};if(typeof require!=="undefined"){var base=require("../base");var ripe=base.ripe;} diff --git a/src/js/ripe.js b/src/js/ripe.js index 662f0f314..439e7e488 100644 --- a/src/js/ripe.js +++ b/src/js/ripe.js @@ -1295,9 +1295,11 @@ ripe.Ripe.prototype.oauth = function(options, callback) { var clientId = options.clientId || localStorage.getItem("oauth_client_id"); var clientSecret = options.clientSecret || localStorage.getItem("oauth_client_secret"); var redirectUri = options.redirectUri || localStorage.getItem("oauth_redirect_uri"); - var scope = options.scope || localStorage.getItem("oauth_scope") || []; + var scope = options.scope || (localStorage.getItem("oauth_scope") || "").split(",") || []; var oauthToken = options.oauthToken || localStorage.getItem("oauth_token"); + scope && localStorage.setItem("oauth_scope", scope.join(",")); + if (oauthToken && clientId && clientSecret && redirectUri) { return this.oauthLogin( oauthToken, @@ -1335,7 +1337,7 @@ ripe.Ripe.prototype.oauth = function(options, callback) { function(result) { if (result) { localStorage.setItem("oauth_token", result.access_token); - localStorage.setItem("oauth_scope", result.scope); + result.scope && localStorage.setItem("oauth_scope", result.scope.join(",")); this.oauth(callback); } else { this.oauth( diff --git a/src/python/ripe_demo/static/js/ripe.js b/src/python/ripe_demo/static/js/ripe.js index 662f0f314..439e7e488 100644 --- a/src/python/ripe_demo/static/js/ripe.js +++ b/src/python/ripe_demo/static/js/ripe.js @@ -1295,9 +1295,11 @@ ripe.Ripe.prototype.oauth = function(options, callback) { var clientId = options.clientId || localStorage.getItem("oauth_client_id"); var clientSecret = options.clientSecret || localStorage.getItem("oauth_client_secret"); var redirectUri = options.redirectUri || localStorage.getItem("oauth_redirect_uri"); - var scope = options.scope || localStorage.getItem("oauth_scope") || []; + var scope = options.scope || (localStorage.getItem("oauth_scope") || "").split(",") || []; var oauthToken = options.oauthToken || localStorage.getItem("oauth_token"); + scope && localStorage.setItem("oauth_scope", scope.join(",")); + if (oauthToken && clientId && clientSecret && redirectUri) { return this.oauthLogin( oauthToken, @@ -1335,7 +1337,7 @@ ripe.Ripe.prototype.oauth = function(options, callback) { function(result) { if (result) { localStorage.setItem("oauth_token", result.access_token); - localStorage.setItem("oauth_scope", result.scope); + result.scope && localStorage.setItem("oauth_scope", result.scope.join(",")); this.oauth(callback); } else { this.oauth(