From 861833c01a7c535b8d152a9061d6d91b915b80fb Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Thu, 24 Feb 2022 18:01:17 +0800 Subject: [PATCH] refactor(core): add prefix for `password_encryption_method` (#281) --- packages/core/src/lib/user.test.ts | 4 ++-- packages/core/src/lib/user.ts | 6 +++--- packages/core/src/utils/password.ts | 6 +++--- packages/schemas/src/db-entries/custom-types.ts | 2 +- packages/schemas/src/db-entries/user.ts | 8 ++++---- packages/schemas/tables/users.sql | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/core/src/lib/user.test.ts b/packages/core/src/lib/user.test.ts index 3896139dd..34723dd5a 100644 --- a/packages/core/src/lib/user.test.ts +++ b/packages/core/src/lib/user.test.ts @@ -1,4 +1,4 @@ -import { PasswordEncryptionMethod } from '@logto/schemas'; +import { UsersPasswordEncryptionMethod } from '@logto/schemas'; import { hasUserWithId } from '@/queries/user'; @@ -56,7 +56,7 @@ describe('encryptUserPassword()', () => { it('generates salt, encrypted and method', () => { const { passwordEncryptionMethod, passwordEncrypted, passwordEncryptionSalt } = encryptUserPassword('user-id', 'password'); - expect(passwordEncryptionMethod).toEqual(PasswordEncryptionMethod.SaltAndPepper); + expect(passwordEncryptionMethod).toEqual(UsersPasswordEncryptionMethod.SaltAndPepper); expect(passwordEncrypted).toHaveLength(64); expect(passwordEncryptionSalt).toHaveLength(21); }); diff --git a/packages/core/src/lib/user.ts b/packages/core/src/lib/user.ts index d2f8ff29e..3b5349378 100644 --- a/packages/core/src/lib/user.ts +++ b/packages/core/src/lib/user.ts @@ -1,4 +1,4 @@ -import { PasswordEncryptionMethod, User } from '@logto/schemas'; +import { UsersPasswordEncryptionMethod, User } from '@logto/schemas'; import { nanoid } from 'nanoid'; import pRetry from 'p-retry'; @@ -29,10 +29,10 @@ export const encryptUserPassword = ( ): { passwordEncryptionSalt: string; passwordEncrypted: string; - passwordEncryptionMethod: PasswordEncryptionMethod; + passwordEncryptionMethod: UsersPasswordEncryptionMethod; } => { const passwordEncryptionSalt = nanoid(); - const passwordEncryptionMethod = PasswordEncryptionMethod.SaltAndPepper; + const passwordEncryptionMethod = UsersPasswordEncryptionMethod.SaltAndPepper; const passwordEncrypted = encryptPassword( userId, password, diff --git a/packages/core/src/utils/password.ts b/packages/core/src/utils/password.ts index b001081bd..9d513174c 100644 --- a/packages/core/src/utils/password.ts +++ b/packages/core/src/utils/password.ts @@ -1,6 +1,6 @@ import { createHash } from 'crypto'; -import { PasswordEncryptionMethod } from '@logto/schemas'; +import { UsersPasswordEncryptionMethod } from '@logto/schemas'; import { assertEnv, repeat } from '@silverhand/essentials'; import { nanoid } from 'nanoid'; import { number, string } from 'zod'; @@ -18,12 +18,12 @@ export const encryptPassword = ( id: string, password: string, salt: string, - method: PasswordEncryptionMethod + method: UsersPasswordEncryptionMethod ): string => { assertThat( // FIXME: // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - method === PasswordEncryptionMethod.SaltAndPepper, + method === UsersPasswordEncryptionMethod.SaltAndPepper, 'password.unsupported_encryption_method', { method } ); diff --git a/packages/schemas/src/db-entries/custom-types.ts b/packages/schemas/src/db-entries/custom-types.ts index 715b74291..a87dbc9f9 100644 --- a/packages/schemas/src/db-entries/custom-types.ts +++ b/packages/schemas/src/db-entries/custom-types.ts @@ -25,6 +25,6 @@ export enum UserLogResult { Success = 'Success', Failed = 'Failed', } -export enum PasswordEncryptionMethod { +export enum UsersPasswordEncryptionMethod { SaltAndPepper = 'SaltAndPepper', } diff --git a/packages/schemas/src/db-entries/user.ts b/packages/schemas/src/db-entries/user.ts index 80083b90a..8ab9a7a30 100644 --- a/packages/schemas/src/db-entries/user.ts +++ b/packages/schemas/src/db-entries/user.ts @@ -12,7 +12,7 @@ import { GeneratedSchema, Guard, } from '../foundations'; -import { PasswordEncryptionMethod } from './custom-types'; +import { UsersPasswordEncryptionMethod } from './custom-types'; export type CreateUser = { id: string; @@ -20,7 +20,7 @@ export type CreateUser = { primaryEmail?: string | null; primaryPhone?: string | null; passwordEncrypted?: string | null; - passwordEncryptionMethod?: PasswordEncryptionMethod | null; + passwordEncryptionMethod?: UsersPasswordEncryptionMethod | null; passwordEncryptionSalt?: string | null; name?: string | null; avatar?: string | null; @@ -35,7 +35,7 @@ export type User = { primaryEmail: string | null; primaryPhone: string | null; passwordEncrypted: string | null; - passwordEncryptionMethod: PasswordEncryptionMethod | null; + passwordEncryptionMethod: UsersPasswordEncryptionMethod | null; passwordEncryptionSalt: string | null; name: string | null; avatar: string | null; @@ -50,7 +50,7 @@ const createGuard: Guard = z.object({ primaryEmail: z.string().nullable().optional(), primaryPhone: z.string().nullable().optional(), passwordEncrypted: z.string().nullable().optional(), - passwordEncryptionMethod: z.nativeEnum(PasswordEncryptionMethod).nullable().optional(), + passwordEncryptionMethod: z.nativeEnum(UsersPasswordEncryptionMethod).nullable().optional(), passwordEncryptionSalt: z.string().nullable().optional(), name: z.string().nullable().optional(), avatar: z.string().nullable().optional(), diff --git a/packages/schemas/tables/users.sql b/packages/schemas/tables/users.sql index 20903cbd0..949f70f87 100644 --- a/packages/schemas/tables/users.sql +++ b/packages/schemas/tables/users.sql @@ -1,4 +1,4 @@ -create type password_encryption_method as enum ('SaltAndPepper'); +create type users_password_encryption_method as enum ('SaltAndPepper'); create table users ( id varchar(24) not null, @@ -6,7 +6,7 @@ create table users ( primary_email varchar(128) unique, primary_phone varchar(128) unique, password_encrypted varchar(128), - password_encryption_method password_encryption_method, + password_encryption_method users_password_encryption_method, password_encryption_salt varchar(128), name varchar(128), avatar varchar(256),