diff --git a/demo/crypto/many-file.ts b/demo/crypto/many-file.ts index 6e2d660..11b0c46 100644 --- a/demo/crypto/many-file.ts +++ b/demo/crypto/many-file.ts @@ -1,9 +1,9 @@ -import {AlwatrUserGenerator, userIdGeneratorPreConfig, userTokenGeneratorPreConfig} from '@alwatr/crypto'; +import {AlwatrCryptoFactory, userIdGeneratorPreConfig, userTokenGeneratorPreConfig} from '@alwatr/crypto'; import {writeJsonFile} from '@alwatr/util/node.js'; -const userFactory = new AlwatrUserGenerator({ - userId: userIdGeneratorPreConfig, - token: { +const userFactory = new AlwatrCryptoFactory({ + hashGeneratorConfig: userIdGeneratorPreConfig, + tokenGeneratorConfig: { ...userTokenGeneratorPreConfig, secret: 'my-very-secret-key', duration: '1y', diff --git a/demo/crypto/user.ts b/demo/crypto/user.ts index c8ed361..b311ab1 100644 --- a/demo/crypto/user.ts +++ b/demo/crypto/user.ts @@ -1,4 +1,4 @@ -import {AlwatrUserGenerator, userIdGeneratorPreConfig, userTokenGeneratorPreConfig} from '@alwatr/crypto'; +import {AlwatrCryptoFactory, userIdGeneratorPreConfig, userTokenGeneratorPreConfig} from '@alwatr/crypto'; import {createLogger} from '@alwatr/logger'; import {delay} from '@alwatr/util'; @@ -7,9 +7,9 @@ import type {User} from '@alwatr/type'; const logger = createLogger('crypto/user', true); const newLocal = 'my-very-secret-key'; -const userGenerator = new AlwatrUserGenerator({ - userId: userIdGeneratorPreConfig, - token: { +const userGenerator = new AlwatrCryptoFactory({ + hashGeneratorConfig: userIdGeneratorPreConfig, + tokenGeneratorConfig: { ...userTokenGeneratorPreConfig, secret: newLocal, duration: '2s', diff --git a/packages/crypto/src/api.ts b/packages/crypto/src/api.ts index 13508d2..795fb81 100644 --- a/packages/crypto/src/api.ts +++ b/packages/crypto/src/api.ts @@ -1,23 +1,22 @@ -import { AlwatrHashGenerator } from './hash.js'; -import { AlwatrTokenGenerator } from './token.js'; +import {AlwatrHashGenerator} from './hash.js'; +import {AlwatrTokenGenerator} from './token.js'; -import type {UserGeneratorConfig, TokenStatus } from './type.js'; +import type {CryptoFactoryConfig, TokenStatus} from './type.js'; /** - * User factory for generating self-validate user-id and user-token. + * Crypto factory for generating self-validate user-id, user-token, secret, device-id. */ -export class AlwatrUserGenerator { +export class AlwatrCryptoFactory { protected _tokenGenerator: AlwatrTokenGenerator; protected _hashGenerator: AlwatrHashGenerator; /** - * Creates a new instance of AlwatrUserFactory. - * @param hashConfig The configuration for the hash generator. - * @param tokenConfig The configuration for the token generator. + * Creates a new instance of crypto factory. + * @param config The configuration used to create the crypto factory. */ - constructor(config: UserGeneratorConfig) { - this._hashGenerator = new AlwatrHashGenerator(config.userId); - this._tokenGenerator = new AlwatrTokenGenerator(config.token); + constructor(config: CryptoFactoryConfig) { + this._hashGenerator = new AlwatrHashGenerator(config.hashGeneratorConfig); + this._tokenGenerator = new AlwatrTokenGenerator(config.tokenGeneratorConfig); } /** @@ -26,7 +25,7 @@ export class AlwatrUserGenerator { * @example * ```typescript * const newUser = { - * id: userFactory.generateUserId(), + * id: cryptoFactory.generateUserId(), * ... * } * ``` @@ -41,7 +40,7 @@ export class AlwatrUserGenerator { * @returns A boolean indicating whether the user ID is valid. * @example * ```typescript - * if (!userFactory.verifyUserId(user.id)) { + * if (!cryptoFactory.verifyUserId(user.id)) { * throw new Error('invalid_user'); * } * ``` @@ -56,7 +55,7 @@ export class AlwatrUserGenerator { * @returns The generated user token. * @example * ```typescript - * const userToken = userFactory.generateToken([user.id, user.lpe]); + * const userToken = cryptoFactory.generateToken([user.id, user.lpe]); * ``` */ generateToken(uniquelyList: (string | number | boolean)[]): string { @@ -70,7 +69,7 @@ export class AlwatrUserGenerator { * @returns The status of the token verification. * @example * ```typescript - * if (!userFactory.verifyToken([user.id, user.lpe], userToken)) { + * if (!cryptoFactory.verifyToken([user.id, user.lpe], userToken)) { * throw new Error('invalid_token'); * } * ``` @@ -78,4 +77,65 @@ export class AlwatrUserGenerator { verifyToken(uniquelyList: (string | number | boolean)[], token: string): TokenStatus { return this._tokenGenerator.verify(uniquelyList.join(), token); } + + /** + * Generates a new self-verifiable secret. + * @returns The generated secret. + * @example + * ```typescript + * const config = { + * storageToken: cryptoFactory.generateSecret(), + * ... + * } + * ``` + */ + generateSecret(): string { + return this._hashGenerator.generateRandomSelfValidate(); + } + + /** + * Validates a user ID without token. + * @param secret The user ID to verify. + * @returns A boolean indicating whether the user ID is valid. + * @example + * ```typescript + * if (!cryptoFactory.verifySecret(config.storageToken)) { + * throw new Error('invalid_secret'); + * } + * ``` + */ + verifySecret(secret: string): boolean { + return this._hashGenerator.verifySelfValidate(secret); + } + + /** + * Generates a new self-verifiable device ID. + * @returns The generated device ID. + * @example + * ```typescript + * const deviceId = deviceFactory.generateDeviceId(); + * ``` + */ + generateDeviceId(): string { + return this._hashGenerator.generateRandomSelfValidate(); + } + + /** + * Validates a device ID. + * @param deviceId The device ID to verify. + * @returns A boolean indicating whether the device ID is valid. + * @example + * ```typescript + * if (!deviceFactory.verifyDeviceId(bodyJson.deviceId)) { + * throw { + * ok: false, + * status: 400, + * error: 'invalid_device_id', + * } + * } + * ``` + */ + verifyDeviceId(deviceId: string): boolean { + return this._hashGenerator.verifySelfValidate(deviceId); + } } diff --git a/packages/crypto/src/type.ts b/packages/crypto/src/type.ts index 2b3f84b..1046213 100644 --- a/packages/crypto/src/type.ts +++ b/packages/crypto/src/type.ts @@ -51,7 +51,7 @@ export interface HashGeneratorConfig { crcLength?: number; } -export interface UserGeneratorConfig { - userId: HashGeneratorConfig; - token: TokenGeneratorConfig; +export interface CryptoFactoryConfig { + hashGeneratorConfig: HashGeneratorConfig; + tokenGeneratorConfig: TokenGeneratorConfig; }