diff --git a/backend/src/lib/crypto/srp.ts b/backend/src/lib/crypto/srp.ts index d8d21f5c1..e6afd0f99 100644 --- a/backend/src/lib/crypto/srp.ts +++ b/backend/src/lib/crypto/srp.ts @@ -5,6 +5,7 @@ import nacl from "tweetnacl"; import tweetnacl from "tweetnacl-util"; import { TUserEncryptionKeys } from "@app/db/schemas"; +import { UserEncryption } from "@app/services/user/user-types"; import { decryptSymmetric128BitHexKeyUTF8, encryptAsymmetric, encryptSymmetric } from "./encryption"; @@ -115,7 +116,7 @@ export const getUserPrivateKey = async ( | "encryptionVersion" > ) => { - if (user.encryptionVersion === 1) { + if (user.encryptionVersion === UserEncryption.V1) { return decryptSymmetric128BitHexKeyUTF8({ ciphertext: user.encryptedPrivateKey, iv: user.iv, @@ -123,7 +124,12 @@ export const getUserPrivateKey = async ( key: password.slice(0, 32).padStart(32 + (password.slice(0, 32).length - new Blob([password]).size), "0") }); } - if (user.encryptionVersion === 2 && user.protectedKey && user.protectedKeyIV && user.protectedKeyTag) { + if ( + user.encryptionVersion === UserEncryption.V2 && + user.protectedKey && + user.protectedKeyIV && + user.protectedKeyTag + ) { const derivedKey = await argon2.hash(password, { salt: Buffer.from(user.salt), memoryCost: 65536, diff --git a/backend/src/lib/validator/validate-email.ts b/backend/src/lib/validator/validate-email.ts index 99c09302d..c0c50155b 100644 --- a/backend/src/lib/validator/validate-email.ts +++ b/backend/src/lib/validator/validate-email.ts @@ -1,16 +1,16 @@ import fs from "fs/promises"; import path from "path"; -export const isDisposableEmail = async (email: string | string[]) => { +export const isDisposableEmail = async (emails: string | string[]) => { const disposableEmails = await fs.readFile(path.join(__dirname, "disposable_emails.txt"), "utf8"); - if (Array.isArray(email)) { - return email.some((el) => { - const emailDomain = el.split("@")[1]; + if (Array.isArray(emails)) { + return emails.some((email) => { + const emailDomain = email.split("@")[1]; return disposableEmails.split("\n").includes(emailDomain); }); } - const emailDomain = email.split("@")[1]; + const emailDomain = emails.split("@")[1]; if (disposableEmails.split("\n").includes(emailDomain)) return true; return false; }; diff --git a/backend/src/services/auth/auth-signup-service.ts b/backend/src/services/auth/auth-signup-service.ts index af78d7730..11c4bc61e 100644 --- a/backend/src/services/auth/auth-signup-service.ts +++ b/backend/src/services/auth/auth-signup-service.ts @@ -24,6 +24,7 @@ import { TProjectMembershipDALFactory } from "../project-membership/project-memb import { TProjectUserMembershipRoleDALFactory } from "../project-membership/project-user-membership-role-dal"; import { SmtpTemplates, TSmtpService } from "../smtp/smtp-service"; import { TUserDALFactory } from "../user/user-dal"; +import { UserEncryption } from "../user/user-types"; import { TAuthDALFactory } from "./auth-dal"; import { validateProviderAuthToken, validateSignUpAuthorization } from "./auth-fns"; import { TCompleteAccountInviteDTO, TCompleteAccountSignupDTO } from "./auth-signup-type"; @@ -174,7 +175,7 @@ export const authSignupServiceFactory = ({ encryptedPrivateKey, iv: encryptedPrivateKeyIV, tag: encryptedPrivateKeyTag, - encryptionVersion: 2 + encryptionVersion: UserEncryption.V2 }); const { tag, encoding, ciphertext, iv } = infisicalSymmetricEncypt(privateKey); const updateduser = await authDAL.transaction(async (tx) => { @@ -214,7 +215,7 @@ export const authSignupServiceFactory = ({ userEncKey = await userDAL.upsertUserEncryptionKey( us.id, { - encryptionVersion: 2, + encryptionVersion: UserEncryption.V2, protectedKey: encKeys.protectedKey, protectedKeyIV: encKeys.protectedKeyIV, protectedKeyTag: encKeys.protectedKeyTag, @@ -236,7 +237,7 @@ export const authSignupServiceFactory = ({ userEncKey = await userDAL.upsertUserEncryptionKey( us.id, { - encryptionVersion: 2, + encryptionVersion: UserEncryption.V2, salt, verifier, publicKey, @@ -452,7 +453,7 @@ export const authSignupServiceFactory = ({ userEncKey = await userDAL.upsertUserEncryptionKey( us.id, { - encryptionVersion: 2, + encryptionVersion: UserEncryption.V2, salt, verifier, publicKey, diff --git a/backend/src/services/user/user-fns.ts b/backend/src/services/user/user-fns.ts index 639320e24..23aff77a4 100644 --- a/backend/src/services/user/user-fns.ts +++ b/backend/src/services/user/user-fns.ts @@ -4,18 +4,14 @@ import { alphaNumericNanoId } from "@app/lib/nanoid"; import { TUserDALFactory } from "@app/services/user/user-dal"; export const normalizeUsername = async (username: string, userDAL: Pick) => { - let attempt = slugify(`${username}-${alphaNumericNanoId(4)}`); + let attempt: string; + let user; - let user = await userDAL.findOne({ username: attempt }); - if (!user) return attempt; - - while (true) { + do { attempt = slugify(`${username}-${alphaNumericNanoId(4)}`); // eslint-disable-next-line no-await-in-loop user = await userDAL.findOne({ username: attempt }); + } while (user); - if (!user) { - return attempt; - } - } + return attempt; }; diff --git a/backend/src/services/user/user-types.ts b/backend/src/services/user/user-types.ts index e69de29bb..f7b46978a 100644 --- a/backend/src/services/user/user-types.ts +++ b/backend/src/services/user/user-types.ts @@ -0,0 +1,4 @@ +export enum UserEncryption { + V1 = 1, + V2 = 2 +}