@alwatr/crypto 1.2.0 → 2.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 +20 -0
- package/README.md +8 -1
- package/hash.d.ts +46 -41
- package/hash.d.ts.map +1 -1
- package/hash.js +52 -47
- package/hash.js.map +1 -1
- package/package.json +7 -8
- package/pre-config.d.ts +1 -1
- package/pre-config.d.ts.map +1 -1
- package/pre-config.js +4 -1
- package/pre-config.js.map +1 -1
- package/token.d.ts +25 -7
- package/token.d.ts.map +1 -1
- package/token.js +29 -11
- package/token.js.map +1 -1
- package/type.d.ts +5 -3
- package/type.d.ts.map +1 -1
- package/type.js.map +1 -1
- package/user.d.ts +34 -29
- package/user.d.ts.map +1 -1
- package/user.js +37 -32
- package/user.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,26 @@
|
|
|
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
|
+
# [2.0.0](https://github.com/Alwatr/eslib/compare/@alwatr/crypto@1.2.1...@alwatr/crypto@2.0.0) (2023-11-29)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **crypto/hash:** review and update all methods and documents ([14acd26](https://github.com/Alwatr/eslib/commit/14acd265a19b1b67bd32f725468fe66985464fe6)) by @AliMD
|
|
11
|
+
* **crypto/preConfig:** update prefix and secret algorithm ([0cfff12](https://github.com/Alwatr/eslib/commit/0cfff124e692d02aad0b5c97908df63bc692f896)) by @AliMD
|
|
12
|
+
* **crypto/token:** review and update all methods and documents ([dc943f8](https://github.com/Alwatr/eslib/commit/dc943f8a007567b58e9e3b7f9cada556ac76ae9b)) by @AliMD
|
|
13
|
+
* **crypto/user:** review and update all methods and documents ([bb79fa8](https://github.com/Alwatr/eslib/commit/bb79fa81f8632d5fe75cac813238b04094d0bb6a)) by @AliMD
|
|
14
|
+
* **crypto:** prefix option ([6be5c90](https://github.com/Alwatr/eslib/commit/6be5c90dad4674e8ae3e27611a13dcf1e08ce11a)) by @AliMD
|
|
15
|
+
|
|
16
|
+
### BREAKING CHANGES
|
|
17
|
+
|
|
18
|
+
* **crypto/user:** methods name updated
|
|
19
|
+
* **crypto/token:** methods name updated
|
|
20
|
+
* **crypto/hash:** methods name updated
|
|
21
|
+
|
|
22
|
+
## [1.2.1](https://github.com/Alwatr/eslib/compare/@alwatr/crypto@1.2.0...@alwatr/crypto@1.2.1) (2023-11-23)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
25
|
+
|
|
6
26
|
# [1.2.0](https://github.com/Alwatr/eslib/compare/@alwatr/crypto@1.1.12...@alwatr/crypto@1.2.0) (2023-11-14)
|
|
7
27
|
|
|
8
28
|
### Features
|
package/README.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# Alwatr Crypto - `@alwatr/crypto`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A robust generator of secure authentication HOTP tokens, employing the HMAC-based One-Time Password algorithm, accompanied by a suite of cryptographic utilities, all encapsulated within a compact TypeScript module.
|
|
4
|
+
|
|
5
|
+
**This package includes:**
|
|
6
|
+
|
|
7
|
+
1. [AlwatrHashGenerator](./src/hash.ts): Secure **self-validate** hash generator.
|
|
8
|
+
2. [AlwatrTokenGenerator](./src/token.ts): Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).
|
|
9
|
+
3. [AlwatrUserGenerator](./src/user.ts): User factory for generating self-validate user-id and user-token.
|
|
10
|
+
4. [PreConfiguration](./src/pre-config.ts): Pre-configuration object for the hash/token generators.
|
|
4
11
|
|
|
5
12
|
## References
|
|
6
13
|
|
package/hash.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/// <reference types="node"
|
|
1
|
+
/// <reference types="node" />
|
|
2
2
|
import { type BinaryLike } from 'node:crypto';
|
|
3
3
|
import type { HashGeneratorConfig } from './type.js';
|
|
4
4
|
/**
|
|
@@ -6,70 +6,75 @@ import type { HashGeneratorConfig } from './type.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export declare class AlwatrHashGenerator {
|
|
8
8
|
config: HashGeneratorConfig;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new instance of the AlwatrHashGenerator class.
|
|
11
|
+
* @param config The configuration for the hash generator.
|
|
12
|
+
*/
|
|
9
13
|
constructor(config: HashGeneratorConfig);
|
|
10
14
|
/**
|
|
11
|
-
* Generate
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* const clientId = hashGenerator.random();
|
|
15
|
+
* Generate a random hash.
|
|
16
|
+
* @returns The generated hash.
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const clientId = hashGenerator.generateRandom();
|
|
17
20
|
* ```
|
|
18
21
|
*/
|
|
19
|
-
|
|
22
|
+
generateRandom(): string;
|
|
20
23
|
/**
|
|
21
|
-
* Generate **self-validate**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* const userId = hashGenerator.randomSelfValidate();
|
|
24
|
+
* Generate a **self-validate** random hash.
|
|
25
|
+
* @returns The generated self-validated hash.
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const userId = hashGenerator.generateRandomSelfValidate();
|
|
27
29
|
* ```
|
|
28
30
|
*/
|
|
29
|
-
|
|
31
|
+
generateRandomSelfValidate(): string;
|
|
30
32
|
/**
|
|
31
|
-
* Generate
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* ```
|
|
36
|
-
* const crcHash = hashGenerator.generate(
|
|
33
|
+
* Generate a hash from data.
|
|
34
|
+
* @param data - The data to generate the hash from.
|
|
35
|
+
* @returns The generated hash.
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const crcHash = hashGenerator.generate(data);
|
|
37
39
|
* ```
|
|
38
40
|
*/
|
|
39
41
|
generate(data: BinaryLike): string;
|
|
40
42
|
/**
|
|
41
|
-
* Generate crc hash.
|
|
43
|
+
* Generate a crc hash.
|
|
44
|
+
* @param data - The data to generate the crc hash from.
|
|
45
|
+
* @returns The generated crc hash.
|
|
42
46
|
*/
|
|
43
|
-
|
|
47
|
+
generateCrc(data: BinaryLike): string;
|
|
44
48
|
/**
|
|
45
|
-
* Generate **self-validate** hash from data.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* ```
|
|
50
|
-
* const userId = hashGenerator.generateSelfValidate(
|
|
49
|
+
* Generate a **self-validate** hash from data.
|
|
50
|
+
* @param data - The data to generate the self-validated hash from.
|
|
51
|
+
* @returns The generated self-validated hash.
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const userId = hashGenerator.generateSelfValidate(data);
|
|
51
55
|
* ```
|
|
52
56
|
*/
|
|
53
57
|
generateSelfValidate(data: BinaryLike): string;
|
|
54
58
|
/**
|
|
55
|
-
* Verify
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* Verify if the generated hash matches the provided hash.
|
|
60
|
+
* @param data - The data to verify.
|
|
61
|
+
* @param hash - The hash to compare against.
|
|
62
|
+
* @returns `true` if the hash is verified, `false` otherwise.
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* if (!hashGenerator.verify(data, hash)) {
|
|
61
66
|
* new Error('data_corrupted');
|
|
62
67
|
* }
|
|
63
68
|
* ```
|
|
64
69
|
*/
|
|
65
70
|
verify(data: BinaryLike, hash: string): boolean;
|
|
66
71
|
/**
|
|
67
|
-
* Verify a **self-validate** hash to check
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* ```
|
|
72
|
-
* if (!hashGenerator.verifySelfValidate(
|
|
72
|
+
* Verify a **self-validate** hash to check if it was generated by this class (with the same options).
|
|
73
|
+
* @param hash - The self-validated hash to verify.
|
|
74
|
+
* @returns `true` if the hash is verified, `false` otherwise.
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* if (!hashGenerator.verifySelfValidate(hash)) {
|
|
73
78
|
* new Error('invalid_user');
|
|
74
79
|
* }
|
|
75
80
|
* ```
|
package/hash.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":";AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":";AAAA,OAAO,EAA2B,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAErD;;GAEG;AACH,qBAAa,mBAAmB;IAKX,MAAM,EAAE,mBAAmB;IAJ9C;;;OAGG;gBACgB,MAAM,EAAE,mBAAmB;IAE9C;;;;;;;OAOG;IACH,cAAc,IAAI,MAAM;IAIxB;;;;;;;OAOG;IACH,0BAA0B,IAAI,MAAM;IAIpC;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAIlC;;;;OAIG;IACH,WAAW,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAKrC;;;;;;;;OAQG;IACH,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAM9C;;;;;;;;;;;OAWG;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,73 +3,78 @@ import { createHash, randomBytes } from 'node:crypto';
|
|
|
3
3
|
* Secure **self-validate** hash generator.
|
|
4
4
|
*/
|
|
5
5
|
export class AlwatrHashGenerator {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new instance of the AlwatrHashGenerator class.
|
|
8
|
+
* @param config The configuration for the hash generator.
|
|
9
|
+
*/
|
|
6
10
|
constructor(config) {
|
|
7
11
|
this.config = config;
|
|
8
12
|
}
|
|
9
13
|
/**
|
|
10
|
-
* Generate
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* const clientId = hashGenerator.random();
|
|
14
|
+
* Generate a random hash.
|
|
15
|
+
* @returns The generated hash.
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const clientId = hashGenerator.generateRandom();
|
|
16
19
|
* ```
|
|
17
20
|
*/
|
|
18
|
-
|
|
21
|
+
generateRandom() {
|
|
19
22
|
return this.generate(randomBytes(16));
|
|
20
23
|
}
|
|
21
24
|
/**
|
|
22
|
-
* Generate **self-validate**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* const userId = hashGenerator.randomSelfValidate();
|
|
25
|
+
* Generate a **self-validate** random hash.
|
|
26
|
+
* @returns The generated self-validated hash.
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const userId = hashGenerator.generateRandomSelfValidate();
|
|
28
30
|
* ```
|
|
29
31
|
*/
|
|
30
|
-
|
|
32
|
+
generateRandomSelfValidate() {
|
|
31
33
|
return this.generateSelfValidate(randomBytes(16));
|
|
32
34
|
}
|
|
33
35
|
/**
|
|
34
|
-
* Generate
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* ```
|
|
39
|
-
* const crcHash = hashGenerator.generate(
|
|
36
|
+
* Generate a hash from data.
|
|
37
|
+
* @param data - The data to generate the hash from.
|
|
38
|
+
* @returns The generated hash.
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const crcHash = hashGenerator.generate(data);
|
|
40
42
|
* ```
|
|
41
43
|
*/
|
|
42
44
|
generate(data) {
|
|
43
|
-
return createHash(this.config.algorithm).update(data).digest(this.config.encoding);
|
|
45
|
+
return this.config.prefix + createHash(this.config.algorithm).update(data).digest(this.config.encoding);
|
|
44
46
|
}
|
|
45
47
|
/**
|
|
46
|
-
* Generate crc hash.
|
|
48
|
+
* Generate a crc hash.
|
|
49
|
+
* @param data - The data to generate the crc hash from.
|
|
50
|
+
* @returns The generated crc hash.
|
|
47
51
|
*/
|
|
48
|
-
|
|
52
|
+
generateCrc(data) {
|
|
49
53
|
const crc = createHash('sha1').update(data).digest(this.config.encoding);
|
|
50
|
-
return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.
|
|
54
|
+
return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.slice(0, this.config.crcLength);
|
|
51
55
|
}
|
|
52
56
|
/**
|
|
53
|
-
* Generate **self-validate** hash from data.
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* ```
|
|
58
|
-
* const userId = hashGenerator.generateSelfValidate(
|
|
57
|
+
* Generate a **self-validate** hash from data.
|
|
58
|
+
* @param data - The data to generate the self-validated hash from.
|
|
59
|
+
* @returns The generated self-validated hash.
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const userId = hashGenerator.generateSelfValidate(data);
|
|
59
63
|
* ```
|
|
60
64
|
*/
|
|
61
65
|
generateSelfValidate(data) {
|
|
62
66
|
const mainHash = this.generate(data);
|
|
63
|
-
const crcHash = this.
|
|
67
|
+
const crcHash = this.generateCrc(mainHash);
|
|
64
68
|
return mainHash + crcHash;
|
|
65
69
|
}
|
|
66
70
|
/**
|
|
67
|
-
* Verify
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
71
|
+
* Verify if the generated hash matches the provided hash.
|
|
72
|
+
* @param data - The data to verify.
|
|
73
|
+
* @param hash - The hash to compare against.
|
|
74
|
+
* @returns `true` if the hash is verified, `false` otherwise.
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* if (!hashGenerator.verify(data, hash)) {
|
|
73
78
|
* new Error('data_corrupted');
|
|
74
79
|
* }
|
|
75
80
|
* ```
|
|
@@ -78,23 +83,23 @@ export class AlwatrHashGenerator {
|
|
|
78
83
|
return hash === this.generate(data);
|
|
79
84
|
}
|
|
80
85
|
/**
|
|
81
|
-
* Verify a **self-validate** hash to check
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* ```
|
|
86
|
-
* if (!hashGenerator.verifySelfValidate(
|
|
86
|
+
* Verify a **self-validate** hash to check if it was generated by this class (with the same options).
|
|
87
|
+
* @param hash - The self-validated hash to verify.
|
|
88
|
+
* @returns `true` if the hash is verified, `false` otherwise.
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* if (!hashGenerator.verifySelfValidate(hash)) {
|
|
87
92
|
* new Error('invalid_user');
|
|
88
93
|
* }
|
|
89
94
|
* ```
|
|
90
95
|
*/
|
|
91
96
|
verifySelfValidate(hash) {
|
|
92
97
|
const gapPos = this.config.crcLength == null || this.config.crcLength < 1
|
|
93
|
-
? hash.length / 2
|
|
98
|
+
? hash.length - (hash.length - this.config.prefix.length) / 2
|
|
94
99
|
: hash.length - this.config.crcLength;
|
|
95
|
-
const mainHash = hash.
|
|
96
|
-
const crcHash = hash.
|
|
97
|
-
return crcHash === this.
|
|
100
|
+
const mainHash = hash.slice(0, gapPos);
|
|
101
|
+
const crcHash = hash.slice(gapPos);
|
|
102
|
+
return crcHash === this.generateCrc(mainHash);
|
|
98
103
|
}
|
|
99
104
|
}
|
|
100
105
|
//# sourceMappingURL=hash.js.map
|
package/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"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QACnC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACrC,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QAC3C,OAAO,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;;OAWG;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,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC;YAC7D,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;QAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QACvC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACnC,OAAO,OAAO,KAAK,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAChD,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 /**\n * Creates a new instance of the AlwatrHashGenerator class.\n * @param config The configuration for the hash generator.\n */\n constructor(public config: HashGeneratorConfig) {}\n\n /**\n * Generate a random hash.\n * @returns The generated hash.\n * @example\n * ```typescript\n * const clientId = hashGenerator.generateRandom();\n * ```\n */\n generateRandom(): string {\n return this.generate(randomBytes(16));\n }\n\n /**\n * Generate a **self-validate** random hash.\n * @returns The generated self-validated hash.\n * @example\n * ```typescript\n * const userId = hashGenerator.generateRandomSelfValidate();\n * ```\n */\n generateRandomSelfValidate(): string {\n return this.generateSelfValidate(randomBytes(16));\n }\n\n /**\n * Generate a hash from data.\n * @param data - The data to generate the hash from.\n * @returns The generated hash.\n * @example\n * ```typescript\n * const crcHash = hashGenerator.generate(data);\n * ```\n */\n generate(data: BinaryLike): string {\n return this.config.prefix + createHash(this.config.algorithm).update(data).digest(this.config.encoding);\n }\n\n /**\n * Generate a crc hash.\n * @param data - The data to generate the crc hash from.\n * @returns The generated 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.slice(0, this.config.crcLength);\n }\n\n /**\n * Generate a **self-validate** hash from data.\n * @param data - The data to generate the self-validated hash from.\n * @returns The generated self-validated hash.\n * @example\n * ```typescript\n * const userId = hashGenerator.generateSelfValidate(data);\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 if the generated hash matches the provided hash.\n * @param data - The data to verify.\n * @param hash - The hash to compare against.\n * @returns `true` if the hash is verified, `false` otherwise.\n * @example\n * ```typescript\n * if (!hashGenerator.verify(data, hash)) {\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 if it was generated by this class (with the same options).\n * @param hash - The self-validated hash to verify.\n * @returns `true` if the hash is verified, `false` otherwise.\n * @example\n * ```typescript\n * if (!hashGenerator.verifySelfValidate(hash)) {\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 - (hash.length - this.config.prefix.length) / 2\n : hash.length - this.config.crcLength;\n const mainHash = hash.slice(0, gapPos);\n const crcHash = hash.slice(gapPos);\n return crcHash === this.generateCrc(mainHash);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/crypto",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A robust generator of secure authentication HOTP tokens, employing the HMAC-based One-Time Password algorithm, accompanied by a suite of cryptographic utilities, all encapsulated within a compact TypeScript module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
7
7
|
"hash",
|
|
@@ -40,13 +40,12 @@
|
|
|
40
40
|
"url": "https://github.com/Alwatr/eslib/issues"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@alwatr/logger": "^2.
|
|
44
|
-
"@alwatr/math": "^1.2.
|
|
45
|
-
"@alwatr/util": "^1.3.
|
|
46
|
-
"tslib": "^2.6.2"
|
|
43
|
+
"@alwatr/logger": "^2.3.1",
|
|
44
|
+
"@alwatr/math": "^1.2.2",
|
|
45
|
+
"@alwatr/util": "^1.3.2"
|
|
47
46
|
},
|
|
48
47
|
"devDependencies": {
|
|
49
|
-
"@types/node": "^20.
|
|
48
|
+
"@types/node": "^20.10.0"
|
|
50
49
|
},
|
|
51
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "27cb935580d5ccdc4459f1018c66f23ea0a42ddf"
|
|
52
51
|
}
|
package/pre-config.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ export declare const userIdGeneratorPreConfig: HashGeneratorConfig;
|
|
|
10
10
|
/**
|
|
11
11
|
* Token generator pre configuration for making secure self-validate **user-token**.
|
|
12
12
|
*/
|
|
13
|
-
export declare const userTokenGeneratorPreConfig: Pick<TokenGeneratorConfig, 'algorithm' | 'encoding'>;
|
|
13
|
+
export declare const userTokenGeneratorPreConfig: Pick<TokenGeneratorConfig, 'algorithm' | 'encoding' | 'prefix'>;
|
|
14
14
|
//# sourceMappingURL=pre-config.d.ts.map
|
package/pre-config.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
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,mBAKtC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,mBAKtC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,EAAE,IAAI,CAAC,oBAAoB,EAAE,WAAW,GAAG,UAAU,GAAG,QAAQ,CAIvG,CAAC"}
|
package/pre-config.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Hash generator pre configuration for making random self-validate **secrets**.
|
|
3
3
|
*/
|
|
4
4
|
export const secretGeneratorPreConfig = {
|
|
5
|
-
|
|
5
|
+
prefix: 's',
|
|
6
|
+
algorithm: 'sha384',
|
|
6
7
|
encoding: 'base64url',
|
|
7
8
|
crcLength: 4,
|
|
8
9
|
};
|
|
@@ -10,6 +11,7 @@ export const secretGeneratorPreConfig = {
|
|
|
10
11
|
* Hash generator pre configuration for making random self-validate **user-id**.
|
|
11
12
|
*/
|
|
12
13
|
export const userIdGeneratorPreConfig = {
|
|
14
|
+
prefix: 'u',
|
|
13
15
|
algorithm: 'sha1',
|
|
14
16
|
encoding: 'base64url',
|
|
15
17
|
crcLength: 4,
|
|
@@ -18,6 +20,7 @@ export const userIdGeneratorPreConfig = {
|
|
|
18
20
|
* Token generator pre configuration for making secure self-validate **user-token**.
|
|
19
21
|
*/
|
|
20
22
|
export const userTokenGeneratorPreConfig = {
|
|
23
|
+
prefix: 't',
|
|
21
24
|
algorithm: 'sha224',
|
|
22
25
|
encoding: 'base64url',
|
|
23
26
|
};
|
package/pre-config.js.map
CHANGED
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"pre-config.js","sourceRoot":"","sources":["src/pre-config.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAwB;IAC3D,MAAM,EAAE,GAAG;IACX,SAAS,EAAE,QAAQ;IACnB,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAwB;IAC3D,MAAM,EAAE,GAAG;IACX,SAAS,EAAE,MAAM;IACjB,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAoE;IAC1G,MAAM,EAAE,GAAG;IACX,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 prefix: 's',\n algorithm: 'sha384',\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 prefix: 'u',\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' | 'prefix'> = {\n prefix: 't',\n algorithm: 'sha224',\n encoding: 'base64url',\n};\n"]}
|
package/token.d.ts
CHANGED
|
@@ -5,24 +5,42 @@ import type { TokenGeneratorConfig, TokenStatus } from './type.js';
|
|
|
5
5
|
export declare class AlwatrTokenGenerator {
|
|
6
6
|
config: TokenGeneratorConfig;
|
|
7
7
|
protected _duration: number | null;
|
|
8
|
+
/**
|
|
9
|
+
* The current epoch based on the configured duration.
|
|
10
|
+
*/
|
|
8
11
|
get epoch(): number;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new instance of AlwatrTokenGenerator.
|
|
14
|
+
* @param config The configuration for the token generator.
|
|
15
|
+
*/
|
|
9
16
|
constructor(config: TokenGeneratorConfig);
|
|
10
|
-
protected _generate(data: string, epoch: number): string;
|
|
11
17
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
18
|
+
* Generates a HOTP token based on the provided data for special duration.
|
|
19
|
+
* @param data The data to generate the token from.
|
|
20
|
+
* @returns The generated token.
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
15
23
|
* user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);
|
|
16
24
|
* ```
|
|
17
25
|
*/
|
|
18
26
|
generate(data: string): string;
|
|
19
27
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
28
|
+
* Verifies if a token is valid.
|
|
29
|
+
* @param data The data used to generate the token.
|
|
30
|
+
* @param token The token to verify.
|
|
31
|
+
* @returns The status of the token verification.
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
23
34
|
* const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);
|
|
24
35
|
* ```
|
|
25
36
|
*/
|
|
26
37
|
verify(data: string, token: string): TokenStatus;
|
|
38
|
+
/**
|
|
39
|
+
* Generates a cryptographic token based on the provided data and epoch.
|
|
40
|
+
* @param data - The data to be used in the token generation.
|
|
41
|
+
* @param epoch - The epoch value to be used in the token generation.
|
|
42
|
+
* @returns The generated cryptographic token.
|
|
43
|
+
*/
|
|
44
|
+
protected _generate(data: string, epoch: number): string;
|
|
27
45
|
}
|
|
28
46
|
//# sourceMappingURL=token.d.ts.map
|
package/token.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["src/token.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,oBAAoB,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEjE;;GAEG;AACH,qBAAa,oBAAoB;
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["src/token.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,oBAAoB,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEjE;;GAEG;AACH,qBAAa,oBAAoB;IAcZ,MAAM,EAAE,oBAAoB;IAb/C,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAEnC;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,CAElB;IAED;;;OAGG;gBACgB,MAAM,EAAE,oBAAoB;IAI/C;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAI9B;;;;;;;;;OASG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW;IAgBhD;;;;;OAKG;IACH,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAKzD"}
|
package/token.js
CHANGED
|
@@ -4,22 +4,26 @@ import { parseDuration } from '@alwatr/math';
|
|
|
4
4
|
* Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).
|
|
5
5
|
*/
|
|
6
6
|
export class AlwatrTokenGenerator {
|
|
7
|
+
/**
|
|
8
|
+
* The current epoch based on the configured duration.
|
|
9
|
+
*/
|
|
7
10
|
get epoch() {
|
|
8
11
|
return this._duration == null ? 0 : Math.floor(Date.now() / this._duration);
|
|
9
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new instance of AlwatrTokenGenerator.
|
|
15
|
+
* @param config The configuration for the token generator.
|
|
16
|
+
*/
|
|
10
17
|
constructor(config) {
|
|
11
18
|
this.config = config;
|
|
12
19
|
this._duration = config.duration == null ? null : parseDuration(config.duration);
|
|
13
20
|
}
|
|
14
|
-
_generate(data, epoch) {
|
|
15
|
-
return createHmac(this.config.algorithm, data)
|
|
16
|
-
.update(data + epoch)
|
|
17
|
-
.digest(this.config.encoding);
|
|
18
|
-
}
|
|
19
21
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
22
|
+
* Generates a HOTP token based on the provided data for special duration.
|
|
23
|
+
* @param data The data to generate the token from.
|
|
24
|
+
* @returns The generated token.
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
23
27
|
* user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);
|
|
24
28
|
* ```
|
|
25
29
|
*/
|
|
@@ -27,9 +31,12 @@ export class AlwatrTokenGenerator {
|
|
|
27
31
|
return this._generate(data, this.epoch);
|
|
28
32
|
}
|
|
29
33
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
34
|
+
* Verifies if a token is valid.
|
|
35
|
+
* @param data The data used to generate the token.
|
|
36
|
+
* @param token The token to verify.
|
|
37
|
+
* @returns The status of the token verification.
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
33
40
|
* const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);
|
|
34
41
|
* ```
|
|
35
42
|
*/
|
|
@@ -48,5 +55,16 @@ export class AlwatrTokenGenerator {
|
|
|
48
55
|
return 'invalid';
|
|
49
56
|
}
|
|
50
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Generates a cryptographic token based on the provided data and epoch.
|
|
60
|
+
* @param data - The data to be used in the token generation.
|
|
61
|
+
* @param epoch - The epoch value to be used in the token generation.
|
|
62
|
+
* @returns The generated cryptographic token.
|
|
63
|
+
*/
|
|
64
|
+
_generate(data, epoch) {
|
|
65
|
+
return this.config.prefix + createHmac(this.config.algorithm, data)
|
|
66
|
+
.update(data + epoch)
|
|
67
|
+
.digest(this.config.encoding);
|
|
68
|
+
}
|
|
51
69
|
}
|
|
52
70
|
//# sourceMappingURL=token.js.map
|
package/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["src/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAC,aAAa,EAAC,MAAM,cAAc,CAAC;AAI3C;;GAEG;AACH,MAAM,OAAO,oBAAoB;IAG/B,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC;IAC9E,CAAC;IAED,YAAmB,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;QAC7C,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,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D,OAAO,SAAS,CAAC;QACnB,CAAC;aACI,CAAC;YACJ,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACO,SAAS,CAAC,IAAY,EAAE,KAAa;QAC7C,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC;aAChE,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC;aACpB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAClC,CAAC;CACF","sourcesContent":["import {createHmac} from 'node:crypto';\n\nimport {parseDuration} from '@alwatr/math';\n\nimport type {TokenGeneratorConfig, TokenStatus} from './type.js';\n\n/**\n * Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).\n */\nexport class AlwatrTokenGenerator {\n protected _duration: number | null;\n\n /**\n * The current epoch based on the configured duration.\n */\n get epoch(): number {\n return this._duration == null ? 0 : Math.floor(Date.now() / this._duration);\n }\n\n /**\n * Creates a new instance of AlwatrTokenGenerator.\n * @param config The configuration for the token generator.\n */\n constructor(public config: TokenGeneratorConfig) {\n this._duration = config.duration == null ? null : parseDuration(config.duration);\n }\n\n /**\n * Generates a HOTP token based on the provided data for special duration.\n * @param data The data to generate the token from.\n * @returns The generated token.\n * @example\n * ```typescript\n * user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);\n * ```\n */\n generate(data: string): string {\n return this._generate(data, this.epoch);\n }\n\n /**\n * Verifies if a token is valid.\n * @param data The data used to generate the token.\n * @param token The token to verify.\n * @returns The status of the token verification.\n * @example\n * ```typescript\n * const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);\n * ```\n */\n verify(data: string, token: string): TokenStatus {\n const epoch = this.epoch;\n if (token === this._generate(data, epoch)) {\n return 'valid';\n }\n else if (this._duration == null) {\n return 'invalid';\n }\n else if (token === this._generate(data, epoch - 1)) {\n return 'expired';\n }\n else {\n return 'invalid';\n }\n }\n\n /**\n * Generates a cryptographic token based on the provided data and epoch.\n * @param data - The data to be used in the token generation.\n * @param epoch - The epoch value to be used in the token generation.\n * @returns The generated cryptographic token.\n */\n protected _generate(data: string, epoch: number): string {\n return this.config.prefix + createHmac(this.config.algorithm, data)\n .update(data + epoch)\n .digest(this.config.encoding);\n }\n}\n"]}
|
package/type.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary';
|
|
|
4
4
|
export type TokenStatus = 'valid' | 'invalid' | 'expired';
|
|
5
5
|
export type HashStatus = 'valid' | 'invalid';
|
|
6
6
|
export interface TokenGeneratorConfig {
|
|
7
|
+
prefix: string;
|
|
7
8
|
/**
|
|
8
9
|
* Secret string data to generate token.
|
|
9
10
|
*/
|
|
@@ -24,6 +25,7 @@ export interface TokenGeneratorConfig {
|
|
|
24
25
|
encoding: CryptoEncoding;
|
|
25
26
|
}
|
|
26
27
|
export interface HashGeneratorConfig {
|
|
28
|
+
prefix: string;
|
|
27
29
|
/**
|
|
28
30
|
* OpenSSl digest algorithm.
|
|
29
31
|
*/
|
|
@@ -37,8 +39,8 @@ export interface HashGeneratorConfig {
|
|
|
37
39
|
*/
|
|
38
40
|
crcLength?: number;
|
|
39
41
|
}
|
|
40
|
-
export interface
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
export interface UserGeneratorConfig {
|
|
43
|
+
userId: HashGeneratorConfig;
|
|
44
|
+
token: TokenGeneratorConfig;
|
|
43
45
|
}
|
|
44
46
|
//# sourceMappingURL=type.d.ts.map
|
package/type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAEjD,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,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IAEhC;;OAEG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;CAC1B;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;IAEzB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,mBAAmB,CAAC;IAC5B,KAAK,EAAE,oBAAoB,CAAC;CAC7B"}
|
package/type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["import type {DurationString} from '@alwatr/math';\n\nexport type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';\nexport type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary';\n\nexport type TokenStatus = 'valid' | 'invalid' | 'expired';\nexport type HashStatus = 'valid' | 'invalid';\n\nexport interface TokenGeneratorConfig {\n /**\n * Secret string data to generate token.\n */\n secret: string;\n\n /**\n * Token expiration time.\n *\n * `null` mean without expiration time\n */\n duration: DurationString | null;\n\n /**\n * OpenSSl digest algorithm.\n */\n algorithm: CryptoAlgorithm;\n\n /**\n * Encoding of token.\n */\n encoding: CryptoEncoding;\n}\n\nexport interface HashGeneratorConfig {\n /**\n * OpenSSl digest algorithm.\n */\n algorithm: CryptoAlgorithm;\n\n /**\n * Encoding of hash.\n */\n encoding: CryptoEncoding;\n\n /**\n * CRC hash max length.\n */\n crcLength?: number;\n}\n\nexport interface
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["import type {DurationString} from '@alwatr/math';\n\nexport type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';\nexport type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary';\n\nexport type TokenStatus = 'valid' | 'invalid' | 'expired';\nexport type HashStatus = 'valid' | 'invalid';\n\nexport interface TokenGeneratorConfig {\n prefix: string;\n\n /**\n * Secret string data to generate token.\n */\n secret: string;\n\n /**\n * Token expiration time.\n *\n * `null` mean without expiration time\n */\n duration: DurationString | null;\n\n /**\n * OpenSSl digest algorithm.\n */\n algorithm: CryptoAlgorithm;\n\n /**\n * Encoding of token.\n */\n encoding: CryptoEncoding;\n}\n\nexport interface HashGeneratorConfig {\n prefix: string;\n\n /**\n * OpenSSl digest algorithm.\n */\n algorithm: CryptoAlgorithm;\n\n /**\n * Encoding of hash.\n */\n encoding: CryptoEncoding;\n\n /**\n * CRC hash max length.\n */\n crcLength?: number;\n}\n\nexport interface UserGeneratorConfig {\n userId: HashGeneratorConfig;\n token: TokenGeneratorConfig;\n}\n"]}
|
package/user.d.ts
CHANGED
|
@@ -1,56 +1,61 @@
|
|
|
1
1
|
import { AlwatrHashGenerator } from './hash.js';
|
|
2
2
|
import { AlwatrTokenGenerator } from './token.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { UserGeneratorConfig, TokenStatus } from './type.js';
|
|
4
4
|
/**
|
|
5
5
|
* User factory for generating self-validate user-id and user-token.
|
|
6
6
|
*/
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class AlwatrUserGenerator {
|
|
8
8
|
protected _tokenGenerator: AlwatrTokenGenerator;
|
|
9
9
|
protected _hashGenerator: AlwatrHashGenerator;
|
|
10
|
-
constructor(hashConfig: HashGeneratorConfig, tokenConfig: TokenGeneratorConfig);
|
|
11
10
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
* Creates a new instance of AlwatrUserFactory.
|
|
12
|
+
* @param hashConfig The configuration for the hash generator.
|
|
13
|
+
* @param tokenConfig The configuration for the token generator.
|
|
14
|
+
*/
|
|
15
|
+
constructor(config: UserGeneratorConfig);
|
|
16
|
+
/**
|
|
17
|
+
* Generates a new self-verifiable user ID.
|
|
18
|
+
* @returns The generated user ID.
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
17
21
|
* const newUser = {
|
|
18
|
-
* id: userFactory.
|
|
22
|
+
* id: userFactory.generateUserId(),
|
|
19
23
|
* ...
|
|
20
24
|
* }
|
|
21
25
|
* ```
|
|
22
26
|
*/
|
|
23
|
-
|
|
27
|
+
generateUserId(): string;
|
|
24
28
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* ```
|
|
30
|
-
* if (!userFactory.
|
|
31
|
-
* new Error('invalid_user');
|
|
29
|
+
* Validates a user ID without token.
|
|
30
|
+
* @param userId The user ID to verify.
|
|
31
|
+
* @returns A boolean indicating whether the user ID is valid.
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* if (!userFactory.verifyUserId(user.id)) {
|
|
35
|
+
* throw new Error('invalid_user');
|
|
32
36
|
* }
|
|
33
37
|
* ```
|
|
34
38
|
*/
|
|
35
|
-
|
|
39
|
+
verifyUserId(userId: string): boolean;
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* ```
|
|
41
|
+
* Generates a user authentication token.
|
|
42
|
+
* @param uniquelyList The list of values to generate the token from.
|
|
43
|
+
* @returns The generated user token.
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
42
46
|
* const userToken = userFactory.generateToken([user.id, user.lpe]);
|
|
43
47
|
* ```
|
|
44
48
|
*/
|
|
45
49
|
generateToken(uniquelyList: (string | number | boolean)[]): string;
|
|
46
50
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
51
|
+
* Verifies a user authentication token.
|
|
52
|
+
* @param uniquelyList The list of values used to generate the token.
|
|
53
|
+
* @param token The user token to verify.
|
|
54
|
+
* @returns The status of the token verification.
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
52
57
|
* if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {
|
|
53
|
-
* new
|
|
58
|
+
* throw new Error('invalid_token');
|
|
54
59
|
* }
|
|
55
60
|
* ```
|
|
56
61
|
*/
|
package/user.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD,OAAO,KAAK,EAAC,mBAAmB,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAEjE;;GAEG;AACH,qBAAa,mBAAmB;IAC9B,SAAS,CAAC,eAAe,EAAE,oBAAoB,CAAC;IAChD,SAAS,CAAC,cAAc,EAAE,mBAAmB,CAAC;IAE9C;;;;OAIG;gBACS,MAAM,EAAE,mBAAmB;IAKvC;;;;;;;;;;OAUG;IACH,cAAc,IAAI,MAAM;IAIxB;;;;;;;;;;OAUG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAIrC;;;;;;;;OAQG;IACH,aAAa,CAAC,YAAY,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,GAAG,MAAM;IAIlE;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,YAAY,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW;CAGrF"}
|
package/user.js
CHANGED
|
@@ -3,46 +3,50 @@ import { AlwatrTokenGenerator } from './token.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* User factory for generating self-validate user-id and user-token.
|
|
5
5
|
*/
|
|
6
|
-
export class
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
export class AlwatrUserGenerator {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new instance of AlwatrUserFactory.
|
|
9
|
+
* @param hashConfig The configuration for the hash generator.
|
|
10
|
+
* @param tokenConfig The configuration for the token generator.
|
|
11
|
+
*/
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this._hashGenerator = new AlwatrHashGenerator(config.userId);
|
|
14
|
+
this._tokenGenerator = new AlwatrTokenGenerator(config.token);
|
|
10
15
|
}
|
|
11
16
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
+
* Generates a new self-verifiable user ID.
|
|
18
|
+
* @returns The generated user ID.
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
17
21
|
* const newUser = {
|
|
18
|
-
* id: userFactory.
|
|
22
|
+
* id: userFactory.generateUserId(),
|
|
19
23
|
* ...
|
|
20
24
|
* }
|
|
21
25
|
* ```
|
|
22
26
|
*/
|
|
23
|
-
|
|
24
|
-
return
|
|
27
|
+
generateUserId() {
|
|
28
|
+
return this._hashGenerator.generateRandomSelfValidate();
|
|
25
29
|
}
|
|
26
30
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* ```
|
|
32
|
-
* if (!userFactory.
|
|
33
|
-
* new Error('invalid_user');
|
|
31
|
+
* Validates a user ID without token.
|
|
32
|
+
* @param userId The user ID to verify.
|
|
33
|
+
* @returns A boolean indicating whether the user ID is valid.
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* if (!userFactory.verifyUserId(user.id)) {
|
|
37
|
+
* throw new Error('invalid_user');
|
|
34
38
|
* }
|
|
35
39
|
* ```
|
|
36
40
|
*/
|
|
37
|
-
|
|
38
|
-
return this._hashGenerator.verifySelfValidate(
|
|
41
|
+
verifyUserId(userId) {
|
|
42
|
+
return this._hashGenerator.verifySelfValidate(userId);
|
|
39
43
|
}
|
|
40
44
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* ```
|
|
45
|
+
* Generates a user authentication token.
|
|
46
|
+
* @param uniquelyList The list of values to generate the token from.
|
|
47
|
+
* @returns The generated user token.
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
46
50
|
* const userToken = userFactory.generateToken([user.id, user.lpe]);
|
|
47
51
|
* ```
|
|
48
52
|
*/
|
|
@@ -50,13 +54,14 @@ export class AlwatrUserFactory {
|
|
|
50
54
|
return this._tokenGenerator.generate(uniquelyList.join());
|
|
51
55
|
}
|
|
52
56
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
57
|
+
* Verifies a user authentication token.
|
|
58
|
+
* @param uniquelyList The list of values used to generate the token.
|
|
59
|
+
* @param token The user token to verify.
|
|
60
|
+
* @returns The status of the token verification.
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
58
63
|
* if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {
|
|
59
|
-
* new
|
|
64
|
+
* throw new Error('invalid_token');
|
|
60
65
|
* }
|
|
61
66
|
* ```
|
|
62
67
|
*/
|
package/user.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.js","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAIlD;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAI9B;;;;OAIG;IACH,YAAY,MAA2B;QACrC,IAAI,CAAC,cAAc,GAAG,IAAI,mBAAmB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC7D,IAAI,CAAC,eAAe,GAAG,IAAI,oBAAoB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;IAED;;;;;;;;;;OAUG;IACH,cAAc;QACZ,OAAO,IAAI,CAAC,cAAc,CAAC,0BAA0B,EAAE,CAAC;IAC1D,CAAC;IAED;;;;;;;;;;OAUG;IACH,YAAY,CAAC,MAAc;QACzB,OAAO,IAAI,CAAC,cAAc,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;;OAQG;IACH,aAAa,CAAC,YAA2C;QACvD,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,YAA2C,EAAE,KAAa;QACpE,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 {UserGeneratorConfig, TokenStatus } from './type.js';\n\n/**\n * User factory for generating self-validate user-id and user-token.\n */\nexport class AlwatrUserGenerator {\n protected _tokenGenerator: AlwatrTokenGenerator;\n protected _hashGenerator: AlwatrHashGenerator;\n\n /**\n * Creates a new instance of AlwatrUserFactory.\n * @param hashConfig The configuration for the hash generator.\n * @param tokenConfig The configuration for the token generator.\n */\n constructor(config: UserGeneratorConfig) {\n this._hashGenerator = new AlwatrHashGenerator(config.userId);\n this._tokenGenerator = new AlwatrTokenGenerator(config.token);\n }\n\n /**\n * Generates a new self-verifiable user ID.\n * @returns The generated user ID.\n * @example\n * ```typescript\n * const newUser = {\n * id: userFactory.generateUserId(),\n * ...\n * }\n * ```\n */\n generateUserId(): string {\n return this._hashGenerator.generateRandomSelfValidate();\n }\n\n /**\n * Validates a user ID without token.\n * @param userId The user ID to verify.\n * @returns A boolean indicating whether the user ID is valid.\n * @example\n * ```typescript\n * if (!userFactory.verifyUserId(user.id)) {\n * throw new Error('invalid_user');\n * }\n * ```\n */\n verifyUserId(userId: string): boolean {\n return this._hashGenerator.verifySelfValidate(userId);\n }\n\n /**\n * Generates a user authentication token.\n * @param uniquelyList The list of values to generate the token from.\n * @returns The generated user token.\n * @example\n * ```typescript\n * const userToken = userFactory.generateToken([user.id, user.lpe]);\n * ```\n */\n generateToken(uniquelyList: (string | number | boolean)[]): string {\n return this._tokenGenerator.generate(uniquelyList.join());\n }\n\n /**\n * Verifies a user authentication token.\n * @param uniquelyList The list of values used to generate the token.\n * @param token The user token to verify.\n * @returns The status of the token verification.\n * @example\n * ```typescript\n * if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {\n * throw new Error('invalid_token');\n * }\n * ```\n */\n verifyToken(uniquelyList: (string | number | boolean)[], token: string): TokenStatus {\n return this._tokenGenerator.verify(uniquelyList.join(), token);\n }\n}\n"]}
|