From bfcec8e9fc3ea9de740d5334a2d92dc8fbdeeecc Mon Sep 17 00:00:00 2001 From: Julien Leicher Date: Tue, 23 Apr 2024 14:00:50 +0200 Subject: [PATCH] feat: refresh API key, closes #47 --- api.http | 4 ++ cmd/serve/front/src/lib/localization/en.ts | 2 + cmd/serve/front/src/lib/localization/fr.ts | 3 ++ cmd/serve/front/src/lib/resources/users.ts | 7 ++- .../src/routes/(main)/profile/+page.svelte | 22 ++++++++- cmd/serve/server.go | 1 + cmd/serve/users.go | 23 +++++++++ .../app/refresh_api_key/refresh_api_key.go | 44 +++++++++++++++++ .../refresh_api_key/refresh_api_key_test.go | 48 +++++++++++++++++++ internal/auth/domain/user.go | 22 +++++++++ internal/auth/domain/user_test.go | 12 +++++ internal/auth/infra/memory/users.go | 8 ++++ internal/auth/infra/mod.go | 2 + internal/auth/infra/sqlite/users.go | 7 +++ 14 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 internal/auth/app/refresh_api_key/refresh_api_key.go create mode 100644 internal/auth/app/refresh_api_key/refresh_api_key_test.go diff --git a/api.http b/api.http index 15eb3b7b..62cb6899 100644 --- a/api.http +++ b/api.http @@ -33,6 +33,10 @@ Content-Type: application/json ### +PUT {{url}}/profile/key + +### + GET {{url}}/targets ### diff --git a/cmd/serve/front/src/lib/localization/en.ts b/cmd/serve/front/src/lib/localization/en.ts index 97289b6d..ed42baf2 100644 --- a/cmd/serve/front/src/lib/localization/en.ts +++ b/cmd/serve/front/src/lib/localization/en.ts @@ -141,6 +141,8 @@ You may reconsider and try to make the target reachable before deleting it.`, 'profile.key': 'API Key', 'profile.key.help': 'Pass this token as an Authorization: Bearer header to communicate with the seelf API. You MUST keep it secret!', + 'profile.key.refresh': 'Regenerate API Key', + 'profile.key.refresh.confirm': 'It will make the old key invalid. Do you confirm?', // Deployment 'deployment.new': 'New deployment', 'deployment.deploy': 'Deploy', diff --git a/cmd/serve/front/src/lib/localization/fr.ts b/cmd/serve/front/src/lib/localization/fr.ts index 32e0468d..ba3d393f 100644 --- a/cmd/serve/front/src/lib/localization/fr.ts +++ b/cmd/serve/front/src/lib/localization/fr.ts @@ -144,6 +144,9 @@ Vous devriez probablement essayer de rendre la cible accessible avant de la supp 'profile.key': 'Clé API', 'profile.key.help': "Passez ce jeton dans l'entête Authorization: Bearer pour communiquer avec l'API seelf. Vous devez le garder secret !", + 'profile.key.refresh': "Regénérer la clé d'API", + 'profile.key.refresh.confirm': + "L'ancienne clé ne sera plus valide. Confirmez-vous cette action ?", // Deployment 'deployment.new': 'Nouveau déploiement', 'deployment.deploy': 'Déployer', diff --git a/cmd/serve/front/src/lib/resources/users.ts b/cmd/serve/front/src/lib/resources/users.ts index 295ba4ac..146dbfa0 100644 --- a/cmd/serve/front/src/lib/resources/users.ts +++ b/cmd/serve/front/src/lib/resources/users.ts @@ -19,13 +19,18 @@ export type UpdateProfileData = { export interface UsersService { update(payload: UpdateProfileData): Promise; + refreshAPIKey(): Promise>; } export class RemoteUsersService implements UsersService { constructor(private readonly _fetcher: FetchService) {} update(payload: UpdateProfileData): Promise { - return this._fetcher.patch('/api/v1/profile', payload); + return this._fetcher.patch('/api/v1/profile', payload); + } + + refreshAPIKey(): Promise> { + return this._fetcher.put('/api/v1/profile/key'); } } diff --git a/cmd/serve/front/src/routes/(main)/profile/+page.svelte b/cmd/serve/front/src/routes/(main)/profile/+page.svelte index 2bcc362c..88cfba17 100644 --- a/cmd/serve/front/src/routes/(main)/profile/+page.svelte +++ b/cmd/serve/front/src/routes/(main)/profile/+page.svelte @@ -8,14 +8,16 @@ import Stack from '$components/stack.svelte'; import TextArea from '$components/text-area.svelte'; import TextInput from '$components/text-input.svelte'; + import Dropdown from '$components/dropdown.svelte'; + import { submitter } from '$lib/form.js'; import service from '$lib/resources/users'; import l from '$lib/localization'; - import Dropdown from '$components/dropdown.svelte'; export let data; let email = data.user.email; let password: Maybe; + let apiKey = data.user.api_key; let locale = l.locale(); const locales = l.locales().map((l) => ({ label: l.displayName, value: l.code })); @@ -27,6 +29,14 @@ password: password ? password : undefined }) .then(() => l.locale(locale)); + + const { + submit: refreshKey, + loading, + errors: refreshErr + } = submitter(() => service.refreshAPIKey().then((d) => (apiKey = d.api_key)), { + confirmation: l.translate('profile.key.refresh.confirm') + });
@@ -64,11 +74,19 @@ +