From f88c574f3f75c8727f175c9debabc5aebdff4d1d Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Tue, 12 Nov 2019 05:49:10 +0100 Subject: [PATCH] Improve HTTP header auth Signed-off-by: Michael Mayer --- frontend/src/common/session.js | 32 ++++++++++++++-------- frontend/src/pages/library/import.vue | 6 ++-- frontend/src/pages/library/index.vue | 6 ++-- frontend/src/pages/library/upload.vue | 8 +++--- frontend/tests/unit/common/session_test.js | 2 +- internal/api/albums.go | 15 ++++++++++ internal/api/albums_test.go | 16 +++++------ internal/api/import.go | 5 ++++ internal/api/labels.go | 10 +++++++ internal/api/photos.go | 10 +++++++ internal/api/session.go | 8 ++++-- internal/api/settings.go | 10 +++++++ internal/config/test.go | 2 ++ 13 files changed, 98 insertions(+), 32 deletions(-) diff --git a/frontend/src/common/session.js b/frontend/src/common/session.js index 2472ea047..9523f6a06 100644 --- a/frontend/src/common/session.js +++ b/frontend/src/common/session.js @@ -8,19 +8,18 @@ export default class Session { constructor(storage) { this.auth = false; - if(storage.getItem("session_storage") === "true") { + if (storage.getItem("session_storage") === "true") { this.storage = window.sessionStorage; } else { this.storage = storage; } - this.session_token = this.storage.getItem("session_token"); + if (this.applyToken(this.storage.getItem("session_token"))) { + const userJson = this.storage.getItem("user"); + this.user = userJson !== "undefined" ? new User(JSON.parse(userJson)) : null; + } - const userJson = this.storage.getItem("user"); - - this.user = userJson !== "undefined" ? new User(JSON.parse(userJson)) : null; - - if(this.isUser()) { + if (this.isUser()) { this.auth = true; } } @@ -36,10 +35,21 @@ export default class Session { this.storage = window.localStorage; } - setToken(token) { + applyToken(token) { + if (!token) { + this.deleteToken(); + return false; + } + this.session_token = token; - this.storage.setItem("session_token", token); Api.defaults.headers.common["X-Session-Token"] = token; + + return true; + } + + setToken(token) { + this.storage.setItem("session_token", token); + return this.applyToken(token); } getToken() { @@ -49,7 +59,7 @@ export default class Session { deleteToken() { this.session_token = null; this.storage.removeItem("session_token"); - Api.defaults.headers.common["X-Session-Token"] = ""; + delete Api.defaults.headers.common["X-Session-Token"]; this.deleteUser(); } @@ -108,7 +118,7 @@ export default class Session { login(email, password) { this.deleteToken(); - return Api.post("session", { email: email, password: password }).then( + return Api.post("session", {email: email, password: password}).then( (result) => { this.setToken(result.data.token); this.setUser(new User(result.data.user)); diff --git a/frontend/src/pages/library/import.vue b/frontend/src/pages/library/import.vue index 9e55d9737..062fff960 100644 --- a/frontend/src/pages/library/import.vue +++ b/frontend/src/pages/library/import.vue @@ -26,7 +26,7 @@