@alwatr/crypto 0.31.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/hash.d.ts +1 -1
- package/hash.d.ts.map +1 -1
- package/hash.js +2 -2
- package/hash.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/package.json +5 -5
- package/pre-config.d.ts +14 -0
- package/pre-config.d.ts.map +1 -0
- package/pre-config.js +24 -0
- package/pre-config.js.map +1 -0
- package/user.d.ts +2 -2
- package/user.d.ts.map +1 -1
- package/user.js +3 -3
- package/user.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,20 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.0.0](https://github.com/AliMD/alwatr/compare/v0.32.0...v1.0.0) (2023-06-14)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
9
|
+
|
|
10
|
+
# [0.32.0](https://github.com/AliMD/alwatr/compare/v0.31.0...v0.32.0) (2023-05-27)
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
- **crypto:** add some pre config ([4b960c5](https://github.com/AliMD/alwatr/commit/4b960c5cb3f7495c0689adcb65c62a1032ae2650))
|
|
15
|
+
|
|
16
|
+
### Performance Improvements
|
|
17
|
+
|
|
18
|
+
- **crypto/hash:** enhance crc generator ([ba8c4bc](https://github.com/AliMD/alwatr/commit/ba8c4bcf8f9ec9767b0bd3b6c3fd5c4f503a84dd))
|
|
19
|
+
|
|
6
20
|
# [0.31.0](https://github.com/AliMD/alwatr/compare/v0.30.0...v0.31.0) (2023-05-08)
|
|
7
21
|
|
|
8
22
|
### Bug Fixes
|
package/hash.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { HashGeneratorConfig } from './type.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export declare class AlwatrHashGenerator {
|
|
8
8
|
config: HashGeneratorConfig;
|
|
9
|
-
constructor(config
|
|
9
|
+
constructor(config: HashGeneratorConfig);
|
|
10
10
|
/**
|
|
11
11
|
* Generate simple hash from random data.
|
|
12
12
|
*
|
package/hash.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":";AAAA,OAAO,EAA0B,KAAK,UAAU,EAAC,MAAM,aAAa,CAAC;AAErE,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAEnD;;GAEG;AACH,qBAAa,mBAAmB;IACX,MAAM,EAAE,mBAAmB;gBAA3B,MAAM,
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":";AAAA,OAAO,EAA0B,KAAK,UAAU,EAAC,MAAM,aAAa,CAAC;AAErE,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAEnD;;GAEG;AACH,qBAAa,mBAAmB;IACX,MAAM,EAAE,mBAAmB;gBAA3B,MAAM,EAAE,mBAAmB;IAE9C;;;;;;;;OAQG;IACH,MAAM,IAAI,MAAM;IAIhB;;;;;;;;OAQG;IACH,kBAAkB,IAAI,MAAM;IAI5B;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAIlC;;OAEG;IACH,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAKtC;;;;;;;;OAQG;IACH,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAM9C;;;;;;;;;;OAUG;IACH,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAI/C;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;CAS1C"}
|
package/hash.js
CHANGED
|
@@ -3,7 +3,7 @@ import { createHash, randomBytes } from 'node:crypto';
|
|
|
3
3
|
* Secure **self-validate** hash generator.
|
|
4
4
|
*/
|
|
5
5
|
export class AlwatrHashGenerator {
|
|
6
|
-
constructor(config
|
|
6
|
+
constructor(config) {
|
|
7
7
|
this.config = config;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
@@ -46,7 +46,7 @@ export class AlwatrHashGenerator {
|
|
|
46
46
|
* Generate crc hash.
|
|
47
47
|
*/
|
|
48
48
|
_generateCrc(data) {
|
|
49
|
-
const crc =
|
|
49
|
+
const crc = createHash('sha1').update(data).digest(this.config.encoding);
|
|
50
50
|
return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.substring(0, this.config.crcLength);
|
|
51
51
|
}
|
|
52
52
|
/**
|
package/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,WAAW,EAAkB,MAAM,aAAa,CAAC;AAIrE;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAC9B,YAAmB,
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,WAAW,EAAkB,MAAM,aAAa,CAAC;AAIrE;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAC9B,YAAmB,MAA2B;QAA3B,WAAM,GAAN,MAAM,CAAqB;IAAG,CAAC;IAElD;;;;;;;;OAQG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,kBAAkB;QAChB,OAAO,IAAI,CAAC,oBAAoB,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAgB;QACvB,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACrF,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,IAAgB;QAC3B,MAAM,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACzE,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpH,CAAC;IAED;;;;;;;;OAQG;IACH,oBAAoB,CAAC,IAAgB;QACnC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACrC,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC5C,OAAO,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;OAUG;IACH,MAAM,CAAC,IAAgB,EAAE,IAAY;QACnC,OAAO,IAAI,KAAK,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,IAAY;QAC7B,MAAM,MAAM,GACV,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC;YACxD,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC;YACjB,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;QAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAC3C,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QACvC,OAAO,OAAO,KAAK,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;IACjD,CAAC;CACF","sourcesContent":["import {createHash, randomBytes, type BinaryLike} from 'node:crypto';\n\nimport type {HashGeneratorConfig} from './type.js';\n\n/**\n * Secure **self-validate** hash generator.\n */\nexport class AlwatrHashGenerator {\n constructor(public config: HashGeneratorConfig) {}\n\n /**\n * Generate simple hash from random data.\n *\n * Example:\n *\n * ```js\n * const clientId = hashGenerator.random();\n * ```\n */\n random(): string {\n return this.generate(randomBytes(16));\n }\n\n /**\n * Generate **self-validate** hash from random data.\n *\n * Example:\n *\n * ```ts\n * const userId = hashGenerator.randomSelfValidate();\n * ```\n */\n randomSelfValidate(): string {\n return this.generateSelfValidate(randomBytes(16));\n }\n\n /**\n * Generate simple hash from data.\n *\n * Example:\n *\n * ```ts\n * const crcHash = hashGenerator.generate(downloadedData);\n * ```\n */\n generate(data: BinaryLike): string {\n return createHash(this.config.algorithm).update(data).digest(this.config.encoding);\n }\n\n /**\n * Generate crc hash.\n */\n _generateCrc(data: BinaryLike): string {\n const crc = createHash('sha1').update(data).digest(this.config.encoding);\n return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.substring(0, this.config.crcLength);\n }\n\n /**\n * Generate **self-validate** hash from data.\n *\n * Example:\n *\n * ```js\n * const userId = hashGenerator.generateSelfValidate(randomData);\n * ```\n */\n generateSelfValidate(data: BinaryLike): string {\n const mainHash = this.generate(data);\n const crcHash = this._generateCrc(mainHash);\n return mainHash + crcHash;\n }\n\n /**\n * Verify `generate(data)` equals to `hash`.\n *\n * Example:\n *\n * ```ts\n * if (!hashGenerator.verify(downloadedData, crcHash)) {\n * new Error('data_corrupted');\n * }\n * ```\n */\n verify(data: BinaryLike, hash: string): boolean {\n return hash === this.generate(data);\n }\n\n /**\n * Verify a **self-validate** hash to check its generated by this class (same options).\n *\n * Example:\n *\n * ```ts\n * if (!hashGenerator.verifySelfValidate(user.id)) {\n * new Error('invalid_user');\n * }\n * ```\n */\n verifySelfValidate(hash: string): boolean {\n const gapPos =\n this.config.crcLength == null || this.config.crcLength < 1\n ? hash.length / 2\n : hash.length - this.config.crcLength;\n const mainHash = hash.substring(0, gapPos);\n const crcHash = hash.substring(gapPos);\n return crcHash === this._generateCrc(mainHash);\n }\n}\n"]}
|
package/index.d.ts
CHANGED
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC"}
|
package/index.js
CHANGED
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAEhC,YAAY,CAAC,cAAc,CAAC,IAAI,CAAC;IAC/B,IAAI,EAAE,gBAAgB;IACtB,OAAO,EAAE,gBAAgB;CAC1B,CAAC,CAAC","sourcesContent":["import {globalAlwatr} from '@alwatr/logger';\n\nexport * from './hash.js';\nexport * from './token.js';\nexport * from './user.js';\nexport * from './type.js';\nexport * from './pre-config.js';\n\nglobalAlwatr.registeredList.push({\n name: '@alwatr/crypto',\n version: _ALWATR_VERSION_,\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/crypto",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Secure authentication HOTP token generator (the HMAC-based One-Time Password algorithm) and crypto utils written in tiny TypeScript module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"url": "https://github.com/AliMD/alwatr/issues"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@alwatr/logger": "^0.
|
|
44
|
-
"@alwatr/math": "^0.
|
|
45
|
-
"tslib": "^2.5.
|
|
43
|
+
"@alwatr/logger": "^1.0.0",
|
|
44
|
+
"@alwatr/math": "^1.0.0",
|
|
45
|
+
"tslib": "^2.5.3"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "bfeaaac8fb74b666a2ae46d6d3a026c7d0b67bfb"
|
|
48
48
|
}
|
package/pre-config.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HashGeneratorConfig, TokenGeneratorConfig } from './type.js';
|
|
2
|
+
/**
|
|
3
|
+
* Hash generator pre configuration for making random self-validate **secrets**.
|
|
4
|
+
*/
|
|
5
|
+
export declare const secretGeneratorPreConfig: HashGeneratorConfig;
|
|
6
|
+
/**
|
|
7
|
+
* Hash generator pre configuration for making random self-validate **user-id**.
|
|
8
|
+
*/
|
|
9
|
+
export declare const userIdGeneratorPreConfig: HashGeneratorConfig;
|
|
10
|
+
/**
|
|
11
|
+
* Token generator pre configuration for making secure self-validate **user-token**.
|
|
12
|
+
*/
|
|
13
|
+
export declare const userTokenGeneratorPreConfig: Pick<TokenGeneratorConfig, 'algorithm' | 'encoding'>;
|
|
14
|
+
//# sourceMappingURL=pre-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pre-config.d.ts","sourceRoot":"","sources":["src/pre-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAE,oBAAoB,EAAC,MAAM,WAAW,CAAC;AAEpE;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,mBAItC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,mBAItC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,EAAE,IAAI,CAAC,oBAAoB,EAAE,WAAW,GAAG,UAAU,CAG5F,CAAC"}
|
package/pre-config.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash generator pre configuration for making random self-validate **secrets**.
|
|
3
|
+
*/
|
|
4
|
+
export const secretGeneratorPreConfig = {
|
|
5
|
+
algorithm: 'sha256',
|
|
6
|
+
encoding: 'base64url',
|
|
7
|
+
crcLength: 4,
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Hash generator pre configuration for making random self-validate **user-id**.
|
|
11
|
+
*/
|
|
12
|
+
export const userIdGeneratorPreConfig = {
|
|
13
|
+
algorithm: 'sha1',
|
|
14
|
+
encoding: 'base64url',
|
|
15
|
+
crcLength: 4,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Token generator pre configuration for making secure self-validate **user-token**.
|
|
19
|
+
*/
|
|
20
|
+
export const userTokenGeneratorPreConfig = {
|
|
21
|
+
algorithm: 'sha224',
|
|
22
|
+
encoding: 'base64url',
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=pre-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pre-config.js","sourceRoot":"","sources":["src/pre-config.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAwB;IAC3D,SAAS,EAAE,QAAQ;IACnB,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAwB;IAC3D,SAAS,EAAE,MAAM;IACjB,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAyD;IAC/F,SAAS,EAAE,QAAQ;IACnB,QAAQ,EAAE,WAAW;CACtB,CAAC","sourcesContent":["import {HashGeneratorConfig, TokenGeneratorConfig} from './type.js';\n\n/**\n * Hash generator pre configuration for making random self-validate **secrets**.\n */\nexport const secretGeneratorPreConfig: HashGeneratorConfig = {\n algorithm: 'sha256',\n encoding: 'base64url',\n crcLength: 4,\n};\n\n/**\n * Hash generator pre configuration for making random self-validate **user-id**.\n */\nexport const userIdGeneratorPreConfig: HashGeneratorConfig = {\n algorithm: 'sha1',\n encoding: 'base64url',\n crcLength: 4,\n};\n\n/**\n * Token generator pre configuration for making secure self-validate **user-token**.\n */\nexport const userTokenGeneratorPreConfig: Pick<TokenGeneratorConfig, 'algorithm' | 'encoding'> = {\n algorithm: 'sha224',\n encoding: 'base64url',\n};\n"]}
|
package/user.d.ts
CHANGED
|
@@ -2,12 +2,12 @@ import { AlwatrHashGenerator } from './hash.js';
|
|
|
2
2
|
import { AlwatrTokenGenerator } from './token.js';
|
|
3
3
|
import type { HashGeneratorConfig, TokenGeneratorConfig, TokenStatus } from './type.js';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* User factory for generating self-validate user-id and user-token.
|
|
6
6
|
*/
|
|
7
7
|
export declare class AlwatrUserFactory {
|
|
8
8
|
protected _tokenGenerator: AlwatrTokenGenerator;
|
|
9
9
|
protected _hashGenerator: AlwatrHashGenerator;
|
|
10
|
-
constructor(
|
|
10
|
+
constructor(hashConfig: HashGeneratorConfig, tokenConfig: TokenGeneratorConfig);
|
|
11
11
|
/**
|
|
12
12
|
* Generate new self-verifiable user-id.
|
|
13
13
|
*
|
package/user.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAEhD,OAAO,KAAK,EAAC,mBAAmB,EAAE,oBAAoB,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEtF;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,SAAS,CAAC,eAAe,uBAAC;IAC1B,SAAS,CAAC,cAAc,sBAAC;gBAGrB,
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAEhD,OAAO,KAAK,EAAC,mBAAmB,EAAE,oBAAoB,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEtF;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,SAAS,CAAC,eAAe,uBAAC;IAC1B,SAAS,CAAC,cAAc,sBAAC;gBAGrB,UAAU,EAAE,mBAAmB,EAC/B,WAAW,EAAE,oBAAoB;IAMrC;;;;;;;;;;;OAWG;IACH,UAAU,IAAI,MAAM;IAIpB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI7B;;;;;;;;OAQG;IACH,aAAa,CAAC,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GAAG,MAAM;IAIrE;;;;;;;;;;OAUG;IACH,WAAW,CAAC,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW;CAGxF"}
|
package/user.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { AlwatrHashGenerator } from './hash.js';
|
|
2
2
|
import { AlwatrTokenGenerator } from './token.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* User factory for generating self-validate user-id and user-token.
|
|
5
5
|
*/
|
|
6
6
|
export class AlwatrUserFactory {
|
|
7
|
-
constructor(
|
|
8
|
-
this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig);
|
|
7
|
+
constructor(hashConfig, tokenConfig) {
|
|
9
8
|
this._hashGenerator = new AlwatrHashGenerator(hashConfig);
|
|
9
|
+
this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig);
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* Generate new self-verifiable user-id.
|
package/user.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.js","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAIhD;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAI5B,YACI,
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAIhD;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAI5B,YACI,UAA+B,EAC/B,WAAiC;QAEnC,IAAI,CAAC,cAAc,GAAG,IAAI,mBAAmB,CAAC,UAAU,CAAC,CAAC;QAC1D,IAAI,CAAC,eAAe,GAAG,IAAI,oBAAoB,CAAC,WAAW,CAAC,CAAC;IAC/D,CAAC;IAED;;;;;;;;;;;OAWG;IACH,UAAU;QACR,OAAO,GAAG,GAAG,IAAI,CAAC,cAAc,CAAC,kBAAkB,EAAE,CAAC;IACxD,CAAC;IAED;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAU;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC,kBAAkB,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IACjE,CAAC;IAED;;;;;;;;OAQG;IACH,aAAa,CAAC,YAA8C;QAC1D,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;;;;OAUG;IACH,WAAW,CAAC,YAA8C,EAAE,KAAa;QACvE,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;IACjE,CAAC;CACF","sourcesContent":["import {AlwatrHashGenerator} from './hash.js';\nimport {AlwatrTokenGenerator} from './token.js';\n\nimport type {HashGeneratorConfig, TokenGeneratorConfig, TokenStatus} from './type.js';\n\n/**\n * User factory for generating self-validate user-id and user-token.\n */\nexport class AlwatrUserFactory {\n protected _tokenGenerator;\n protected _hashGenerator;\n\n constructor(\n hashConfig: HashGeneratorConfig,\n tokenConfig: TokenGeneratorConfig,\n ) {\n this._hashGenerator = new AlwatrHashGenerator(hashConfig);\n this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig);\n }\n\n /**\n * Generate new self-verifiable user-id.\n *\n * Example:\n *\n * ```ts\n * const newUser = {\n * id: userFactory.generateId(),\n * ...\n * }\n * ```\n */\n generateId(): string {\n return 'U' + this._hashGenerator.randomSelfValidate();\n }\n\n /**\n * Validate user-id without token.\n *\n * Example:\n *\n * ```ts\n * if (!userFactory.verifyId(user.id)) {\n * new Error('invalid_user');\n * }\n * ```\n */\n verifyId(id: string): boolean {\n return this._hashGenerator.verifySelfValidate(id.substring(1));\n }\n\n /**\n * Generate user auth token.\n *\n * Example:\n *\n * ```ts\n * const userToken = userFactory.generateToken([user.id, user.lpe]);\n * ```\n */\n generateToken(uniquelyList: Array<string | number | boolean>): string {\n return this._tokenGenerator.generate(uniquelyList.join());\n }\n\n /**\n * Verify user auth token.\n *\n * Example:\n *\n * ```ts\n * if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {\n * new error('invalid_token');\n * }\n * ```\n */\n verifyToken(uniquelyList: Array<string | number | boolean>, token: string): TokenStatus {\n return this._tokenGenerator.verify(uniquelyList.join(), token);\n }\n}\n"]}
|