From 55ce44aebfda999741106f43ede3f6194831e0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Tue, 26 Sep 2023 12:15:44 +0200 Subject: [PATCH] Forbid to add the same account twice --- CHANGELOG.md | 1 + src/locales/en.json | 1 + src/locales/fr.json | 1 + src/views/AddAccountView.vue | 21 +++++++++++++++++---- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1a78e..5b0c522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Fixed - Invalid preferences are now automatically corrected +- It is now impossible to add the same account twice ## [0.3.0] - 2023-08-25 diff --git a/src/locales/en.json b/src/locales/en.json index 7c13534..802096b 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -35,6 +35,7 @@ "addAccount": "Add account", "cancel": "@:invariants.controls.cancel", "error": { + "accountAlreadyExists": "You already have an account on this domain that uses this local part.", "invalidBase64": "The key must be a valid base64 string.", "invalidKeyLength": "The key's length must be either 128 bits (16 bytes) or 256 bits (32 bytes).", "invalidSeparator": "The separator must be a single character.", diff --git a/src/locales/fr.json b/src/locales/fr.json index 125e2f2..b796013 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -35,6 +35,7 @@ "addAccount": "Ajouter", "cancel": "@:invariants.controls.cancel", "error": { + "accountAlreadyExists": "Vous avez déjà un compte sur ce nom de domaine qui utilise cette partie locale.", "invalidBase64": "La clé doit être une chaîne de caractère en base64.", "invalidKeyLength": "La longueur de la clé doit être de 128 bits (16 bytes) ou de 256 bits (32 bytes).", "invalidSeparator": "Le séparateur doit être un unique caractère.", diff --git a/src/views/AddAccountView.vue b/src/views/AddAccountView.vue index 3ae1b08..9570c01 100644 --- a/src/views/AddAccountView.vue +++ b/src/views/AddAccountView.vue @@ -51,11 +51,24 @@ const addAccount = () => { resetErrorMessage(); var hasError = false; var key = null; + var accountId = null; if (separator.value.length != 1) { hasError = setErrorMessage('addAccount.error.invalidSeparator', separatorErrorMessageId); } - if (localPart.value.includes(separator.value)) { - hasError = setErrorMessage('addAccount.error.localPartSeparator', localPartErrorMessageId); + try { + if (localPart.value.includes(separator.value)) { + throw new Error('addAccount.error.localPartSeparator'); + } + accountId = `${localPart.value}@${domainName.value}`; + for (const acc of accounts.value) { + const comp = `${acc.localPart}@${acc.domain}`; + if (accountId == comp) { + throw new Error('addAccount.error.accountAlreadyExists'); + } + } + } catch (e) { + console.log(e); + hasError = setErrorMessage(e.message, localPartErrorMessageId); } try { key = base64Decode(privateKey.value); @@ -65,8 +78,8 @@ const addAccount = () => { } catch (e) { hasError = setErrorMessage(e.message, addrKeyErrorMessageId); } - if (!hasError) { - const hash = sha256(`${localPart.value}@${domainName.value}`); + if (!hasError && key && accountId) { + const hash = sha256(accountId); const newAccount = { id: base32Encode(hash, 'RFC4648', { padding: false }).toLowerCase(), localPart: localPart.value,