From 0ef465d679616119ac1d5b15488d16957cb111a8 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 15:07:51 +0330 Subject: [PATCH 01/24] feat(token/user): base userFactory --- core/token/src/type.ts | 4 +++- core/token/src/user.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 core/token/src/user.ts diff --git a/core/token/src/type.ts b/core/token/src/type.ts index 62c3e83fb..618c35394 100644 --- a/core/token/src/type.ts +++ b/core/token/src/type.ts @@ -4,7 +4,7 @@ export type DigestAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | export type TokenStatus = 'valid' | 'invalid' | 'expired'; -export type TokenGeneratorConfig = { +export interface TokenGeneratorConfig { /** * Secret string data to generate token. */ @@ -27,3 +27,5 @@ export type TokenGeneratorConfig = { */ encoding: 'base64' | 'base64url' | 'hex' | 'binary'; }; + +export interface UserFactoryConfig extends TokenGeneratorConfig {} diff --git a/core/token/src/user.ts b/core/token/src/user.ts new file mode 100644 index 000000000..839d514a3 --- /dev/null +++ b/core/token/src/user.ts @@ -0,0 +1,36 @@ +import {createLogger} from '@alwatr/logger'; + +import type {UserFactoryConfig} from './type.js'; +import { AlwatrTokenGenerator } from './token.js'; + +export class AlwatrUserFactory { + protected _logger = createLogger('alwatr-user-factory'); + + private _$passwordTokengenerator = new AlwatrTokenGenerator(this.config) + private _$userTokengenerator = new AlwatrTokenGenerator({ + algorithm: 'sha224', + duration: '6m', + encoding: 'base64url', + secret: '' + }) + + constructor(public config: UserFactoryConfig) { + this._logger.logMethodArgs?.('constructor', config); + } + + generateId(): string { + + } + verifyId(): boolean { + + } + + generateToken(): string { + + } + verifyToken(): boolean { + + } + + +} From 0ec8473fb5ec636093e3fb09e2115ce356dee7b1 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 16:23:27 +0330 Subject: [PATCH 02/24] feat(math): generateHash --- core/math/package.json | 1 + core/math/src/crypto.ts | 12 ++++++++++++ core/math/tsconfig.json | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 core/math/src/crypto.ts diff --git a/core/math/package.json b/core/math/package.json index 5a6327215..e9988b80b 100644 --- a/core/math/package.json +++ b/core/math/package.json @@ -31,6 +31,7 @@ }, "dependencies": { "@alwatr/logger": "^0.30.0", + "@alwatr/type": "^0.30.0", "tslib": "^2.5.0" } } diff --git a/core/math/src/crypto.ts b/core/math/src/crypto.ts new file mode 100644 index 000000000..fef65ed20 --- /dev/null +++ b/core/math/src/crypto.ts @@ -0,0 +1,12 @@ +import {createHash} from 'node:crypto'; + +import type {CryptoAlgorithm, CryptoEncoding} from '@alwatr/type'; + +export const generateHash = ( + data: string, + options: {algorithm: CryptoAlgorithm; encoding: CryptoEncoding; outputLength: number}, +): string => { + return createHash(options.algorithm, {outputLength: options.outputLength}) + .update(data, 'utf8') + .digest(options.encoding); +}; diff --git a/core/math/tsconfig.json b/core/math/tsconfig.json index 1f9b9b7c4..5689e0024 100644 --- a/core/math/tsconfig.json +++ b/core/math/tsconfig.json @@ -10,6 +10,7 @@ "include": ["src/**/*.ts"], "exclude": [], "references": [ - {"path": "../logger"} + {"path": "../logger"}, + {"path": "../type"} ] } From 065f825e8d7baf2407696f3b9cff5e1aa178b436 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 16:23:45 +0330 Subject: [PATCH 03/24] feat(type): crypto --- core/type/src/crypto.ts | 3 +++ core/type/src/index.ts | 1 + 2 files changed, 4 insertions(+) create mode 100644 core/type/src/crypto.ts diff --git a/core/type/src/crypto.ts b/core/type/src/crypto.ts new file mode 100644 index 000000000..3248780dc --- /dev/null +++ b/core/type/src/crypto.ts @@ -0,0 +1,3 @@ +export type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512'; + +export type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary'; diff --git a/core/type/src/index.ts b/core/type/src/index.ts index a6181eded..3086d71cb 100644 --- a/core/type/src/index.ts +++ b/core/type/src/index.ts @@ -2,6 +2,7 @@ export * from './chat.js'; export * from './service-response.js'; export * from './storage.js'; export * from './global.js'; +export * from './crypto.js'; export * from './i18n.js'; export * from './type-helper.js'; export * from './event-signal.js'; From c718cc1e3e6ea35bbef621ec3555815ab63e0b7b Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 16:24:34 +0330 Subject: [PATCH 04/24] refactor(token): use type from @alwatr/type --- core/token/package.json | 1 + core/token/src/token.ts | 4 ++-- core/token/src/type.ts | 11 +++++------ core/token/tsconfig.json | 1 + 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/token/package.json b/core/token/package.json index 85c0ea6cc..4c192bcd4 100644 --- a/core/token/package.json +++ b/core/token/package.json @@ -41,6 +41,7 @@ "dependencies": { "@alwatr/logger": "^0.30.0", "@alwatr/math": "^0.30.0", + "@alwatr/type": "^0.30.0", "tslib": "^2.5.0" } } diff --git a/core/token/src/token.ts b/core/token/src/token.ts index 106bb7ca1..6d38ab8dc 100644 --- a/core/token/src/token.ts +++ b/core/token/src/token.ts @@ -3,9 +3,9 @@ import {createHmac} from 'node:crypto'; import {createLogger, globalAlwatr} from '@alwatr/logger'; import {parseDuration} from '@alwatr/math'; -import type {TokenGeneratorConfig, TokenStatus, DigestAlgorithm} from './type.js'; +import type {TokenGeneratorConfig, TokenStatus} from './type.js'; -export type {TokenGeneratorConfig, TokenStatus, DigestAlgorithm}; +export type {TokenGeneratorConfig, TokenStatus}; globalAlwatr.registeredList.push({ name: '@alwatr/token', diff --git a/core/token/src/type.ts b/core/token/src/type.ts index 618c35394..7f5aacb60 100644 --- a/core/token/src/type.ts +++ b/core/token/src/type.ts @@ -1,6 +1,5 @@ import type {DurationString} from '@alwatr/math'; - -export type DigestAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512'; +import type {CryptoAlgorithm, CryptoEncoding} from '@alwatr/type'; export type TokenStatus = 'valid' | 'invalid' | 'expired'; @@ -20,12 +19,12 @@ export interface TokenGeneratorConfig { /** * OpenSSl digest algorithm. */ - algorithm: DigestAlgorithm; + algorithm: CryptoAlgorithm; /** * Encoding of token. */ - encoding: 'base64' | 'base64url' | 'hex' | 'binary'; -}; + encoding: CryptoEncoding; +} -export interface UserFactoryConfig extends TokenGeneratorConfig {} +export type UserFactoryConfig = TokenGeneratorConfig diff --git a/core/token/tsconfig.json b/core/token/tsconfig.json index 1b4a27413..e514f816f 100644 --- a/core/token/tsconfig.json +++ b/core/token/tsconfig.json @@ -11,6 +11,7 @@ "exclude": [], "references": [ {"path": "../logger"}, + {"path": "../type"}, {"path": "../math"}, ] } From 7b534876ada337c4137eda64c8f569c3ce1e426a Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 16:24:47 +0330 Subject: [PATCH 05/24] feat(token/user): AlwatrUserFactory --- core/token/src/user.ts | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/core/token/src/user.ts b/core/token/src/user.ts index 839d514a3..f1601989d 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -1,36 +1,48 @@ import {createLogger} from '@alwatr/logger'; +import {random} from '@alwatr/math'; +import {generateHash} from '@alwatr/math/crypto.js'; +import {User} from '@alwatr/type'; + +import {AlwatrTokenGenerator} from './token.js'; import type {UserFactoryConfig} from './type.js'; -import { AlwatrTokenGenerator } from './token.js'; export class AlwatrUserFactory { protected _logger = createLogger('alwatr-user-factory'); - private _$passwordTokengenerator = new AlwatrTokenGenerator(this.config) - private _$userTokengenerator = new AlwatrTokenGenerator({ - algorithm: 'sha224', - duration: '6m', - encoding: 'base64url', - secret: '' - }) + private _$tokenGenerator = new AlwatrTokenGenerator(this.config); constructor(public config: UserFactoryConfig) { this._logger.logMethodArgs?.('constructor', config); } generateId(): string { - + const options = { + algorithm: 'sha1', + encoding: 'hex', + outputLength: 10, + } as const; + const part1 = generateHash(random.uuid, options); + const part2 = generateHash(part1, options); + return part1 + part2; } - verifyId(): boolean { + verifyId(id: string): boolean { + const options = { + algorithm: 'sha1', + encoding: 'hex', + outputLength: 10, + } as const; + const part1 = id.substring(0, options.outputLength); + const part2 = id.substring(options.outputLength, options.outputLength * 2); + return part2 === generateHash(part1, options); } - generateToken(): string { - + generateToken(user: User): string { + return this._$tokenGenerator.generate(user.id + '-' + user.lpe); } - verifyToken(): boolean { + verifyToken(user: User, token: string): boolean { + return this.generateToken(user) === token; } - - } From 046403ab5e3a01642e9a171e2953f087d35c15c5 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 16:26:26 +0330 Subject: [PATCH 06/24] fix(demo/token): import type --- demo/token/benchmark.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/demo/token/benchmark.ts b/demo/token/benchmark.ts index 530c85af3..28d34ab0f 100644 --- a/demo/token/benchmark.ts +++ b/demo/token/benchmark.ts @@ -1,6 +1,8 @@ -import {type DigestAlgorithm, AlwatrTokenGenerator} from '@alwatr/token'; +import {AlwatrTokenGenerator} from '@alwatr/token'; import {delay} from '@alwatr/util'; +import type {CryptoAlgorithm} from '@alwatr/type'; + if (process.env.NODE_ENV !== 'production') { console.log('Please run node in production for benchmark. NODE_ENV=production node demo/token/benchmark.js'); process.exit(); @@ -15,7 +17,7 @@ const tokenGenerator = new AlwatrTokenGenerator({ const sampleData = 'Lorem ipsum dolor sit amet consectetur adipisicing elit.'; -function benchmark(algorithm: DigestAlgorithm): void { +function benchmark(algorithm: CryptoAlgorithm): void { tokenGenerator.config.algorithm = algorithm; const now = Date.now(); const testRun = 1_000_000; From fbc4246d61fbf0c26e43933d209ad4809b6af14c Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 19:03:25 +0330 Subject: [PATCH 07/24] revert(math): crypto --- core/math/package.json | 1 - core/math/src/crypto.ts | 12 ------------ core/math/tsconfig.json | 1 - 3 files changed, 14 deletions(-) delete mode 100644 core/math/src/crypto.ts diff --git a/core/math/package.json b/core/math/package.json index e9988b80b..5a6327215 100644 --- a/core/math/package.json +++ b/core/math/package.json @@ -31,7 +31,6 @@ }, "dependencies": { "@alwatr/logger": "^0.30.0", - "@alwatr/type": "^0.30.0", "tslib": "^2.5.0" } } diff --git a/core/math/src/crypto.ts b/core/math/src/crypto.ts deleted file mode 100644 index fef65ed20..000000000 --- a/core/math/src/crypto.ts +++ /dev/null @@ -1,12 +0,0 @@ -import {createHash} from 'node:crypto'; - -import type {CryptoAlgorithm, CryptoEncoding} from '@alwatr/type'; - -export const generateHash = ( - data: string, - options: {algorithm: CryptoAlgorithm; encoding: CryptoEncoding; outputLength: number}, -): string => { - return createHash(options.algorithm, {outputLength: options.outputLength}) - .update(data, 'utf8') - .digest(options.encoding); -}; diff --git a/core/math/tsconfig.json b/core/math/tsconfig.json index 5689e0024..5a4e77cd9 100644 --- a/core/math/tsconfig.json +++ b/core/math/tsconfig.json @@ -11,6 +11,5 @@ "exclude": [], "references": [ {"path": "../logger"}, - {"path": "../type"} ] } From 5c6b4db1b073b974a9942401b68786f81827f96b Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 19:03:49 +0330 Subject: [PATCH 08/24] revert(type): crypto --- core/type/src/crypto.ts | 3 --- core/type/src/index.ts | 1 - 2 files changed, 4 deletions(-) delete mode 100644 core/type/src/crypto.ts diff --git a/core/type/src/crypto.ts b/core/type/src/crypto.ts deleted file mode 100644 index 3248780dc..000000000 --- a/core/type/src/crypto.ts +++ /dev/null @@ -1,3 +0,0 @@ -export type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512'; - -export type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary'; diff --git a/core/type/src/index.ts b/core/type/src/index.ts index 3086d71cb..a6181eded 100644 --- a/core/type/src/index.ts +++ b/core/type/src/index.ts @@ -2,7 +2,6 @@ export * from './chat.js'; export * from './service-response.js'; export * from './storage.js'; export * from './global.js'; -export * from './crypto.js'; export * from './i18n.js'; export * from './type-helper.js'; export * from './event-signal.js'; From 324151bd29428094679ee8e1125d70c580fb48f4 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 19:04:03 +0330 Subject: [PATCH 09/24] feat(token): hash --- core/token/src/hash.ts | 95 ++++++++++++++++++++++++++++++++++++++++++ core/token/src/type.ts | 28 ++++++++++++- 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 core/token/src/hash.ts diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts new file mode 100644 index 000000000..ad8dfe48e --- /dev/null +++ b/core/token/src/hash.ts @@ -0,0 +1,95 @@ +import {createHash, type BinaryLike} from 'node:crypto'; + +import {createLogger} from '@alwatr/logger'; + +import type {HashGeneratorConfig, HashStatus} from './type.js'; + +export type {HashGeneratorConfig, HashStatus}; + +export class AlwatrHashGenerator { + protected _logger = createLogger('alwatr-hash-generator'); + + constructor(public config: HashGeneratorConfig) { + this._logger.logMethodArgs?.('constructor', config); + } + + /** + * Generate random bytes in nodejs + */ + + + // random1(): string { + // return this.generate1(); + // } + + /** + * Generate hash from data. + * + * Example: + * + * ```js + * const username = 'test'; + * const hash = generate(username); + * ``` + */ + generate1(data: BinaryLike): string { + return createHash(this.config.algorithm, {outputLength: this.config.outputLength}) + .update(data) + .digest(this.config.encoding); + } + + /** + * Verify hash with data and token + * + * Example: + * + * ```js + * const username = 'test'; + * const hash = generate(username); + * + * if (verify(username, hash)) { + * console.log(username, 'is valid'); + * } + * ``` + */ + verify1(data: BinaryLike, hash: string): boolean { + return hash === this.generate1(data); + } + + /** + * Generate hash from data. + * + * Example: + * + * ```js + * const username = 'test'; + * const hash = generate(username); + * ``` + */ + generate2(data: BinaryLike): string { + const part1 = this.generate1(data); + const part2 = this.generate1(part1); + return part1 + part2; + } + + /** + * Verify hash with data. + * + * Example: + * + * ```js + * const username = 'test'; + * const hash = generate(username); + * + * if (verify(username, hash) === 'valid') { + * console.log(username, 'is valid'); + * } + * ``` + */ + verify2(hash: string): boolean { + const hashLen = hash.length; + const part1 = hash.substring(0, hashLen / 2); + const part2 = hash.substring(hashLen / 2, hashLen); + return this.verify1(part1, part2); + } +} diff --git a/core/token/src/type.ts b/core/token/src/type.ts index 7f5aacb60..e2f50f7d4 100644 --- a/core/token/src/type.ts +++ b/core/token/src/type.ts @@ -1,7 +1,11 @@ import type {DurationString} from '@alwatr/math'; -import type {CryptoAlgorithm, CryptoEncoding} from '@alwatr/type'; + +export type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512'; +export type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary'; export type TokenStatus = 'valid' | 'invalid' | 'expired'; +export type HashStatus = 'valid' | 'invalid'; + export interface TokenGeneratorConfig { /** @@ -27,4 +31,24 @@ export interface TokenGeneratorConfig { encoding: CryptoEncoding; } -export type UserFactoryConfig = TokenGeneratorConfig +export interface HashGeneratorConfig { + /** + * OpenSSl digest algorithm. + */ + algorithm: CryptoAlgorithm; + + /** + * Encoding of hash. + */ + encoding: CryptoEncoding; + + /** + * Output length in bytes. + */ + outputLength: number; +} + +export interface UserFactoryConfig { + tokenConfig: TokenGeneratorConfig; + hashConfig: HashGeneratorConfig; +} From 5b91d400bf346c388096a2d725ae165e71c92928 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 19:04:19 +0330 Subject: [PATCH 10/24] feat(token): use hash for user --- core/token/src/user.ts | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/core/token/src/user.ts b/core/token/src/user.ts index f1601989d..e226c7263 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -1,41 +1,27 @@ import {createLogger} from '@alwatr/logger'; -import {random} from '@alwatr/math'; -import {generateHash} from '@alwatr/math/crypto.js'; -import {User} from '@alwatr/type'; +import {AlwatrHashGenerator} from './hash.js'; import {AlwatrTokenGenerator} from './token.js'; import type {UserFactoryConfig} from './type.js'; +import type {User} from '@alwatr/type'; export class AlwatrUserFactory { protected _logger = createLogger('alwatr-user-factory'); - private _$tokenGenerator = new AlwatrTokenGenerator(this.config); + private _$tokenGenerator = new AlwatrTokenGenerator(this.config.tokenConfig); + private _$hashGenerator = new AlwatrHashGenerator(this.config.hashConfig); constructor(public config: UserFactoryConfig) { this._logger.logMethodArgs?.('constructor', config); } generateId(): string { - const options = { - algorithm: 'sha1', - encoding: 'hex', - outputLength: 10, - } as const; - const part1 = generateHash(random.uuid, options); - const part2 = generateHash(part1, options); - return part1 + part2; + return this._$hashGenerator.generate2(); } verifyId(id: string): boolean { - const options = { - algorithm: 'sha1', - encoding: 'hex', - outputLength: 10, - } as const; - const part1 = id.substring(0, options.outputLength); - const part2 = id.substring(options.outputLength, options.outputLength * 2); - return part2 === generateHash(part1, options); + return this._$hashGenerator.verify2(id); } generateToken(user: User): string { From 2e3d5e6a6fa1420e5e0b09804a54318c186373a4 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 19:33:46 +0330 Subject: [PATCH 11/24] docs(token): user --- core/token/src/hash.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index ad8dfe48e..22314ff16 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -29,7 +29,7 @@ export class AlwatrHashGenerator { * * ```js * const username = 'test'; - * const hash = generate(username); + * const hash = generate1(username); * ``` */ generate1(data: BinaryLike): string { @@ -39,16 +39,16 @@ export class AlwatrHashGenerator { } /** - * Verify hash with data and token + * Verify hash with data and hash. * * Example: * * ```js * const username = 'test'; - * const hash = generate(username); + * const userId = generate1(username); * - * if (verify(username, hash)) { - * console.log(username, 'is valid'); + * if (verify1(username, userId)) { + * console.log(userId, 'is valid'); * } * ``` */ @@ -57,13 +57,13 @@ export class AlwatrHashGenerator { } /** - * Generate hash from data. + * Generate `self-verified` hash from data. * * Example: * * ```js * const username = 'test'; - * const hash = generate(username); + * const hash = generate2(username); * ``` */ generate2(data: BinaryLike): string { @@ -73,16 +73,16 @@ export class AlwatrHashGenerator { } /** - * Verify hash with data. + * Verify `self-verified` hash. * * Example: * * ```js * const username = 'test'; - * const hash = generate(username); + * const userId = generate2(username); * - * if (verify(username, hash) === 'valid') { - * console.log(username, 'is valid'); + * if (verify2(username, userId)) { + * console.log(userId, 'is valid'); * } * ``` */ From dbac3ed4766184327f4c4d696323facccad422d3 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sat, 29 Apr 2023 19:48:24 +0330 Subject: [PATCH 12/24] feat(token): index.js --- core/token/package.json | 4 ++-- core/token/src/hash.ts | 4 +--- core/token/src/index.ts | 10 ++++++++++ core/token/src/token.ts | 9 +-------- 4 files changed, 14 insertions(+), 13 deletions(-) create mode 100644 core/token/src/index.ts diff --git a/core/token/package.json b/core/token/package.json index 4c192bcd4..b08591910 100644 --- a/core/token/package.json +++ b/core/token/package.json @@ -18,9 +18,9 @@ "esm", "alwatr" ], - "main": "token.js", + "main": "index.js", "type": "module", - "types": "token.d.ts", + "types": "index.d.ts", "author": "S. Ali Mihandoost ", "license": "MIT", "files": [ diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index 22314ff16..2dbad57d6 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -2,9 +2,7 @@ import {createHash, type BinaryLike} from 'node:crypto'; import {createLogger} from '@alwatr/logger'; -import type {HashGeneratorConfig, HashStatus} from './type.js'; - -export type {HashGeneratorConfig, HashStatus}; +import type {HashGeneratorConfig} from './type.js'; export class AlwatrHashGenerator { protected _logger = createLogger('alwatr-hash-generator'); diff --git a/core/token/src/index.ts b/core/token/src/index.ts new file mode 100644 index 000000000..f57e3bf65 --- /dev/null +++ b/core/token/src/index.ts @@ -0,0 +1,10 @@ +import {globalAlwatr} from '@alwatr/logger'; + +export * from './hash.js'; +export * from './token.js'; +export * from './type.js'; + +globalAlwatr.registeredList.push({ + name: '@alwatr/token', + version: _ALWATR_VERSION_, +}); diff --git a/core/token/src/token.ts b/core/token/src/token.ts index 6d38ab8dc..07a588f0b 100644 --- a/core/token/src/token.ts +++ b/core/token/src/token.ts @@ -1,17 +1,10 @@ import {createHmac} from 'node:crypto'; -import {createLogger, globalAlwatr} from '@alwatr/logger'; +import {createLogger} from '@alwatr/logger'; import {parseDuration} from '@alwatr/math'; import type {TokenGeneratorConfig, TokenStatus} from './type.js'; -export type {TokenGeneratorConfig, TokenStatus}; - -globalAlwatr.registeredList.push({ - name: '@alwatr/token', - version: _ALWATR_VERSION_, -}); - export class AlwatrTokenGenerator { protected _logger = createLogger('alwatr-token-generator'); private _duration: number | null; From ad03a19fc4970c01c39ac1b40e8d933a8d0539a0 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 20:01:45 +0330 Subject: [PATCH 13/24] fix(demo): token --- demo/token/benchmark.ts | 2 +- demo/token/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/token/benchmark.ts b/demo/token/benchmark.ts index 28d34ab0f..eddbc6148 100644 --- a/demo/token/benchmark.ts +++ b/demo/token/benchmark.ts @@ -1,7 +1,7 @@ import {AlwatrTokenGenerator} from '@alwatr/token'; import {delay} from '@alwatr/util'; -import type {CryptoAlgorithm} from '@alwatr/type'; +import type {CryptoAlgorithm} from '@alwatr/token/type.js'; if (process.env.NODE_ENV !== 'production') { console.log('Please run node in production for benchmark. NODE_ENV=production node demo/token/benchmark.js'); diff --git a/demo/token/index.ts b/demo/token/index.ts index c1bf21c10..1b00c262e 100644 --- a/demo/token/index.ts +++ b/demo/token/index.ts @@ -1,7 +1,7 @@ import {createLogger} from '@alwatr/logger'; import {type TokenStatus, AlwatrTokenGenerator} from '@alwatr/token'; -const logger = createLogger('token/demo'); +const logger = createLogger('token/demo', true); const tokenGenerator = new AlwatrTokenGenerator({ secret: 'my-very-secret-key', From 89bf7c5289003e1f87c56063364618b8c1edf809 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 20:02:06 +0330 Subject: [PATCH 14/24] feat(token): random hash --- core/token/src/hash.ts | 15 +++++++-------- core/token/src/user.ts | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index 2dbad57d6..68a764f9d 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -1,4 +1,4 @@ -import {createHash, type BinaryLike} from 'node:crypto'; +import {createHash, randomBytes, type BinaryLike} from 'node:crypto'; import {createLogger} from '@alwatr/logger'; @@ -11,14 +11,13 @@ export class AlwatrHashGenerator { this._logger.logMethodArgs?.('constructor', config); } - /** - * Generate random bytes in nodejs - */ - + random1(): string { + return this.generate1(randomBytes(32)); + } - // random1(): string { - // return this.generate1(); - // } + random2(): string { + return this.generate2(randomBytes(32)); + } /** * Generate hash from data. diff --git a/core/token/src/user.ts b/core/token/src/user.ts index e226c7263..fea6146a1 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -9,15 +9,15 @@ import type {User} from '@alwatr/type'; export class AlwatrUserFactory { protected _logger = createLogger('alwatr-user-factory'); - private _$tokenGenerator = new AlwatrTokenGenerator(this.config.tokenConfig); private _$hashGenerator = new AlwatrHashGenerator(this.config.hashConfig); + private _$tokenGenerator = new AlwatrTokenGenerator(this.config.tokenConfig); constructor(public config: UserFactoryConfig) { this._logger.logMethodArgs?.('constructor', config); } generateId(): string { - return this._$hashGenerator.generate2(); + return this._$hashGenerator.random2(); } verifyId(id: string): boolean { From 662eaa69ef7ee2127ef5b7a2a9c615043a5b6511 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 22:03:43 +0330 Subject: [PATCH 15/24] refactor(crypto/token): review, remove logger, document --- core/token/src/token.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/core/token/src/token.ts b/core/token/src/token.ts index 07a588f0b..8c0e82848 100644 --- a/core/token/src/token.ts +++ b/core/token/src/token.ts @@ -1,22 +1,20 @@ import {createHmac} from 'node:crypto'; -import {createLogger} from '@alwatr/logger'; import {parseDuration} from '@alwatr/math'; import type {TokenGeneratorConfig, TokenStatus} from './type.js'; +/** + * Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm). + */ export class AlwatrTokenGenerator { - protected _logger = createLogger('alwatr-token-generator'); - private _duration: number | null; + protected _duration: number | null; get epoch(): number { - return this._duration == null - ? 0 - : Math.floor(Date.now() / this._duration); + return this._duration == null ? 0 : Math.floor(Date.now() / this._duration); } constructor(public config: TokenGeneratorConfig) { - this._logger.logMethodArgs?.('constructor', config); this._duration = config.duration == null ? null : parseDuration(config.duration); } @@ -26,10 +24,24 @@ export class AlwatrTokenGenerator { .digest(this.config.encoding); } + /** + * Generate HOTP token from data base on special duration. + * + * ```ts + * user.auth = tokenGenerator.generate(`${user.id}-${user.role}`); + * ``` + */ generate(data: string): string { return this._generate(data, this.epoch); } + /** + * Token validation. + * + * ```ts + * const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth); + * ``` + */ verify(data: string, token: string): TokenStatus { const epoch = this.epoch; if (token === this._generate(data, epoch)) { From ce08ac92e7e0207addfd911e1212868a3fc363c9 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 22:04:51 +0330 Subject: [PATCH 16/24] refactor(crypto/hash): review, remove logger, document, change methods name --- core/token/src/hash.ts | 99 ++++++++++++++++++++++-------------------- core/token/src/type.ts | 9 +++- 2 files changed, 60 insertions(+), 48 deletions(-) diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index 68a764f9d..94c2b6520 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -1,92 +1,99 @@ import {createHash, randomBytes, type BinaryLike} from 'node:crypto'; -import {createLogger} from '@alwatr/logger'; - import type {HashGeneratorConfig} from './type.js'; +/** + * Secure **self-validate** hash generator. + */ export class AlwatrHashGenerator { - protected _logger = createLogger('alwatr-hash-generator'); - - constructor(public config: HashGeneratorConfig) { - this._logger.logMethodArgs?.('constructor', config); - } + constructor(public config: HashGeneratorConfig) {} - random1(): string { - return this.generate1(randomBytes(32)); + /** + * Generate simple hash from random data. + * + * Example: + * + * ```js + * const clientId = hashGenerator.random(); + * ``` + */ + random(size: number = this.config.mainSize): string { + return this.generate(randomBytes(size), size); } - random2(): string { - return this.generate2(randomBytes(32)); + /** + * Generate **self-validate** hash from random data. + * + * Example: + * + * ```ts + * const userId = hashGenerator.randomSelfValidate(); + * ``` + */ + randomSelfValidate(size: number = this.config.mainSize): string { + return this.generateSelfValidate(randomBytes(size), size); } /** - * Generate hash from data. + * Generate simple hash from data. * * Example: * - * ```js - * const username = 'test'; - * const hash = generate1(username); + * ```ts + * const crcHash = hashGenerator.generate(downloadedData); * ``` */ - generate1(data: BinaryLike): string { - return createHash(this.config.algorithm, {outputLength: this.config.outputLength}) + generate(data: BinaryLike, size: number = this.config.mainSize): string { + return createHash(this.config.algorithm, {outputLength: size}) .update(data) .digest(this.config.encoding); } /** - * Verify hash with data and hash. + * Generate **self-validate** hash from data. * * Example: * * ```js - * const username = 'test'; - * const userId = generate1(username); - * - * if (verify1(username, userId)) { - * console.log(userId, 'is valid'); - * } + * const userId = hashGenerator.generateSelfValidate(randomData); * ``` */ - verify1(data: BinaryLike, hash: string): boolean { - return hash === this.generate1(data); + generateSelfValidate(data: BinaryLike): string { + const mainHash = this.generate(data, this.config.mainSize); + const crcHash = this.generate(mainHash, this.config.crcSize); + return mainHash + crcHash; } /** - * Generate `self-verified` hash from data. + * Verify `generate(data)` equals to `hash`. * * Example: * - * ```js - * const username = 'test'; - * const hash = generate2(username); + * ```ts + * if (!hashGenerator.verify(downloadedData, crcHash)) { + * new Error('data_corrupted'); + * } * ``` */ - generate2(data: BinaryLike): string { - const part1 = this.generate1(data); - const part2 = this.generate1(part1); - return part1 + part2; + verify(data: BinaryLike, hash: string): boolean { + return hash === this.generate(data); } /** - * Verify `self-verified` hash. + * Verify a **self-validate** hash to check its generated by this class (same options). * * Example: * - * ```js - * const username = 'test'; - * const userId = generate2(username); - * - * if (verify2(username, userId)) { - * console.log(userId, 'is valid'); + * ```ts + * if (!hashGenerator.verifySelfValidate(user.id)) { + * new Error('invalid_user'); * } * ``` */ - verify2(hash: string): boolean { - const hashLen = hash.length; - const part1 = hash.substring(0, hashLen / 2); - const part2 = hash.substring(hashLen / 2, hashLen); - return this.verify1(part1, part2); + verifySelfValidate(hash: string): boolean { + const crcLen = this.generate('', this.config.crcSize).length; + const part1 = hash.substring(0, hash.length - crcLen); + const part2 = hash.substring(hash.length - crcLen, hash.length); + return this.verify(part1, part2); } } diff --git a/core/token/src/type.ts b/core/token/src/type.ts index e2f50f7d4..c87af2ac7 100644 --- a/core/token/src/type.ts +++ b/core/token/src/type.ts @@ -43,9 +43,14 @@ export interface HashGeneratorConfig { encoding: CryptoEncoding; /** - * Output length in bytes. + * Main hash length in bytes. */ - outputLength: number; + mainSize: number; + + /** + * Validation hash length in bytes. + */ + crcSize: number; } export interface UserFactoryConfig { From f5a464095b446c9333414c475747610db9b2a3b6 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 22:05:01 +0330 Subject: [PATCH 17/24] refactor(crypto/user): review, remove logger, document --- core/token/src/index.ts | 1 + core/token/src/user.ts | 70 +++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/core/token/src/index.ts b/core/token/src/index.ts index f57e3bf65..b3f511828 100644 --- a/core/token/src/index.ts +++ b/core/token/src/index.ts @@ -2,6 +2,7 @@ import {globalAlwatr} from '@alwatr/logger'; export * from './hash.js'; export * from './token.js'; +export * from './user.js'; export * from './type.js'; globalAlwatr.registeredList.push({ diff --git a/core/token/src/user.ts b/core/token/src/user.ts index fea6146a1..4f765b110 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -1,34 +1,70 @@ -import {createLogger} from '@alwatr/logger'; - import {AlwatrHashGenerator} from './hash.js'; import {AlwatrTokenGenerator} from './token.js'; -import type {UserFactoryConfig} from './type.js'; -import type {User} from '@alwatr/type'; +import type {TokenStatus, UserFactoryConfig} from './type.js'; +/** + * Secure User ID/Token Factory (generate and validate authentication process). + */ export class AlwatrUserFactory { - protected _logger = createLogger('alwatr-user-factory'); - - private _$hashGenerator = new AlwatrHashGenerator(this.config.hashConfig); - private _$tokenGenerator = new AlwatrTokenGenerator(this.config.tokenConfig); + constructor(public config: UserFactoryConfig) {} - constructor(public config: UserFactoryConfig) { - this._logger.logMethodArgs?.('constructor', config); - } + protected _hashGenerator = new AlwatrHashGenerator(this.config.hashConfig); + protected _tokenGenerator = new AlwatrTokenGenerator(this.config.tokenConfig); + /** + * Generate new self-verifiable user-id. + * + * Example: + * + * ```ts + * const newUser = { + * id: userFactory.generateId(), + * ... + * } + * ``` + */ generateId(): string { - return this._$hashGenerator.random2(); + return this._hashGenerator.randomSelfValidate(); } + /** + * Validate user-id without token. + * + * Example: + * + * ```ts + * const newUser = { + * id: userFactory.generateId(), + * ... + * } + * ``` + */ verifyId(id: string): boolean { - return this._$hashGenerator.verify2(id); + return this._hashGenerator.verifySelfValidate(id); } - generateToken(user: User): string { - return this._$tokenGenerator.generate(user.id + '-' + user.lpe); + /** + * Generate user auth token. + * + * Example: + * + * ```ts + * ``` + */ + generateToken(uniquelyList: Array): string { + return this._tokenGenerator.generate(uniquelyList.join()); } - verifyToken(user: User, token: string): boolean { - return this.generateToken(user) === token; + /** + * Verify user auth token. + * + * Example: + * + * ```ts + * ``` + */ + verifyToken(uniquelyList: Array, token: string): TokenStatus { + return this._tokenGenerator.verify(uniquelyList.join(), token); } } From 8a81adb31a7e9f0baa04864eb1586cdf1fa00aaa Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 22:19:06 +0330 Subject: [PATCH 18/24] fix(crypto/hash): params issue --- core/token/src/hash.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index 94c2b6520..16674ac10 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -17,8 +17,8 @@ export class AlwatrHashGenerator { * const clientId = hashGenerator.random(); * ``` */ - random(size: number = this.config.mainSize): string { - return this.generate(randomBytes(size), size); + random(): string { + return this.generate(randomBytes(this.config.mainSize), this.config.mainSize); } /** @@ -30,8 +30,8 @@ export class AlwatrHashGenerator { * const userId = hashGenerator.randomSelfValidate(); * ``` */ - randomSelfValidate(size: number = this.config.mainSize): string { - return this.generateSelfValidate(randomBytes(size), size); + randomSelfValidate(): string { + return this.generateSelfValidate(randomBytes(this.config.mainSize)); } /** From 445b8650a1abd98696b7fe954bcfca23f76afba8 Mon Sep 17 00:00:00 2001 From: Mohammad Honarvar Date: Sat, 29 Apr 2023 22:25:14 +0330 Subject: [PATCH 19/24] feat(demo): crypto hash --- demo/token/hash.ts | 17 +++++++++++++++++ demo/token/{index.ts => token.ts} | 0 2 files changed, 17 insertions(+) create mode 100644 demo/token/hash.ts rename demo/token/{index.ts => token.ts} (100%) diff --git a/demo/token/hash.ts b/demo/token/hash.ts new file mode 100644 index 000000000..fc70dd9b7 --- /dev/null +++ b/demo/token/hash.ts @@ -0,0 +1,17 @@ +import {AlwatrHashGenerator} from '@alwatr/token'; + +const hashGenerator = new AlwatrHashGenerator({ + algorithm: 'sha1', + mainSize: 12, + crcSize: 4, + encoding: 'base64url', +}); + +const test = (): void => { + const hash = hashGenerator.randomSelfValidate(); + console.log('hash: %s validation: %s', hash, hashGenerator.verifySelfValidate(hash)); +}; + +for (let index = 0; index < 10; index++) { + test(); +} diff --git a/demo/token/index.ts b/demo/token/token.ts similarity index 100% rename from demo/token/index.ts rename to demo/token/token.ts From 7a6c27e2f1b78df4aaa03a764f1b57477f1220fc Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 23:37:08 +0330 Subject: [PATCH 20/24] feat(crypto/hash): new crc length strategy --- core/token/src/hash.ts | 33 +++++++++++++++++++++------------ core/token/src/type.ts | 9 ++------- demo/token/hash.ts | 3 +-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index 16674ac10..6748c1f7a 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -18,7 +18,7 @@ export class AlwatrHashGenerator { * ``` */ random(): string { - return this.generate(randomBytes(this.config.mainSize), this.config.mainSize); + return this.generate(randomBytes(16)); } /** @@ -31,7 +31,7 @@ export class AlwatrHashGenerator { * ``` */ randomSelfValidate(): string { - return this.generateSelfValidate(randomBytes(this.config.mainSize)); + return this.generateSelfValidate(randomBytes(16)); } /** @@ -43,10 +43,16 @@ export class AlwatrHashGenerator { * const crcHash = hashGenerator.generate(downloadedData); * ``` */ - generate(data: BinaryLike, size: number = this.config.mainSize): string { - return createHash(this.config.algorithm, {outputLength: size}) - .update(data) - .digest(this.config.encoding); + generate(data: BinaryLike): string { + return createHash(this.config.algorithm).update(data).digest(this.config.encoding); + } + + /** + * Generate crc hash. + */ + _generateCrc(data: BinaryLike): string { + const crc = this.generate(data); + return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.substring(0, this.config.crcLength); } /** @@ -59,8 +65,8 @@ export class AlwatrHashGenerator { * ``` */ generateSelfValidate(data: BinaryLike): string { - const mainHash = this.generate(data, this.config.mainSize); - const crcHash = this.generate(mainHash, this.config.crcSize); + const mainHash = this.generate(data); + const crcHash = this._generateCrc(mainHash); return mainHash + crcHash; } @@ -91,9 +97,12 @@ export class AlwatrHashGenerator { * ``` */ verifySelfValidate(hash: string): boolean { - const crcLen = this.generate('', this.config.crcSize).length; - const part1 = hash.substring(0, hash.length - crcLen); - const part2 = hash.substring(hash.length - crcLen, hash.length); - return this.verify(part1, part2); + const gapPos = + this.config.crcLength == null || this.config.crcLength < 1 + ? hash.length / 2 + : hash.length - this.config.crcLength; + const mainHash = hash.substring(0, gapPos); + const crcHash = hash.substring(gapPos); + return crcHash === this._generateCrc(mainHash); } } diff --git a/core/token/src/type.ts b/core/token/src/type.ts index c87af2ac7..ed9d2ecfa 100644 --- a/core/token/src/type.ts +++ b/core/token/src/type.ts @@ -43,14 +43,9 @@ export interface HashGeneratorConfig { encoding: CryptoEncoding; /** - * Main hash length in bytes. + * CRC hash max length. */ - mainSize: number; - - /** - * Validation hash length in bytes. - */ - crcSize: number; + crcLength?: number; } export interface UserFactoryConfig { diff --git a/demo/token/hash.ts b/demo/token/hash.ts index fc70dd9b7..13e2e26ed 100644 --- a/demo/token/hash.ts +++ b/demo/token/hash.ts @@ -2,9 +2,8 @@ import {AlwatrHashGenerator} from '@alwatr/token'; const hashGenerator = new AlwatrHashGenerator({ algorithm: 'sha1', - mainSize: 12, - crcSize: 4, encoding: 'base64url', + crcLength: 8, }); const test = (): void => { From 89afd7f9a3c89f5c626d83210bef3d6c1af863b3 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 23:37:54 +0330 Subject: [PATCH 21/24] feat(crypto/user): crcLength --- core/token/src/user.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/token/src/user.ts b/core/token/src/user.ts index 4f765b110..55ddfe96d 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -1,16 +1,22 @@ import {AlwatrHashGenerator} from './hash.js'; import {AlwatrTokenGenerator} from './token.js'; -import type {TokenStatus, UserFactoryConfig} from './type.js'; +import type {HashGeneratorConfig, TokenGeneratorConfig, TokenStatus} from './type.js'; /** * Secure User ID/Token Factory (generate and validate authentication process). */ export class AlwatrUserFactory { - constructor(public config: UserFactoryConfig) {} + protected _tokenGenerator; + protected _hashGenerator; - protected _hashGenerator = new AlwatrHashGenerator(this.config.hashConfig); - protected _tokenGenerator = new AlwatrTokenGenerator(this.config.tokenConfig); + constructor( + tokenConfig: TokenGeneratorConfig, + hashConfig: HashGeneratorConfig = {algorithm: 'sha1', encoding: 'base64url', crcLength: 8}, + ) { + this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig); + this._hashGenerator = new AlwatrHashGenerator(hashConfig); + } /** * Generate new self-verifiable user-id. From 7acec94bc7aee467cff4a3a1f1ca9bf014486469 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 29 Apr 2023 23:43:06 +0330 Subject: [PATCH 22/24] refactor(hash): alwatr default config --- core/token/src/hash.ts | 2 +- core/token/src/user.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/token/src/hash.ts b/core/token/src/hash.ts index 6748c1f7a..411dc864c 100644 --- a/core/token/src/hash.ts +++ b/core/token/src/hash.ts @@ -6,7 +6,7 @@ import type {HashGeneratorConfig} from './type.js'; * Secure **self-validate** hash generator. */ export class AlwatrHashGenerator { - constructor(public config: HashGeneratorConfig) {} + constructor(public config: HashGeneratorConfig = {algorithm: 'sha1', encoding: 'base64url', crcLength: 8}) {} /** * Generate simple hash from random data. diff --git a/core/token/src/user.ts b/core/token/src/user.ts index 55ddfe96d..e49643442 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -12,7 +12,7 @@ export class AlwatrUserFactory { constructor( tokenConfig: TokenGeneratorConfig, - hashConfig: HashGeneratorConfig = {algorithm: 'sha1', encoding: 'base64url', crcLength: 8}, + hashConfig?: HashGeneratorConfig, ) { this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig); this._hashGenerator = new AlwatrHashGenerator(hashConfig); From 55a840e5c221263fed421bb7f792be3763f9a1a6 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sun, 30 Apr 2023 18:51:15 +0330 Subject: [PATCH 23/24] docs(token): complete some examples Co-authored-by: Mohammad Honarvar --- core/token/src/token.ts | 2 +- core/token/src/user.ts | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/token/src/token.ts b/core/token/src/token.ts index 8c0e82848..f1bccc09b 100644 --- a/core/token/src/token.ts +++ b/core/token/src/token.ts @@ -36,7 +36,7 @@ export class AlwatrTokenGenerator { } /** - * Token validation. + * Token verification. * * ```ts * const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth); diff --git a/core/token/src/user.ts b/core/token/src/user.ts index e49643442..4f8095697 100644 --- a/core/token/src/user.ts +++ b/core/token/src/user.ts @@ -40,8 +40,9 @@ export class AlwatrUserFactory { * Example: * * ```ts - * const newUser = { - * id: userFactory.generateId(), + * const newId = userFactory.generateId(); + * ... + * if (userFactory.verifyId(newId)) { * ... * } * ``` @@ -56,6 +57,7 @@ export class AlwatrUserFactory { * Example: * * ```ts + * const userToken = userFactory.generateToken(['userId', 1]) * ``` */ generateToken(uniquelyList: Array): string { @@ -68,6 +70,11 @@ export class AlwatrUserFactory { * Example: * * ```ts + * const userToken = userFactory.generateToken(['userId', 1]); + * ... + * if (userFactory.verifyToken(userToken)) { + * ... + * } * ``` */ verifyToken(uniquelyList: Array, token: string): TokenStatus { From 5747c15646ede87d7a01e103cbb92f2cf300947f Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sun, 30 Apr 2023 18:52:20 +0330 Subject: [PATCH 24/24] chore(token): cleanup dependencies & tsconfig Co-authored-by: Mohammad Honarvar --- core/math/tsconfig.json | 2 +- core/token/package.json | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/core/math/tsconfig.json b/core/math/tsconfig.json index 5a4e77cd9..1f9b9b7c4 100644 --- a/core/math/tsconfig.json +++ b/core/math/tsconfig.json @@ -10,6 +10,6 @@ "include": ["src/**/*.ts"], "exclude": [], "references": [ - {"path": "../logger"}, + {"path": "../logger"} ] } diff --git a/core/token/package.json b/core/token/package.json index b08591910..7f612bcf1 100644 --- a/core/token/package.json +++ b/core/token/package.json @@ -41,7 +41,6 @@ "dependencies": { "@alwatr/logger": "^0.30.0", "@alwatr/math": "^0.30.0", - "@alwatr/type": "^0.30.0", "tslib": "^2.5.0" } }