From a02ecf12def5163ed53c46ca4acc4a8dfaa4cd17 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 24 Oct 2022 09:21:39 +0200 Subject: [PATCH] Auth: Use "username" instead of "name" in the session API #98 #2796 Signed-off-by: Michael Mayer --- frontend/package-lock.json | 48 +++++++++---------- frontend/src/common/session.js | 4 +- frontend/src/page/login.vue | 16 +++---- .../acceptance-auth/authentication.js | 14 +++--- .../acceptance/acceptance-auth/components.js | 2 +- frontend/tests/acceptance/page-model/page.js | 4 +- go.mod | 2 +- go.sum | 4 +- internal/api/auth_session_test.go | 14 +++--- internal/form/user_login.go | 2 +- 10 files changed, 56 insertions(+), 54 deletions(-) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 2c5156af1..7de2d020e 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -2350,9 +2350,9 @@ } }, "node_modules/@types/node": { - "version": "18.11.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", - "integrity": "sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==" + "version": "18.11.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.4.tgz", + "integrity": "sha512-BxcJpBu8D3kv/GZkx/gSMz6VnTJREBj/4lbzYOQueUOELkt8WrO6zAcSPmp9uRPEW/d+lUO8QK0W2xnS1hEU0A==" }, "node_modules/@types/parse-json": { "version": "4.0.0", @@ -3903,9 +3903,9 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, "node_modules/core-js": { - "version": "3.25.5", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.25.5.tgz", - "integrity": "sha512-nbm6eZSjm+ZuBQxCUPQKQCoUEfFOXjUZ8dTTyikyKaWrTYmAVbykQfwsKE5dBK88u3QCkCrzsx/PPlKfhsvgpw==", + "version": "3.26.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.26.0.tgz", + "integrity": "sha512-+DkDrhoR4Y0PxDz6rurahuB+I45OsEUv8E1maPTB6OuHRohMMcznBq9TMpdpDMm/hUPob/mJJS3PqgbHpMTQgw==", "hasInstallScript": true, "funding": { "type": "opencollective", @@ -3913,9 +3913,9 @@ } }, "node_modules/core-js-compat": { - "version": "3.25.5", - "resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.25.5.tgz", - "integrity": "sha512-ovcyhs2DEBUIE0MGEKHP4olCUW/XYte3Vroyxuh38rD1wAO4dHohsovUC4eAOuzFxE6b+RXvBU3UZ9o0YhUTkA==", + "version": "3.26.0", + "resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.26.0.tgz", + "integrity": "sha512-piOX9Go+Z4f9ZiBFLnZ5VrOpBl0h7IGCkiFUN11QTe6LjAvOT3ifL/5TdoizMh99hcGy5SoLyWbapIY/PIb/3A==", "dependencies": { "browserslist": "^4.21.4" }, @@ -12147,9 +12147,9 @@ } }, "node_modules/uglify-js": { - "version": "3.17.3", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.3.tgz", - "integrity": "sha512-JmMFDME3iufZnBpyKL+uS78LRiC+mK55zWfM5f/pWBJfpOttXAqYfdDGRukYhJuyRinvPVAtUhvy7rlDybNtFg==", + "version": "3.17.4", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", + "integrity": "sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==", "optional": true, "bin": { "uglifyjs": "bin/uglifyjs" @@ -14827,9 +14827,9 @@ } }, "@types/node": { - "version": "18.11.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", - "integrity": "sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==" + "version": "18.11.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.4.tgz", + "integrity": "sha512-BxcJpBu8D3kv/GZkx/gSMz6VnTJREBj/4lbzYOQueUOELkt8WrO6zAcSPmp9uRPEW/d+lUO8QK0W2xnS1hEU0A==" }, "@types/parse-json": { "version": "4.0.0", @@ -16036,14 +16036,14 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, "core-js": { - "version": "3.25.5", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.25.5.tgz", - "integrity": "sha512-nbm6eZSjm+ZuBQxCUPQKQCoUEfFOXjUZ8dTTyikyKaWrTYmAVbykQfwsKE5dBK88u3QCkCrzsx/PPlKfhsvgpw==" + "version": "3.26.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.26.0.tgz", + "integrity": "sha512-+DkDrhoR4Y0PxDz6rurahuB+I45OsEUv8E1maPTB6OuHRohMMcznBq9TMpdpDMm/hUPob/mJJS3PqgbHpMTQgw==" }, "core-js-compat": { - "version": "3.25.5", - "resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.25.5.tgz", - "integrity": "sha512-ovcyhs2DEBUIE0MGEKHP4olCUW/XYte3Vroyxuh38rD1wAO4dHohsovUC4eAOuzFxE6b+RXvBU3UZ9o0YhUTkA==", + "version": "3.26.0", + "resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.26.0.tgz", + "integrity": "sha512-piOX9Go+Z4f9ZiBFLnZ5VrOpBl0h7IGCkiFUN11QTe6LjAvOT3ifL/5TdoizMh99hcGy5SoLyWbapIY/PIb/3A==", "requires": { "browserslist": "^4.21.4" } @@ -21848,9 +21848,9 @@ "integrity": "sha512-f9BESNVhzlhEFf2CHMSj40NWOjYPl1YKYbrvIr/hFTDEmLq7SRbWvm7FcdcpCYT95zrOhC7gZSxjdnnTpBcwVw==" }, "uglify-js": { - "version": "3.17.3", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.3.tgz", - "integrity": "sha512-JmMFDME3iufZnBpyKL+uS78LRiC+mK55zWfM5f/pWBJfpOttXAqYfdDGRukYhJuyRinvPVAtUhvy7rlDybNtFg==", + "version": "3.17.4", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", + "integrity": "sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==", "optional": true }, "uid-safe": { diff --git a/frontend/src/common/session.js b/frontend/src/common/session.js index a03b01316..dd62c6899 100644 --- a/frontend/src/common/session.js +++ b/frontend/src/common/session.js @@ -294,10 +294,10 @@ export default class Session { return LoginPage === window.location.href.substring(window.location.href.lastIndexOf("/") + 1); } - login(name, password, token) { + login(username, password, token) { this.deleteId(); - return Api.post("session", { name, password, token }).then((resp) => { + return Api.post("session", { username, password, token }).then((resp) => { const reload = this.config.getLanguage() !== resp.data?.config?.settings?.ui?.language; this.setResp(resp); this.sendClientInfo(); diff --git a/frontend/src/page/login.vue b/frontend/src/page/login.vue index d58a11ec1..4be52ab0a 100644 --- a/frontend/src/page/login.vue +++ b/frontend/src/page/login.vue @@ -10,16 +10,16 @@ { window.location = route.href; }, 100); }, login() { - const name = this.name.trim(); + const username = this.username.trim(); const password = this.password.trim(); - if (name === "" || password === "") { + if (username === "" || password === "") { return; } this.loading = true; - this.$session.login(name, password).then( + this.$session.login(username, password).then( () => { this.load(); } diff --git a/frontend/tests/acceptance/acceptance-auth/authentication.js b/frontend/tests/acceptance/acceptance-auth/authentication.js index 236bcbec9..1bbdd5a22 100644 --- a/frontend/tests/acceptance/acceptance-auth/authentication.js +++ b/frontend/tests/acceptance/acceptance-auth/authentication.js @@ -18,12 +18,12 @@ test.meta("testID", "authentication-001").meta({ type: "short", mode: "auth" })( await t.navigateTo("/library/browse"); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); - await t.typeText(page.nameInput, "admin", { replace: true }); + await t.typeText(page.usernameInput, "admin", { replace: true }); await t.expect(page.loginAction.hasAttribute("disabled", "disabled")).ok(); @@ -46,14 +46,14 @@ test.meta("testID", "authentication-001").meta({ type: "short", mode: "auth" })( await page.logout(); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); await t.navigateTo("/library/settings"); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); @@ -67,7 +67,7 @@ test.meta("testID", "authentication-002").meta({ type: "short", mode: "auth" })( await t.navigateTo("/library/favorites"); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); @@ -76,7 +76,7 @@ test.meta("testID", "authentication-002").meta({ type: "short", mode: "auth" })( await t.navigateTo("/library/archive"); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); @@ -130,7 +130,7 @@ test.meta("testID", "authentication-003").meta({ type: "short", mode: "auth" })( await t.navigateTo("/library/archive"); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); diff --git a/frontend/tests/acceptance/acceptance-auth/components.js b/frontend/tests/acceptance/acceptance-auth/components.js index 19fbd086a..ab3c65a0d 100644 --- a/frontend/tests/acceptance/acceptance-auth/components.js +++ b/frontend/tests/acceptance/acceptance-auth/components.js @@ -37,7 +37,7 @@ test.meta("testID", "components-001").meta({ mode: "auth" })( await toolbar.triggerMobileMenuAction("logout"); await t - .expect(page.nameInput.visible) + .expect(page.usernameInput.visible) .ok() .expect(Selector(".input-search input").visible) .notOk(); diff --git a/frontend/tests/acceptance/page-model/page.js b/frontend/tests/acceptance/page-model/page.js index adbd4ddb9..c1db7efb6 100644 --- a/frontend/tests/acceptance/page-model/page.js +++ b/frontend/tests/acceptance/page-model/page.js @@ -23,7 +23,7 @@ export default class Page { this.cardTitle = Selector("button.action-title-edit", { timeout: 7000 }); this.cardDescription = Selector('div[title="Description"]', { timeout: 7000 }); this.cardLocation = Selector("button.action-location", { timeout: 7000 }); - this.nameInput = Selector(".input-name input", { timeout: 7000 }); + this.usernameInput = Selector(".input-username input", { timeout: 7000 }); this.passwordInput = Selector(".input-password input", { timeout: 7000 }); this.togglePasswordMode = Selector(".v-input__icon--append", { timeout: 7000 }); this.loginAction = Selector(".action-confirm", { timeout: 7000 }); @@ -31,7 +31,7 @@ export default class Page { async login(username, password) { await t - .typeText(Selector(".input-name input"), username, { replace: true, timeout: 5000 }) + .typeText(Selector(".input-username input"), username, { replace: true, timeout: 5000 }) .typeText(Selector(".input-password input"), password, { replace: true }) .click(Selector(".action-confirm")); } diff --git a/go.mod b/go.mod index d8b0b7bbf..183558d8f 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 github.com/sevlyar/go-daemon v0.1.6 github.com/sirupsen/logrus v1.9.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.1 github.com/studio-b12/gowebdav v0.0.0-20211106090535-29e74efa701f github.com/tensorflow/tensorflow v1.15.2 github.com/tidwall/gjson v1.14.3 diff --git a/go.sum b/go.sum index fa244c169..fa7070db8 100644 --- a/go.sum +++ b/go.sum @@ -910,6 +910,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -918,8 +919,9 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/studio-b12/gowebdav v0.0.0-20211106090535-29e74efa701f h1:SLJx0nHhb2ZLlYNMAbrYsjwmVwXx4yRT48lNIxOp7ts= github.com/studio-b12/gowebdav v0.0.0-20211106090535-29e74efa701f/go.mod h1:gCcfDlA1Y7GqOaeEKw5l9dOGx1VLdc/HuQSlQAaZ30s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= diff --git a/internal/api/auth_session_test.go b/internal/api/auth_session_test.go index 61f7a6c6f..30136778e 100644 --- a/internal/api/auth_session_test.go +++ b/internal/api/auth_session_test.go @@ -34,7 +34,7 @@ func TestCreateSession(t *testing.T) { defer conf.SetAuthMode(config.AuthModePublic) CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": "admin", "password": "photoprism"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "admin", "password": "photoprism"}`) log.Debugf("BODY: %s", r.Body.String()) val2 := gjson.Get(r.Body.String(), "user.Name") assert.Equal(t, "admin", val2.String()) @@ -46,7 +46,7 @@ func TestCreateSession(t *testing.T) { defer conf.SetAuthMode(config.AuthModePublic) CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": 123, "password": "xxx"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": 123, "password": "xxx"}`) assert.Equal(t, http.StatusBadRequest, r.Code) }) t.Run("PublicInvalidToken", func(t *testing.T) { @@ -55,7 +55,7 @@ func TestCreateSession(t *testing.T) { defer conf.SetAuthMode(config.AuthModePublic) CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": "admin", "password": "photoprism", "token": "xxx"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "admin", "password": "photoprism", "token": "xxx"}`) assert.Equal(t, http.StatusNotFound, r.Code) }) t.Run("AdminInvalidToken", func(t *testing.T) { @@ -89,7 +89,7 @@ func TestCreateSession(t *testing.T) { t.Run("PublicValidToken", func(t *testing.T) { app, router, _ := NewApiTest() CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": "admin", "password": "photoprism", "token": "1jxf3jfn2k"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "admin", "password": "photoprism", "token": "1jxf3jfn2k"}`) assert.Equal(t, http.StatusOK, r.Code) }) t.Run("AdminInvalidPassword", func(t *testing.T) { @@ -115,7 +115,7 @@ func TestCreateSession(t *testing.T) { CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": "alice", "password": "Alice123!"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "alice", "password": "Alice123!"}`) userEmail := gjson.Get(r.Body.String(), "user.Email") userName := gjson.Get(r.Body.String(), "user.Name") assert.Equal(t, "alice@example.com", userEmail.String()) @@ -128,7 +128,7 @@ func TestCreateSession(t *testing.T) { defer conf.SetAuthMode(config.AuthModePublic) CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": "bob", "password": "Bobbob123!"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "bob", "password": "Bobbob123!"}`) userEmail := gjson.Get(r.Body.String(), "user.Email") userName := gjson.Get(r.Body.String(), "user.Name") assert.Equal(t, "bob@example.com", userEmail.String()) @@ -141,7 +141,7 @@ func TestCreateSession(t *testing.T) { defer conf.SetAuthMode(config.AuthModePublic) CreateSession(router) - r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"name": "bob", "password": "helloworld"}`) + r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "bob", "password": "helloworld"}`) val := gjson.Get(r.Body.String(), "error") assert.Equal(t, i18n.Msg(i18n.ErrInvalidCredentials), val.String()) assert.Equal(t, http.StatusUnauthorized, r.Code) diff --git a/internal/form/user_login.go b/internal/form/user_login.go index 8713b65cd..ffe547dea 100644 --- a/internal/form/user_login.go +++ b/internal/form/user_login.go @@ -6,7 +6,7 @@ import ( // Login represents a login form. type Login struct { - UserName string `json:"name,omitempty"` + UserName string `json:"username,omitempty"` UserEmail string `json:"email,omitempty"` Password string `json:"password,omitempty"` AuthToken string `json:"token,omitempty"`