@alwatr/crypto 1.2.1 → 3.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 +34 -0
- package/README.md +8 -1
- package/api.d.ts +129 -0
- package/api.d.ts.map +1 -0
- package/api.js +136 -0
- package/api.js.map +1 -0
- package/hash.d.ts +69 -43
- package/hash.d.ts.map +1 -1
- package/hash.js +53 -50
- package/hash.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js +2 -2
- package/index.js.map +1 -1
- package/package.json +10 -11
- package/pre-config.d.ts +12 -7
- package/pre-config.d.ts.map +1 -1
- package/pre-config.js +18 -8
- package/pre-config.js.map +1 -1
- package/token.d.ts +57 -12
- package/token.d.ts.map +1 -1
- package/token.js +40 -26
- package/token.js.map +1 -1
- package/type.d.ts +8 -41
- package/type.d.ts.map +1 -1
- package/type.js.map +1 -1
- package/user.d.ts +0 -59
- package/user.d.ts.map +0 -1
- package/user.js +0 -67
- package/user.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,40 @@
|
|
|
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
|
+
# [3.0.0](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/crypto@2.0.0...@alwatr/crypto@3.0.0) (2023-12-09)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **crypto:** use import type ([30e3bac](https://github.com/Alwatr/alwatr-es-sdk/commit/30e3bacb187d58417cb62e2a1511de4ade3f80c0)) by @njfamirm
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* **crypto/api:** AlwatrCryptoFactory with secret and device id ([2d754a1](https://github.com/Alwatr/alwatr-es-sdk/commit/2d754a19b2f04f64d0828e31ba004fc192f466d3)) by @njfamirm
|
|
15
|
+
* **crypto/api:** device id generator preconfig ([71e80d6](https://github.com/Alwatr/alwatr-es-sdk/commit/71e80d63743579505a6be17d014c364e9f6cf55c)) by @njfamirm
|
|
16
|
+
* **crypto:** complete rewrite with new api ([3d56861](https://github.com/Alwatr/alwatr-es-sdk/commit/3d56861a2857e760c7cd5f03be98f003738fc7a9)) by @AliMD
|
|
17
|
+
* **crypto:** rename user file to api ([52343ea](https://github.com/Alwatr/alwatr-es-sdk/commit/52343ead04c23f50bedac2caa01f46bf489ab318)) by @njfamirm
|
|
18
|
+
|
|
19
|
+
### BREAKING CHANGES
|
|
20
|
+
|
|
21
|
+
* **crypto:** new api
|
|
22
|
+
* **crypto/api:** rename AlwatrUserGenerator to AlwatrCryptoFactory and change config
|
|
23
|
+
|
|
24
|
+
# [2.0.0](https://github.com/Alwatr/eslib/compare/@alwatr/crypto@1.2.1...@alwatr/crypto@2.0.0) (2023-11-29)
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* **crypto/hash:** review and update all methods and documents ([14acd26](https://github.com/Alwatr/eslib/commit/14acd265a19b1b67bd32f725468fe66985464fe6)) by @AliMD
|
|
29
|
+
* **crypto/preConfig:** update prefix and secret algorithm ([0cfff12](https://github.com/Alwatr/eslib/commit/0cfff124e692d02aad0b5c97908df63bc692f896)) by @AliMD
|
|
30
|
+
* **crypto/token:** review and update all methods and documents ([dc943f8](https://github.com/Alwatr/eslib/commit/dc943f8a007567b58e9e3b7f9cada556ac76ae9b)) by @AliMD
|
|
31
|
+
* **crypto/user:** review and update all methods and documents ([bb79fa8](https://github.com/Alwatr/eslib/commit/bb79fa81f8632d5fe75cac813238b04094d0bb6a)) by @AliMD
|
|
32
|
+
* **crypto:** prefix option ([6be5c90](https://github.com/Alwatr/eslib/commit/6be5c90dad4674e8ae3e27611a13dcf1e08ce11a)) by @AliMD
|
|
33
|
+
|
|
34
|
+
### BREAKING CHANGES
|
|
35
|
+
|
|
36
|
+
* **crypto/user:** methods name updated
|
|
37
|
+
* **crypto/token:** methods name updated
|
|
38
|
+
* **crypto/hash:** methods name updated
|
|
39
|
+
|
|
6
40
|
## [1.2.1](https://github.com/Alwatr/eslib/compare/@alwatr/crypto@1.2.0...@alwatr/crypto@1.2.1) (2023-11-23)
|
|
7
41
|
|
|
8
42
|
**Note:** Version bump only for package @alwatr/crypto
|
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/api.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { DurationString } from '@alwatr/math';
|
|
2
|
+
import { AlwatrHashGenerator } from './hash.js';
|
|
3
|
+
import { AlwatrTokenGenerator, type TokenValidity } from './token.js';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for the CryptoFactory.
|
|
6
|
+
*/
|
|
7
|
+
export interface CryptoFactoryConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The secret used for encryption and decryption tokens.
|
|
10
|
+
*/
|
|
11
|
+
secret: string;
|
|
12
|
+
/**
|
|
13
|
+
* The duration for which the token is valid.
|
|
14
|
+
*/
|
|
15
|
+
duration: DurationString | 'infinite';
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Crypto factory for generating self-validate user-id, user-token, secret, device-id.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AlwatrCryptoFactory {
|
|
21
|
+
protected _generators: {
|
|
22
|
+
readonly secret: AlwatrHashGenerator;
|
|
23
|
+
readonly deviceId: AlwatrHashGenerator;
|
|
24
|
+
readonly userId: AlwatrHashGenerator;
|
|
25
|
+
readonly token: AlwatrTokenGenerator;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new instance of crypto factory.
|
|
29
|
+
* @param config The configuration used to create the crypto factory.
|
|
30
|
+
*/
|
|
31
|
+
constructor(config: CryptoFactoryConfig);
|
|
32
|
+
/**
|
|
33
|
+
* Generate self-verifiable user ID.
|
|
34
|
+
* @returns The generated user ID.
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const newUser = {
|
|
38
|
+
* id: cryptoFactory.generateUserId(),
|
|
39
|
+
* ...
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
generateUserId(): string;
|
|
44
|
+
/**
|
|
45
|
+
* Verify a user ID without token.
|
|
46
|
+
* @param userId The user ID to verify.
|
|
47
|
+
* @returns A boolean indicating whether the user ID is valid.
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* if (!cryptoFactory.verifyUserId(user.id)) {
|
|
51
|
+
* throw new Error('invalid_user');
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
verifyUserId(userId: string): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Generate authentication token.
|
|
58
|
+
* @param uniquelyList The list of uniq values to generate the token from.
|
|
59
|
+
* @returns The generated user token.
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const userToken = cryptoFactory.generateToken([user.id, user.lpe]);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
generateToken(uniquelyList: (string | number)[]): string;
|
|
66
|
+
/**
|
|
67
|
+
* Verify a authentication token.
|
|
68
|
+
* @param uniquelyList The list of uniq values used to generate the token.
|
|
69
|
+
* @param token The user token to verify.
|
|
70
|
+
* @returns The validity of the token.
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* if (!cryptoFactory.verifyToken([user.id, user.lpe], userToken)) {
|
|
74
|
+
* throw new Error('invalid_token');
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
verifyToken(uniquelyList: (string | number)[], token: string): TokenValidity;
|
|
79
|
+
/**
|
|
80
|
+
* Generate self-verifiable secret.
|
|
81
|
+
* @returns The generated secret.
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const config = {
|
|
85
|
+
* storageToken: cryptoFactory.generateSecret(),
|
|
86
|
+
* ...
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
generateSecret(): string;
|
|
91
|
+
/**
|
|
92
|
+
* Verify a secret.
|
|
93
|
+
* @param secret The secret to verify.
|
|
94
|
+
* @returns A boolean indicating whether the secret is valid.
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* if (!cryptoFactory.verifySecret(config.storageToken)) {
|
|
98
|
+
* throw new Error('invalid_secret');
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
verifySecret(secret: string): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Generate self-verifiable device ID.
|
|
105
|
+
* @returns The generated device ID.
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const deviceId = deviceFactory.generateDeviceId();
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
generateDeviceId(): string;
|
|
112
|
+
/**
|
|
113
|
+
* Verify a device ID.
|
|
114
|
+
* @param deviceId The device ID to verify.
|
|
115
|
+
* @returns A boolean indicating whether the device ID is valid.
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* if (!deviceFactory.verifyDeviceId(bodyJson.deviceId)) {
|
|
119
|
+
* throw {
|
|
120
|
+
* ok: false,
|
|
121
|
+
* status: 400,
|
|
122
|
+
* error: 'invalid_device_id',
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
verifyDeviceId(deviceId: string): boolean;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=api.d.ts.map
|
package/api.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAO9C,OAAO,EAAC,oBAAoB,EAAE,KAAK,aAAa,EAAC,MAAM,YAAY,CAAC;AAEpE;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,QAAQ,EAAE,cAAc,GAAG,UAAU,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,mBAAmB;IAC9B,SAAS,CAAC,WAAW;;;;;MAAC;IAEtB;;;OAGG;gBACS,MAAM,EAAE,mBAAmB;IAYvC;;;;;;;;;;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,CAAC,EAAE,GAAG,MAAM;IAIxD;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,YAAY,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,aAAa;IAI5E;;;;;;;;;;OAUG;IACH,cAAc,IAAI,MAAM;IAIxB;;;;;;;;;;OAUG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAIrC;;;;;;;OAOG;IACH,gBAAgB,IAAI,MAAM;IAI1B;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;CAG1C"}
|
package/api.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { AlwatrHashGenerator } from './hash.js';
|
|
2
|
+
import { deviceIdGeneratorRecommendedConfig, secretGeneratorRecommendedConfig, userIdGeneratorRecommendedConfig, userTokenGeneratorRecommendedConfig, } from './pre-config.js';
|
|
3
|
+
import { AlwatrTokenGenerator } from './token.js';
|
|
4
|
+
/**
|
|
5
|
+
* Crypto factory for generating self-validate user-id, user-token, secret, device-id.
|
|
6
|
+
*/
|
|
7
|
+
export class AlwatrCryptoFactory {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new instance of crypto factory.
|
|
10
|
+
* @param config The configuration used to create the crypto factory.
|
|
11
|
+
*/
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this._generators = {
|
|
14
|
+
secret: new AlwatrHashGenerator(secretGeneratorRecommendedConfig),
|
|
15
|
+
deviceId: new AlwatrHashGenerator(deviceIdGeneratorRecommendedConfig),
|
|
16
|
+
userId: new AlwatrHashGenerator(userIdGeneratorRecommendedConfig),
|
|
17
|
+
token: new AlwatrTokenGenerator({
|
|
18
|
+
...userTokenGeneratorRecommendedConfig,
|
|
19
|
+
...config
|
|
20
|
+
}),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generate self-verifiable user ID.
|
|
25
|
+
* @returns The generated user ID.
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const newUser = {
|
|
29
|
+
* id: cryptoFactory.generateUserId(),
|
|
30
|
+
* ...
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
generateUserId() {
|
|
35
|
+
return this._generators.userId.generateRandomSelfValidate();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Verify a user ID without token.
|
|
39
|
+
* @param userId The user ID to verify.
|
|
40
|
+
* @returns A boolean indicating whether the user ID is valid.
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* if (!cryptoFactory.verifyUserId(user.id)) {
|
|
44
|
+
* throw new Error('invalid_user');
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
verifyUserId(userId) {
|
|
49
|
+
return this._generators.userId.verifySelfValidate(userId);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Generate authentication token.
|
|
53
|
+
* @param uniquelyList The list of uniq values to generate the token from.
|
|
54
|
+
* @returns The generated user token.
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const userToken = cryptoFactory.generateToken([user.id, user.lpe]);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
generateToken(uniquelyList) {
|
|
61
|
+
return this._generators.token.generate(uniquelyList.join());
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Verify a authentication token.
|
|
65
|
+
* @param uniquelyList The list of uniq values used to generate the token.
|
|
66
|
+
* @param token The user token to verify.
|
|
67
|
+
* @returns The validity of the token.
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* if (!cryptoFactory.verifyToken([user.id, user.lpe], userToken)) {
|
|
71
|
+
* throw new Error('invalid_token');
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
verifyToken(uniquelyList, token) {
|
|
76
|
+
return this._generators.token.verify(uniquelyList.join(), token);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Generate self-verifiable secret.
|
|
80
|
+
* @returns The generated secret.
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const config = {
|
|
84
|
+
* storageToken: cryptoFactory.generateSecret(),
|
|
85
|
+
* ...
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
generateSecret() {
|
|
90
|
+
return this._generators.secret.generateRandomSelfValidate();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Verify a secret.
|
|
94
|
+
* @param secret The secret to verify.
|
|
95
|
+
* @returns A boolean indicating whether the secret is valid.
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* if (!cryptoFactory.verifySecret(config.storageToken)) {
|
|
99
|
+
* throw new Error('invalid_secret');
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
verifySecret(secret) {
|
|
104
|
+
return this._generators.secret.verifySelfValidate(secret);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Generate self-verifiable device ID.
|
|
108
|
+
* @returns The generated device ID.
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const deviceId = deviceFactory.generateDeviceId();
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
generateDeviceId() {
|
|
115
|
+
return this._generators.deviceId.generateRandomSelfValidate();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Verify a device ID.
|
|
119
|
+
* @param deviceId The device ID to verify.
|
|
120
|
+
* @returns A boolean indicating whether the device ID is valid.
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* if (!deviceFactory.verifyDeviceId(bodyJson.deviceId)) {
|
|
124
|
+
* throw {
|
|
125
|
+
* ok: false,
|
|
126
|
+
* status: 400,
|
|
127
|
+
* error: 'invalid_device_id',
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
verifyDeviceId(deviceId) {
|
|
133
|
+
return this._generators.deviceId.verifySelfValidate(deviceId);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=api.js.map
|
package/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["src/api.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EACL,kCAAkC,EAClC,gCAAgC,EAChC,gCAAgC,EAChC,mCAAmC,GACpC,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAC,oBAAoB,EAAqB,MAAM,YAAY,CAAC;AAiBpE;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAG9B;;;OAGG;IACH,YAAY,MAA2B;QACrC,IAAI,CAAC,WAAW,GAAG;YACjB,MAAM,EAAE,IAAI,mBAAmB,CAAC,gCAAgC,CAAC;YACjE,QAAQ,EAAE,IAAI,mBAAmB,CAAC,kCAAkC,CAAC;YACrE,MAAM,EAAE,IAAI,mBAAmB,CAAC,gCAAgC,CAAC;YACjE,KAAK,EAAE,IAAI,oBAAoB,CAAC;gBAC9B,GAAG,mCAAmC;gBACtC,GAAG,MAAM;aACV,CAAC;SACM,CAAC;IACb,CAAC;IAED;;;;;;;;;;OAUG;IACH,cAAc;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,0BAA0B,EAAE,CAAC;IAC9D,CAAC;IAED;;;;;;;;;;OAUG;IACH,YAAY,CAAC,MAAc;QACzB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;;OAQG;IACH,aAAa,CAAC,YAAiC;QAC7C,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9D,CAAC;IAED;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,YAAiC,EAAE,KAAa;QAC1D,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;;;OAUG;IACH,cAAc;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,0BAA0B,EAAE,CAAC;IAC9D,CAAC;IAED;;;;;;;;;;OAUG;IACH,YAAY,CAAC,MAAc;QACzB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;OAOG;IACH,gBAAgB;QACd,OAAO,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,0BAA0B,EAAE,CAAC;IAChE,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,QAAgB;QAC7B,OAAO,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC;IAChE,CAAC;CACF","sourcesContent":["import {DurationString} from '@alwatr/math';\n\nimport {AlwatrHashGenerator} from './hash.js';\nimport {\n deviceIdGeneratorRecommendedConfig,\n secretGeneratorRecommendedConfig,\n userIdGeneratorRecommendedConfig,\n userTokenGeneratorRecommendedConfig,\n} from './pre-config.js';\nimport {AlwatrTokenGenerator, type TokenValidity} from './token.js';\n\n/**\n * Configuration options for the CryptoFactory.\n */\nexport interface CryptoFactoryConfig {\n /**\n * The secret used for encryption and decryption tokens.\n */\n secret: string;\n\n /**\n * The duration for which the token is valid.\n */\n duration: DurationString | 'infinite';\n}\n\n/**\n * Crypto factory for generating self-validate user-id, user-token, secret, device-id.\n */\nexport class AlwatrCryptoFactory {\n protected _generators;\n\n /**\n * Creates a new instance of crypto factory.\n * @param config The configuration used to create the crypto factory.\n */\n constructor(config: CryptoFactoryConfig) {\n this._generators = {\n secret: new AlwatrHashGenerator(secretGeneratorRecommendedConfig),\n deviceId: new AlwatrHashGenerator(deviceIdGeneratorRecommendedConfig),\n userId: new AlwatrHashGenerator(userIdGeneratorRecommendedConfig),\n token: new AlwatrTokenGenerator({\n ...userTokenGeneratorRecommendedConfig,\n ...config\n }),\n } as const;\n }\n\n /**\n * Generate self-verifiable user ID.\n * @returns The generated user ID.\n * @example\n * ```typescript\n * const newUser = {\n * id: cryptoFactory.generateUserId(),\n * ...\n * }\n * ```\n */\n generateUserId(): string {\n return this._generators.userId.generateRandomSelfValidate();\n }\n\n /**\n * Verify 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 (!cryptoFactory.verifyUserId(user.id)) {\n * throw new Error('invalid_user');\n * }\n * ```\n */\n verifyUserId(userId: string): boolean {\n return this._generators.userId.verifySelfValidate(userId);\n }\n\n /**\n * Generate authentication token.\n * @param uniquelyList The list of uniq values to generate the token from.\n * @returns The generated user token.\n * @example\n * ```typescript\n * const userToken = cryptoFactory.generateToken([user.id, user.lpe]);\n * ```\n */\n generateToken(uniquelyList: (string | number)[]): string {\n return this._generators.token.generate(uniquelyList.join());\n }\n\n /**\n * Verify a authentication token.\n * @param uniquelyList The list of uniq values used to generate the token.\n * @param token The user token to verify.\n * @returns The validity of the token.\n * @example\n * ```typescript\n * if (!cryptoFactory.verifyToken([user.id, user.lpe], userToken)) {\n * throw new Error('invalid_token');\n * }\n * ```\n */\n verifyToken(uniquelyList: (string | number)[], token: string): TokenValidity {\n return this._generators.token.verify(uniquelyList.join(), token);\n }\n\n /**\n * Generate self-verifiable secret.\n * @returns The generated secret.\n * @example\n * ```typescript\n * const config = {\n * storageToken: cryptoFactory.generateSecret(),\n * ...\n * }\n * ```\n */\n generateSecret(): string {\n return this._generators.secret.generateRandomSelfValidate();\n }\n\n /**\n * Verify a secret.\n * @param secret The secret to verify.\n * @returns A boolean indicating whether the secret is valid.\n * @example\n * ```typescript\n * if (!cryptoFactory.verifySecret(config.storageToken)) {\n * throw new Error('invalid_secret');\n * }\n * ```\n */\n verifySecret(secret: string): boolean {\n return this._generators.secret.verifySelfValidate(secret);\n }\n\n /**\n * Generate self-verifiable device ID.\n * @returns The generated device ID.\n * @example\n * ```typescript\n * const deviceId = deviceFactory.generateDeviceId();\n * ```\n */\n generateDeviceId(): string {\n return this._generators.deviceId.generateRandomSelfValidate();\n }\n\n /**\n * Verify a device ID.\n * @param deviceId The device ID to verify.\n * @returns A boolean indicating whether the device ID is valid.\n * @example\n * ```typescript\n * if (!deviceFactory.verifyDeviceId(bodyJson.deviceId)) {\n * throw {\n * ok: false,\n * status: 400,\n * error: 'invalid_device_id',\n * }\n * }\n * ```\n */\n verifyDeviceId(deviceId: string): boolean {\n return this._generators.deviceId.verifySelfValidate(deviceId);\n }\n}\n"]}
|
package/hash.d.ts
CHANGED
|
@@ -1,76 +1,102 @@
|
|
|
1
|
-
/// <reference types="node"
|
|
1
|
+
/// <reference types="node" />
|
|
2
2
|
import { type BinaryLike } from 'node:crypto';
|
|
3
|
-
import type {
|
|
3
|
+
import type { CryptoAlgorithm, CryptoEncoding } from './type.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents the configuration for a hash generator.
|
|
6
|
+
*/
|
|
7
|
+
export interface HashGeneratorConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The prefix to be added to the generated hash.
|
|
10
|
+
*/
|
|
11
|
+
prefix: string;
|
|
12
|
+
/**
|
|
13
|
+
* The algorithm used for hashing.
|
|
14
|
+
*/
|
|
15
|
+
algorithm: CryptoAlgorithm;
|
|
16
|
+
/**
|
|
17
|
+
* The encoding used for the generated hash.
|
|
18
|
+
*/
|
|
19
|
+
encoding: CryptoEncoding;
|
|
20
|
+
/**
|
|
21
|
+
* The length of the CRC (Cyclic Redundancy Check) value.
|
|
22
|
+
*/
|
|
23
|
+
crcLength: number;
|
|
24
|
+
}
|
|
4
25
|
/**
|
|
5
26
|
* Secure **self-validate** hash generator.
|
|
6
27
|
*/
|
|
7
28
|
export declare class AlwatrHashGenerator {
|
|
8
29
|
config: HashGeneratorConfig;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new instance of the AlwatrHashGenerator class.
|
|
32
|
+
* @param config The configuration for the hash generator.
|
|
33
|
+
*/
|
|
9
34
|
constructor(config: HashGeneratorConfig);
|
|
10
35
|
/**
|
|
11
|
-
* Generate
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* const clientId = hashGenerator.random();
|
|
36
|
+
* Generate a random hash.
|
|
37
|
+
* @returns The generated hash.
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const clientId = hashGenerator.generateRandom();
|
|
17
41
|
* ```
|
|
18
42
|
*/
|
|
19
|
-
|
|
43
|
+
generateRandom(): string;
|
|
20
44
|
/**
|
|
21
|
-
* Generate **self-validate**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* const userId = hashGenerator.randomSelfValidate();
|
|
45
|
+
* Generate a **self-validate** random hash.
|
|
46
|
+
* @returns The generated self-validated hash.
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const userId = hashGenerator.generateRandomSelfValidate();
|
|
27
50
|
* ```
|
|
28
51
|
*/
|
|
29
|
-
|
|
52
|
+
generateRandomSelfValidate(): string;
|
|
30
53
|
/**
|
|
31
|
-
* Generate
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* ```
|
|
36
|
-
* const crcHash = hashGenerator.generate(
|
|
54
|
+
* Generate a hash from data.
|
|
55
|
+
* @param data - The data to generate the hash from.
|
|
56
|
+
* @returns The generated hash.
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const crcHash = hashGenerator.generate(data);
|
|
37
60
|
* ```
|
|
38
61
|
*/
|
|
39
62
|
generate(data: BinaryLike): string;
|
|
40
63
|
/**
|
|
41
|
-
* Generate crc hash.
|
|
64
|
+
* Generate a crc hash.
|
|
65
|
+
* @param data - The data to generate the crc hash from.
|
|
66
|
+
* @returns The generated crc hash.
|
|
42
67
|
*/
|
|
43
|
-
|
|
68
|
+
generateCrc(data: BinaryLike): string;
|
|
44
69
|
/**
|
|
45
|
-
* Generate **self-validate** hash from data.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* ```
|
|
50
|
-
* const userId = hashGenerator.generateSelfValidate(
|
|
70
|
+
* Generate a **self-validate** hash from data.
|
|
71
|
+
* @param data - The data to generate the self-validated hash from.
|
|
72
|
+
* @returns The generated self-validated hash.
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const userId = hashGenerator.generateSelfValidate(data);
|
|
51
76
|
* ```
|
|
52
77
|
*/
|
|
53
78
|
generateSelfValidate(data: BinaryLike): string;
|
|
54
79
|
/**
|
|
55
|
-
* Verify
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
80
|
+
* Verify if the generated hash matches the provided hash.
|
|
81
|
+
* @param data - The data to verify.
|
|
82
|
+
* @param hash - The hash to compare against.
|
|
83
|
+
* @returns `true` if the hash is verified, `false` otherwise.
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* if (!hashGenerator.verify(data, hash)) {
|
|
61
87
|
* new Error('data_corrupted');
|
|
62
88
|
* }
|
|
63
89
|
* ```
|
|
64
90
|
*/
|
|
65
91
|
verify(data: BinaryLike, hash: string): boolean;
|
|
66
92
|
/**
|
|
67
|
-
* Verify a **self-validate** hash to check
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* ```
|
|
72
|
-
* if (!hashGenerator.verifySelfValidate(
|
|
73
|
-
* new Error('
|
|
93
|
+
* Verify a **self-validate** hash to check if it was generated by this class (with the same options).
|
|
94
|
+
* @param hash - The self-validated hash to verify.
|
|
95
|
+
* @returns `true` if the hash is verified, `false` otherwise.
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* if (!hashGenerator.verifySelfValidate(hash)) {
|
|
99
|
+
* new Error('invalid_hash');
|
|
74
100
|
* }
|
|
75
101
|
* ```
|
|
76
102
|
*/
|
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,
|
|
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,eAAe,EAAE,cAAc,EAAC,MAAM,WAAW,CAAC;AAE/D;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;IAEzB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;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;CAM1C"}
|
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,21 @@ 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(
|
|
87
|
-
* new Error('
|
|
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)) {
|
|
92
|
+
* new Error('invalid_hash');
|
|
88
93
|
* }
|
|
89
94
|
* ```
|
|
90
95
|
*/
|
|
91
96
|
verifySelfValidate(hash) {
|
|
92
|
-
const gapPos =
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const crcHash = hash.substring(gapPos);
|
|
97
|
-
return crcHash === this._generateCrc(mainHash);
|
|
97
|
+
const gapPos = hash.length - this.config.crcLength;
|
|
98
|
+
const mainHash = hash.slice(0, gapPos);
|
|
99
|
+
const crcHash = hash.slice(gapPos);
|
|
100
|
+
return crcHash === this.generateCrc(mainHash);
|
|
98
101
|
}
|
|
99
102
|
}
|
|
100
103
|
//# sourceMappingURL=hash.js.map
|
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;
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,WAAW,EAAkB,MAAM,aAAa,CAAC;AA6BrE;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAC9B;;;OAGG;IACH,YAAmB,MAA2B;QAA3B,WAAM,GAAN,MAAM,CAAqB;IAAG,CAAC;IAElD;;;;;;;OAOG;IACH,cAAc;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;OAOG;IACH,0BAA0B;QACxB,OAAO,IAAI,CAAC,oBAAoB,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAgB;QACvB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC1G,CAAC;IAED;;;;OAIG;IACH,WAAW,CAAC,IAAgB;QAC1B,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,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAChH,CAAC;IAED;;;;;;;;OAQG;IACH,oBAAoB,CAAC,IAAgB;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,GAAG,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;QACnD,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 {CryptoAlgorithm, CryptoEncoding} from './type.js';\n\n/**\n * Represents the configuration for a hash generator.\n */\nexport interface HashGeneratorConfig {\n /**\n * The prefix to be added to the generated hash.\n */\n prefix: string;\n\n /**\n * The algorithm used for hashing.\n */\n algorithm: CryptoAlgorithm;\n\n /**\n * The encoding used for the generated hash.\n */\n encoding: CryptoEncoding;\n\n /**\n * The length of the CRC (Cyclic Redundancy Check) value.\n */\n crcLength: number;\n}\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_hash');\n * }\n * ```\n */\n verifySelfValidate(hash: string): boolean {\n const gapPos = 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/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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { definePackage } from '@alwatr/logger';
|
|
2
2
|
export * from './hash.js';
|
|
3
3
|
export * from './token.js';
|
|
4
|
-
export * from './
|
|
4
|
+
export * from './api.js';
|
|
5
5
|
export * from './type.js';
|
|
6
6
|
export * from './pre-config.js';
|
|
7
|
-
definePackage('crypto', '
|
|
7
|
+
definePackage('crypto', '3.x');
|
|
8
8
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAE7C,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAE7C,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAEhC,aAAa,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC","sourcesContent":["import {definePackage} from '@alwatr/logger';\n\nexport * from './hash.js';\nexport * from './token.js';\nexport * from './api.js';\nexport * from './type.js';\nexport * from './pre-config.js';\n\ndefinePackage('crypto', '3.x');\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/crypto",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.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",
|
|
@@ -32,21 +32,20 @@
|
|
|
32
32
|
},
|
|
33
33
|
"repository": {
|
|
34
34
|
"type": "git",
|
|
35
|
-
"url": "https://github.com/Alwatr/
|
|
35
|
+
"url": "https://github.com/Alwatr/alwatr-es-sdk",
|
|
36
36
|
"directory": "packages/crypto"
|
|
37
37
|
},
|
|
38
|
-
"homepage": "https://github.com/Alwatr/
|
|
38
|
+
"homepage": "https://github.com/Alwatr/alwatr-es-sdk/tree/next/packages/crypto#readme",
|
|
39
39
|
"bugs": {
|
|
40
|
-
"url": "https://github.com/Alwatr/
|
|
40
|
+
"url": "https://github.com/Alwatr/alwatr-es-sdk/issues"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@alwatr/logger": "^2.3.
|
|
44
|
-
"@alwatr/math": "^1.2.
|
|
45
|
-
"@alwatr/util": "^1.3.
|
|
46
|
-
"tslib": "^2.6.2"
|
|
43
|
+
"@alwatr/logger": "^2.3.2",
|
|
44
|
+
"@alwatr/math": "^1.2.3",
|
|
45
|
+
"@alwatr/util": "^1.3.3"
|
|
47
46
|
},
|
|
48
47
|
"devDependencies": {
|
|
49
|
-
"@types/node": "^20.
|
|
48
|
+
"@types/node": "^20.10.4"
|
|
50
49
|
},
|
|
51
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "932c439c39ad9aa340cf53e0704cece565d68326"
|
|
52
51
|
}
|
package/pre-config.d.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import { HashGeneratorConfig
|
|
1
|
+
import type { HashGeneratorConfig } from './hash.js';
|
|
2
|
+
import type { TokenGeneratorConfig } from './token.js';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* Alwatr hash generator recommended configuration for making random self-validate **user-id**.
|
|
5
|
+
*/
|
|
6
|
+
export declare const userIdGeneratorRecommendedConfig: HashGeneratorConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Hash generator recommended configuration for making random self-validate **device-id**.
|
|
4
9
|
*/
|
|
5
|
-
export declare const
|
|
10
|
+
export declare const deviceIdGeneratorRecommendedConfig: HashGeneratorConfig;
|
|
6
11
|
/**
|
|
7
|
-
* Hash generator pre configuration for making random self-validate **
|
|
12
|
+
* Hash generator pre configuration for making random self-validate **secrets**.
|
|
8
13
|
*/
|
|
9
|
-
export declare const
|
|
14
|
+
export declare const secretGeneratorRecommendedConfig: HashGeneratorConfig;
|
|
10
15
|
/**
|
|
11
|
-
* Token generator
|
|
16
|
+
* Token generator recommended configuration for making secure self-validate **user-token**.
|
|
12
17
|
*/
|
|
13
|
-
export declare const
|
|
18
|
+
export declare const userTokenGeneratorRecommendedConfig: Omit<TokenGeneratorConfig, 'secret' | 'duration'>;
|
|
14
19
|
//# 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,
|
|
1
|
+
{"version":3,"file":"pre-config.d.ts","sourceRoot":"","sources":["src/pre-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AACnD,OAAO,KAAK,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAErD;;GAEG;AACH,eAAO,MAAM,gCAAgC,EAAE,mBAK9C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kCAAkC,EAAE,mBAGhD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gCAAgC,EAAE,mBAK9C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mCAAmC,EAAE,IAAI,CAAC,oBAAoB,EAAE,QAAQ,GAAG,UAAU,CAIjG,CAAC"}
|
package/pre-config.js
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Alwatr hash generator recommended configuration for making random self-validate **user-id**.
|
|
3
3
|
*/
|
|
4
|
-
export const
|
|
5
|
-
|
|
4
|
+
export const userIdGeneratorRecommendedConfig = {
|
|
5
|
+
prefix: 'u',
|
|
6
|
+
algorithm: 'sha1',
|
|
6
7
|
encoding: 'base64url',
|
|
7
8
|
crcLength: 4,
|
|
8
9
|
};
|
|
9
10
|
/**
|
|
10
|
-
* Hash generator
|
|
11
|
+
* Hash generator recommended configuration for making random self-validate **device-id**.
|
|
11
12
|
*/
|
|
12
|
-
export const
|
|
13
|
-
|
|
13
|
+
export const deviceIdGeneratorRecommendedConfig = {
|
|
14
|
+
...userIdGeneratorRecommendedConfig,
|
|
15
|
+
prefix: 'd',
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Hash generator pre configuration for making random self-validate **secrets**.
|
|
19
|
+
*/
|
|
20
|
+
export const secretGeneratorRecommendedConfig = {
|
|
21
|
+
prefix: 's',
|
|
22
|
+
algorithm: 'sha384',
|
|
14
23
|
encoding: 'base64url',
|
|
15
24
|
crcLength: 4,
|
|
16
25
|
};
|
|
17
26
|
/**
|
|
18
|
-
* Token generator
|
|
27
|
+
* Token generator recommended configuration for making secure self-validate **user-token**.
|
|
19
28
|
*/
|
|
20
|
-
export const
|
|
29
|
+
export const userTokenGeneratorRecommendedConfig = {
|
|
30
|
+
prefix: 't',
|
|
21
31
|
algorithm: 'sha224',
|
|
22
32
|
encoding: 'base64url',
|
|
23
33
|
};
|
package/pre-config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pre-config.js","sourceRoot":"","sources":["src/pre-config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pre-config.js","sourceRoot":"","sources":["src/pre-config.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAwB;IACnE,MAAM,EAAE,GAAG;IACX,SAAS,EAAE,MAAM;IACjB,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,kCAAkC,GAAwB;IACrE,GAAG,gCAAgC;IACnC,MAAM,EAAE,GAAG;CACZ,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAwB;IACnE,MAAM,EAAE,GAAG;IACX,SAAS,EAAE,QAAQ;IACnB,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAsD;IACpG,MAAM,EAAE,GAAG;IACX,SAAS,EAAE,QAAQ;IACnB,QAAQ,EAAE,WAAW;CACtB,CAAC","sourcesContent":["import type {HashGeneratorConfig} from './hash.js';\nimport type {TokenGeneratorConfig} from './token.js';\n\n/**\n * Alwatr hash generator recommended configuration for making random self-validate **user-id**.\n */\nexport const userIdGeneratorRecommendedConfig: HashGeneratorConfig = {\n prefix: 'u',\n algorithm: 'sha1',\n encoding: 'base64url',\n crcLength: 4,\n};\n\n/**\n * Hash generator recommended configuration for making random self-validate **device-id**.\n */\nexport const deviceIdGeneratorRecommendedConfig: HashGeneratorConfig = {\n ...userIdGeneratorRecommendedConfig,\n prefix: 'd',\n};\n\n/**\n * Hash generator pre configuration for making random self-validate **secrets**.\n */\nexport const secretGeneratorRecommendedConfig: HashGeneratorConfig = {\n prefix: 's',\n algorithm: 'sha384',\n encoding: 'base64url',\n crcLength: 4,\n};\n\n/**\n * Token generator recommended configuration for making secure self-validate **user-token**.\n */\nexport const userTokenGeneratorRecommendedConfig: Omit<TokenGeneratorConfig, 'secret' | 'duration'> = {\n prefix: 't',\n algorithm: 'sha224',\n encoding: 'base64url',\n};\n"]}
|
package/token.d.ts
CHANGED
|
@@ -1,28 +1,73 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { DurationString } from '@alwatr/math';
|
|
2
|
+
import type { CryptoAlgorithm, CryptoEncoding } from './type.js';
|
|
3
|
+
export type TokenValidity = 'valid' | 'invalid' | 'expired';
|
|
4
|
+
/**
|
|
5
|
+
* Represents the configuration for a token generator.
|
|
6
|
+
*/
|
|
7
|
+
export interface TokenGeneratorConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The prefix to be added to the generated hash.
|
|
10
|
+
*/
|
|
11
|
+
prefix: string;
|
|
12
|
+
/**
|
|
13
|
+
* The algorithm used for hashing.
|
|
14
|
+
*/
|
|
15
|
+
algorithm: CryptoAlgorithm;
|
|
16
|
+
/**
|
|
17
|
+
* The encoding used for the generated hash.
|
|
18
|
+
*/
|
|
19
|
+
encoding: CryptoEncoding;
|
|
20
|
+
/**
|
|
21
|
+
* The secret used for encryption and decryption tokens.
|
|
22
|
+
*/
|
|
23
|
+
secret: string;
|
|
24
|
+
/**
|
|
25
|
+
* The duration for which the token is valid.
|
|
26
|
+
*/
|
|
27
|
+
duration: DurationString | 'infinite';
|
|
28
|
+
}
|
|
2
29
|
/**
|
|
3
30
|
* Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).
|
|
4
31
|
*/
|
|
5
32
|
export declare class AlwatrTokenGenerator {
|
|
6
33
|
config: TokenGeneratorConfig;
|
|
7
|
-
|
|
8
|
-
|
|
34
|
+
private _duration;
|
|
35
|
+
/**
|
|
36
|
+
* The current epoch based on the configured duration.
|
|
37
|
+
*/
|
|
38
|
+
protected get _epoch(): number;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new instance of AlwatrTokenGenerator.
|
|
41
|
+
* @param config The configuration for the token generator.
|
|
42
|
+
*/
|
|
9
43
|
constructor(config: TokenGeneratorConfig);
|
|
10
|
-
protected _generate(data: string, epoch: number): string;
|
|
11
44
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
45
|
+
* Generates a HOTP token based on the provided data for special duration.
|
|
46
|
+
* @param data The data to generate the token from.
|
|
47
|
+
* @returns The generated token.
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
15
50
|
* user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);
|
|
16
51
|
* ```
|
|
17
52
|
*/
|
|
18
53
|
generate(data: string): string;
|
|
19
54
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
55
|
+
* Verifies if a token is valid.
|
|
56
|
+
* @param data The data used to generate the token.
|
|
57
|
+
* @param token The token to verify.
|
|
58
|
+
* @returns The validity of the token.
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const validateStatus = tokenGenerator.verify([user.id,user.role].join(), user.auth);
|
|
24
62
|
* ```
|
|
25
63
|
*/
|
|
26
|
-
verify(data: string, token: string):
|
|
64
|
+
verify(data: string, token: string): TokenValidity;
|
|
65
|
+
/**
|
|
66
|
+
* Generates a cryptographic token based on the provided data and epoch.
|
|
67
|
+
* @param data - The data to be used in the token generation.
|
|
68
|
+
* @param epoch - The epoch value to be used in the token generation.
|
|
69
|
+
* @returns The generated cryptographic token.
|
|
70
|
+
*/
|
|
71
|
+
protected _generate(data: string, epoch: number): string;
|
|
27
72
|
}
|
|
28
73
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["src/token.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,cAAc,EAAgB,MAAM,cAAc,CAAC;AAE3D,OAAO,KAAK,EAAC,eAAe,EAAE,cAAc,EAAC,MAAM,WAAW,CAAC;AAE/D,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;IAEzB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,QAAQ,EAAE,cAAc,GAAG,UAAU,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,oBAAoB;IAcZ,MAAM,EAAE,oBAAoB;IAb/C,OAAO,CAAC,SAAS,CAAS;IAE1B;;OAEG;IACH,SAAS,KAAK,MAAM,IAAI,MAAM,CAE7B;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,aAAa;IAQlD;;;;;OAKG;IACH,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAQzD"}
|
package/token.js
CHANGED
|
@@ -4,49 +4,63 @@ 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
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The current epoch based on the configured duration.
|
|
9
|
+
*/
|
|
10
|
+
get _epoch() {
|
|
11
|
+
return this._duration == 0 ? 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
|
-
this._duration = config.duration ==
|
|
13
|
-
}
|
|
14
|
-
_generate(data, epoch) {
|
|
15
|
-
return createHmac(this.config.algorithm, data)
|
|
16
|
-
.update(data + epoch)
|
|
17
|
-
.digest(this.config.encoding);
|
|
19
|
+
this._duration = config.duration == 'infinite' ? 0 : parseDuration(config.duration);
|
|
18
20
|
}
|
|
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
|
*/
|
|
26
30
|
generate(data) {
|
|
27
|
-
return this._generate(data, this.
|
|
31
|
+
return this._generate(data, this._epoch);
|
|
28
32
|
}
|
|
29
33
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
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 validity of the token.
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const validateStatus = tokenGenerator.verify([user.id,user.role].join(), user.auth);
|
|
34
41
|
* ```
|
|
35
42
|
*/
|
|
36
43
|
verify(data, token) {
|
|
37
|
-
const epoch = this.
|
|
38
|
-
if (token === this._generate(data, epoch))
|
|
44
|
+
const epoch = this._epoch;
|
|
45
|
+
if (token === this._generate(data, epoch))
|
|
39
46
|
return 'valid';
|
|
40
|
-
|
|
41
|
-
else if (this._duration == null) {
|
|
47
|
+
if (this._duration == 0)
|
|
42
48
|
return 'invalid';
|
|
43
|
-
|
|
44
|
-
else if (token === this._generate(data, epoch - 1)) {
|
|
49
|
+
if (token === this._generate(data, epoch - 1))
|
|
45
50
|
return 'expired';
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
return 'invalid';
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Generates a cryptographic token based on the provided data and epoch.
|
|
55
|
+
* @param data - The data to be used in the token generation.
|
|
56
|
+
* @param epoch - The epoch value to be used in the token generation.
|
|
57
|
+
* @returns The generated cryptographic token.
|
|
58
|
+
*/
|
|
59
|
+
_generate(data, epoch) {
|
|
60
|
+
return (this.config.prefix +
|
|
61
|
+
createHmac(this.config.algorithm, data)
|
|
62
|
+
.update(data + epoch)
|
|
63
|
+
.digest(this.config.encoding));
|
|
50
64
|
}
|
|
51
65
|
}
|
|
52
66
|
//# 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,
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["src/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAiB,aAAa,EAAC,MAAM,cAAc,CAAC;AAoC3D;;GAEG;AACH,MAAM,OAAO,oBAAoB;IAG/B;;OAEG;IACH,IAAc,MAAM;QAClB,OAAO,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3E,CAAC;IAED;;;OAGG;IACH,YAAmB,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;QAC7C,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,QAAQ,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtF,CAAC;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAY;QACnB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,IAAY,EAAE,KAAa;QAChC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,KAAK,KAAK,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC;YAAE,OAAO,OAAO,CAAC;QAC1D,IAAI,IAAI,CAAC,SAAS,IAAI,CAAC;YAAE,OAAO,SAAS,CAAC;QAC1C,IAAI,KAAK,KAAK,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC;YAAE,OAAO,SAAS,CAAC;QAChE,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;;OAKG;IACO,SAAS,CAAC,IAAY,EAAE,KAAa;QAC7C,OAAO,CACL,IAAI,CAAC,MAAM,CAAC,MAAM;YAClB,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC;iBACpC,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC;iBACpB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAChC,CAAC;IACJ,CAAC;CACF","sourcesContent":["import {createHmac} from 'node:crypto';\n\nimport {DurationString, parseDuration} from '@alwatr/math';\n\nimport type {CryptoAlgorithm, CryptoEncoding} from './type.js';\n\nexport type TokenValidity = 'valid' | 'invalid' | 'expired';\n\n/**\n * Represents the configuration for a token generator.\n */\nexport interface TokenGeneratorConfig {\n /**\n * The prefix to be added to the generated hash.\n */\n prefix: string;\n\n /**\n * The algorithm used for hashing.\n */\n algorithm: CryptoAlgorithm;\n\n /**\n * The encoding used for the generated hash.\n */\n encoding: CryptoEncoding;\n\n /**\n * The secret used for encryption and decryption tokens.\n */\n secret: string;\n\n /**\n * The duration for which the token is valid.\n */\n duration: DurationString | 'infinite';\n}\n\n/**\n * Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).\n */\nexport class AlwatrTokenGenerator {\n private _duration: number;\n\n /**\n * The current epoch based on the configured duration.\n */\n protected get _epoch(): number {\n return this._duration == 0 ? 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 == 'infinite' ? 0 : 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 validity of the token.\n * @example\n * ```typescript\n * const validateStatus = tokenGenerator.verify([user.id,user.role].join(), user.auth);\n * ```\n */\n verify(data: string, token: string): TokenValidity {\n const epoch = this._epoch;\n if (token === this._generate(data, epoch)) return 'valid';\n if (this._duration == 0) return 'invalid';\n if (token === this._generate(data, epoch - 1)) return 'expired';\n return 'invalid';\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 (\n this.config.prefix +\n createHmac(this.config.algorithm, data)\n .update(data + epoch)\n .digest(this.config.encoding)\n );\n }\n}\n"]}
|
package/type.d.ts
CHANGED
|
@@ -1,44 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Represents a cryptographic algorithm.
|
|
3
|
+
* Supported algorithms include: 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'.
|
|
4
|
+
*/
|
|
2
5
|
export type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';
|
|
6
|
+
/**
|
|
7
|
+
* Represents the encoding options for cryptographic operations.
|
|
8
|
+
* The encoding can be one of the following: 'base64', 'base64url', 'hex', or 'binary'.
|
|
9
|
+
*/
|
|
3
10
|
export type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary';
|
|
4
|
-
export type TokenStatus = 'valid' | 'invalid' | 'expired';
|
|
5
|
-
export type HashStatus = 'valid' | 'invalid';
|
|
6
|
-
export interface TokenGeneratorConfig {
|
|
7
|
-
/**
|
|
8
|
-
* Secret string data to generate token.
|
|
9
|
-
*/
|
|
10
|
-
secret: string;
|
|
11
|
-
/**
|
|
12
|
-
* Token expiration time.
|
|
13
|
-
*
|
|
14
|
-
* `null` mean without expiration time
|
|
15
|
-
*/
|
|
16
|
-
duration: DurationString | null;
|
|
17
|
-
/**
|
|
18
|
-
* OpenSSl digest algorithm.
|
|
19
|
-
*/
|
|
20
|
-
algorithm: CryptoAlgorithm;
|
|
21
|
-
/**
|
|
22
|
-
* Encoding of token.
|
|
23
|
-
*/
|
|
24
|
-
encoding: CryptoEncoding;
|
|
25
|
-
}
|
|
26
|
-
export interface HashGeneratorConfig {
|
|
27
|
-
/**
|
|
28
|
-
* OpenSSl digest algorithm.
|
|
29
|
-
*/
|
|
30
|
-
algorithm: CryptoAlgorithm;
|
|
31
|
-
/**
|
|
32
|
-
* Encoding of hash.
|
|
33
|
-
*/
|
|
34
|
-
encoding: CryptoEncoding;
|
|
35
|
-
/**
|
|
36
|
-
* CRC hash max length.
|
|
37
|
-
*/
|
|
38
|
-
crcLength?: number;
|
|
39
|
-
}
|
|
40
|
-
export interface UserFactoryConfig {
|
|
41
|
-
tokenConfig: TokenGeneratorConfig;
|
|
42
|
-
hashConfig: HashGeneratorConfig;
|
|
43
|
-
}
|
|
44
11
|
//# 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
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEzF;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,WAAW,GAAG,KAAK,GAAG,QAAQ,CAAC"}
|
package/type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents a cryptographic algorithm.\n * Supported algorithms include: 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'.\n */\nexport type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';\n\n/**\n * Represents the encoding options for cryptographic operations.\n * The encoding can be one of the following: 'base64', 'base64url', 'hex', or 'binary'.\n */\nexport type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary';\n"]}
|
package/user.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { AlwatrHashGenerator } from './hash.js';
|
|
2
|
-
import { AlwatrTokenGenerator } from './token.js';
|
|
3
|
-
import type { HashGeneratorConfig, TokenGeneratorConfig, TokenStatus } from './type.js';
|
|
4
|
-
/**
|
|
5
|
-
* User factory for generating self-validate user-id and user-token.
|
|
6
|
-
*/
|
|
7
|
-
export declare class AlwatrUserFactory {
|
|
8
|
-
protected _tokenGenerator: AlwatrTokenGenerator;
|
|
9
|
-
protected _hashGenerator: AlwatrHashGenerator;
|
|
10
|
-
constructor(hashConfig: HashGeneratorConfig, tokenConfig: TokenGeneratorConfig);
|
|
11
|
-
/**
|
|
12
|
-
* Generate new self-verifiable user-id.
|
|
13
|
-
*
|
|
14
|
-
* Example:
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* const newUser = {
|
|
18
|
-
* id: userFactory.generateId(),
|
|
19
|
-
* ...
|
|
20
|
-
* }
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
generateId(): string;
|
|
24
|
-
/**
|
|
25
|
-
* Validate user-id without token.
|
|
26
|
-
*
|
|
27
|
-
* Example:
|
|
28
|
-
*
|
|
29
|
-
* ```ts
|
|
30
|
-
* if (!userFactory.verifyId(user.id)) {
|
|
31
|
-
* new Error('invalid_user');
|
|
32
|
-
* }
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
verifyId(id: string): boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Generate user auth token.
|
|
38
|
-
*
|
|
39
|
-
* Example:
|
|
40
|
-
*
|
|
41
|
-
* ```ts
|
|
42
|
-
* const userToken = userFactory.generateToken([user.id, user.lpe]);
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
generateToken(uniquelyList: (string | number | boolean)[]): string;
|
|
46
|
-
/**
|
|
47
|
-
* Verify user auth token.
|
|
48
|
-
*
|
|
49
|
-
* Example:
|
|
50
|
-
*
|
|
51
|
-
* ```ts
|
|
52
|
-
* if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {
|
|
53
|
-
* new error('invalid_token');
|
|
54
|
-
* }
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
verifyToken(uniquelyList: (string | number | boolean)[], token: string): TokenStatus;
|
|
58
|
-
}
|
|
59
|
-
//# sourceMappingURL=user.d.ts.map
|
package/user.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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;gBAEb,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAE,oBAAoB;IAK9E;;;;;;;;;;;OAWG;IACH,UAAU,IAAI,MAAM;IAIpB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI7B;;;;;;;;OAQG;IACH,aAAa,CAAC,YAAY,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,GAAG,MAAM;IAIlE;;;;;;;;;;OAUG;IACH,WAAW,CAAC,YAAY,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW;CAGrF"}
|
package/user.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { AlwatrHashGenerator } from './hash.js';
|
|
2
|
-
import { AlwatrTokenGenerator } from './token.js';
|
|
3
|
-
/**
|
|
4
|
-
* User factory for generating self-validate user-id and user-token.
|
|
5
|
-
*/
|
|
6
|
-
export class AlwatrUserFactory {
|
|
7
|
-
constructor(hashConfig, tokenConfig) {
|
|
8
|
-
this._hashGenerator = new AlwatrHashGenerator(hashConfig);
|
|
9
|
-
this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Generate new self-verifiable user-id.
|
|
13
|
-
*
|
|
14
|
-
* Example:
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* const newUser = {
|
|
18
|
-
* id: userFactory.generateId(),
|
|
19
|
-
* ...
|
|
20
|
-
* }
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
generateId() {
|
|
24
|
-
return 'U' + this._hashGenerator.randomSelfValidate();
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Validate user-id without token.
|
|
28
|
-
*
|
|
29
|
-
* Example:
|
|
30
|
-
*
|
|
31
|
-
* ```ts
|
|
32
|
-
* if (!userFactory.verifyId(user.id)) {
|
|
33
|
-
* new Error('invalid_user');
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
verifyId(id) {
|
|
38
|
-
return this._hashGenerator.verifySelfValidate(id.substring(1));
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Generate user auth token.
|
|
42
|
-
*
|
|
43
|
-
* Example:
|
|
44
|
-
*
|
|
45
|
-
* ```ts
|
|
46
|
-
* const userToken = userFactory.generateToken([user.id, user.lpe]);
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
generateToken(uniquelyList) {
|
|
50
|
-
return this._tokenGenerator.generate(uniquelyList.join());
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Verify user auth token.
|
|
54
|
-
*
|
|
55
|
-
* Example:
|
|
56
|
-
*
|
|
57
|
-
* ```ts
|
|
58
|
-
* if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {
|
|
59
|
-
* new error('invalid_token');
|
|
60
|
-
* }
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
verifyToken(uniquelyList, token) {
|
|
64
|
-
return this._tokenGenerator.verify(uniquelyList.join(), token);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
//# sourceMappingURL=user.js.map
|
package/user.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,YAAY,UAA+B,EAAE,WAAiC;QAC5E,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,YAA2C;QACvD,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;;;;OAUG;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 {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(hashConfig: HashGeneratorConfig, tokenConfig: TokenGeneratorConfig) {\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: (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: (string | number | boolean)[], token: string): TokenStatus {\n return this._tokenGenerator.verify(uniquelyList.join(), token);\n }\n}\n"]}
|