Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
feat(crypto/api): AlwatrCryptoFactory with secret and device id
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename AlwatrUserGenerator to AlwatrCryptoFactory and change config
  • Loading branch information
njfamirm authored and alimd committed Dec 4, 2023
1 parent 52343ea commit 2d754a1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 26 deletions.
8 changes: 4 additions & 4 deletions demo/crypto/many-file.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
8 changes: 4 additions & 4 deletions demo/crypto/user.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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',
Expand Down
90 changes: 75 additions & 15 deletions packages/crypto/src/api.ts
Original file line number Diff line number Diff line change
@@ -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);
}

/**
Expand All @@ -26,7 +25,7 @@ export class AlwatrUserGenerator {
* @example
* ```typescript
* const newUser = {
* id: userFactory.generateUserId(),
* id: cryptoFactory.generateUserId(),
* ...
* }
* ```
Expand All @@ -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');
* }
* ```
Expand All @@ -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 {
Expand All @@ -70,12 +69,73 @@ 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');
* }
* ```
*/
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);
}
}
6 changes: 3 additions & 3 deletions packages/crypto/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface HashGeneratorConfig {
crcLength?: number;
}

export interface UserGeneratorConfig {
userId: HashGeneratorConfig;
token: TokenGeneratorConfig;
export interface CryptoFactoryConfig {
hashGeneratorConfig: HashGeneratorConfig;
tokenGeneratorConfig: TokenGeneratorConfig;
}

0 comments on commit 2d754a1

Please sign in to comment.