From 6d8bbc2417758f3613bf77766fc6ee3d570d8277 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Tue, 21 Oct 2014 02:21:01 -0500 Subject: [PATCH] Make API object propagate authentication failures Auto-logout when an API call gives a 403 --- www/jssrc/blerg/API.js | 48 +++++++++++++++++++++++++++++++++++----- www/jssrc/blerg/Blerg.js | 6 ++++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js index 19b6734..64b1108 100644 --- a/www/jssrc/blerg/API.js +++ b/www/jssrc/blerg/API.js @@ -76,21 +76,29 @@ enyo.kind({ }); req.go(); }, + expireClientAuthentication: function() { + blerg.API.loggedIn = false; + blerg.API.username = ''; + enyo.setCookie('auth', '', {"Max-Age": 0}); + }, logout: function() { var req = new enyo.Ajax({ url: baseURL + '/logout', method: 'POST' }); - var logout_func = function(inSender, inResponse) { - blerg.API.loggedIn = false; - blerg.API.username = ''; - enyo.setCookie('auth', '', {"Max-Age": 0}); + var logout_func = function() { + this.expireClientAuthentication(); this.bubble('onLogoutSuccessful'); - }; + }.bind(this); req.response(this, logout_func); req.error(this, logout_func); req.go(); }, + authenticationFailed: function() { + enyo.log("Authentication failed -- logging out"); + this.expireClientAuthentication(); + this.bubble('onAuthFailure'); + }, changePassword: function(oldpassword, newpassword) { var req = new enyo.Ajax({ url: baseURL + '/passwd', @@ -186,6 +194,11 @@ enyo.kind({ req.response(this, function(inSender, inResponse) { this.bubble('onStatus', inResponse); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, clearStatus: function(type) { @@ -206,6 +219,11 @@ enyo.kind({ inResponse.type = type; this.bubble('onClearStatus', inResponse); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, loadFeed: function() { @@ -221,6 +239,11 @@ enyo.kind({ entries: inResponse }); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, getSubscriptionStatus: function(username) { @@ -233,6 +256,11 @@ enyo.kind({ subscribed: inResponse.subscribed }); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, subscription: function(username, v) { @@ -250,6 +278,11 @@ enyo.kind({ subscribed: inResponse.status == "success" && subv }); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, subscribe: function(username) { @@ -279,6 +312,11 @@ enyo.kind({ }); } }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); } }); diff --git a/www/jssrc/blerg/Blerg.js b/www/jssrc/blerg/Blerg.js index 361b0c9..80037c4 100644 --- a/www/jssrc/blerg/Blerg.js +++ b/www/jssrc/blerg/Blerg.js @@ -13,7 +13,8 @@ enyo.kind({ onSetTitle: "setTitle", onPostVisibility: "postVisibilityUpdate", onReload: "sendReload", - onShowChangePassword: "showChangePassword" + onShowChangePassword: "showChangePassword", + onAuthFailure: "authFailure" }, components: [ {classes: "blerg-header", components: [ @@ -99,5 +100,8 @@ enyo.kind({ }, showChangePassword: function() { this.$.passwdDialog.show(); + }, + authFailure: function(inSender, inEvent) { + this.logout(); } }); -- 2.25.1