@alwatr/crypto 0.31.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 +109 -0
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/hash.d.ts +79 -0
- package/hash.d.ts.map +1 -0
- package/hash.js +100 -0
- package/hash.js.map +1 -0
- package/index.d.ts +5 -0
- package/index.d.ts.map +1 -0
- package/index.js +10 -0
- package/index.js.map +1 -0
- package/package.json +48 -0
- package/token.d.ts +28 -0
- package/token.d.ts.map +1 -0
- package/token.js +52 -0
- package/token.js.map +1 -0
- package/type.d.ts +44 -0
- package/type.d.ts.map +1 -0
- package/type.js +2 -0
- package/type.js.map +1 -0
- package/user.d.ts +59 -0
- package/user.d.ts.map +1 -0
- package/user.js +67 -0
- package/user.js.map +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.31.0](https://github.com/AliMD/alwatr/compare/v0.30.0...v0.31.0) (2023-05-08)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **crypto:** package ([866c5f4](https://github.com/AliMD/alwatr/commit/866c5f490ea2eaa75bf177f35b3f4711931e13d2))
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
- **crypto:** make prefix for userId ([5baa00a](https://github.com/AliMD/alwatr/commit/5baa00aafb16a4c6ed1d77913edddd090f732dad))
|
|
15
|
+
|
|
16
|
+
# [0.30.0](https://github.com/AliMD/alwatr/compare/v0.29.0...v0.30.0) (2023-03-06)
|
|
17
|
+
|
|
18
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
19
|
+
|
|
20
|
+
# [0.29.0](https://github.com/AliMD/alwatr/compare/v0.28.0...v0.29.0) (2023-02-10)
|
|
21
|
+
|
|
22
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
23
|
+
|
|
24
|
+
# [0.28.0](https://github.com/AliMD/alwatr/compare/v0.27.0...v0.28.0) (2023-01-20)
|
|
25
|
+
|
|
26
|
+
### Bug Fixes
|
|
27
|
+
|
|
28
|
+
- **token:** types ([a7da60e](https://github.com/AliMD/alwatr/commit/a7da60e720ac83b8d2d2ed5c0b811dea1952a2b9))
|
|
29
|
+
|
|
30
|
+
### Features
|
|
31
|
+
|
|
32
|
+
- **token:** generate and verify token without expiration time ([6db78f0](https://github.com/AliMD/alwatr/commit/6db78f0644e076c3401a263173d7139838bbbf0c))
|
|
33
|
+
- **type:** define math types ([8c19f40](https://github.com/AliMD/alwatr/commit/8c19f4058d4361b7d3f4f714595e34cb6fa21109))
|
|
34
|
+
|
|
35
|
+
# [0.27.0](https://github.com/AliMD/alwatr/compare/v0.26.0...v0.27.0) (2022-12-29)
|
|
36
|
+
|
|
37
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
38
|
+
|
|
39
|
+
# [0.26.0](https://github.com/AliMD/alwatr/compare/v0.25.0...v0.26.0) (2022-12-22)
|
|
40
|
+
|
|
41
|
+
### Bug Fixes
|
|
42
|
+
|
|
43
|
+
- set correct path ([d01ce6f](https://github.com/AliMD/alwatr/commit/d01ce6ffa749a5e3e0e11e35b4ed61d75d61fec9))
|
|
44
|
+
- tsconfig ([e96dcd3](https://github.com/AliMD/alwatr/commit/e96dcd30774a9f06f7d051e0504192cbbe019e35))
|
|
45
|
+
|
|
46
|
+
# [0.25.0](https://github.com/AliMD/alwatr/compare/v0.24.1...v0.25.0) (2022-12-07)
|
|
47
|
+
|
|
48
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
49
|
+
|
|
50
|
+
## [0.24.1](https://github.com/AliMD/alwatr/compare/v0.24.0...v0.24.1) (2022-12-01)
|
|
51
|
+
|
|
52
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
53
|
+
|
|
54
|
+
# [0.24.0](https://github.com/AliMD/alwatr/compare/v0.23.0...v0.24.0) (2022-11-28)
|
|
55
|
+
|
|
56
|
+
### Bug Fixes
|
|
57
|
+
|
|
58
|
+
- use ~ for package version ([4e027ff](https://github.com/AliMD/alwatr/commit/4e027ff63875e03b088ebcdc1bdf2495f4494eec))
|
|
59
|
+
|
|
60
|
+
# [0.23.0](https://github.com/AliMD/alwatr/compare/v0.22.1...v0.23.0) (2022-11-23)
|
|
61
|
+
|
|
62
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
63
|
+
|
|
64
|
+
## [0.22.1](https://github.com/AliMD/alwatr/compare/v0.22.0...v0.22.1) (2022-11-21)
|
|
65
|
+
|
|
66
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
67
|
+
|
|
68
|
+
# [0.22.0](https://github.com/AliMD/alwatr/compare/v0.21.0...v0.22.0) (2022-11-20)
|
|
69
|
+
|
|
70
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
71
|
+
|
|
72
|
+
# [0.21.0](https://github.com/AliMD/alwatr/compare/v0.20.0...v0.21.0) (2022-11-13)
|
|
73
|
+
|
|
74
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
75
|
+
|
|
76
|
+
# [0.20.0](https://github.com/AliMD/alwatr/compare/v0.19.0...v0.20.0) (2022-11-05)
|
|
77
|
+
|
|
78
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
79
|
+
|
|
80
|
+
# [0.19.0](https://github.com/AliMD/alwatr/compare/v0.18.0...v0.19.0) (2022-11-01)
|
|
81
|
+
|
|
82
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
83
|
+
|
|
84
|
+
# [0.18.0](https://github.com/AliMD/alwatr/compare/v0.17.0...v0.18.0) (2022-10-22)
|
|
85
|
+
|
|
86
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
87
|
+
|
|
88
|
+
# [0.17.0](https://github.com/AliMD/alwatr/compare/v0.16.1...v0.17.0) (2022-10-21)
|
|
89
|
+
|
|
90
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
91
|
+
|
|
92
|
+
# [0.16.0](https://github.com/AliMD/alwatr/compare/v0.15.0...v0.16.0) (2022-09-08)
|
|
93
|
+
|
|
94
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
95
|
+
|
|
96
|
+
# [0.15.0](https://github.com/AliMD/alwatr/compare/v0.14.0...v0.15.0) (2022-09-01)
|
|
97
|
+
|
|
98
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
99
|
+
|
|
100
|
+
# [0.14.0](https://github.com/AliMD/alwatr/compare/v0.13.0...v0.14.0) (2022-08-19)
|
|
101
|
+
|
|
102
|
+
**Note:** Version bump only for package @alwatr/crypto
|
|
103
|
+
|
|
104
|
+
# [0.13.0](https://github.com/AliMD/alwatr/compare/v0.12.0...v0.13.0) (2022-08-06)
|
|
105
|
+
|
|
106
|
+
### Features
|
|
107
|
+
|
|
108
|
+
- **token:** generate and verify HOTP tpkens ([d0372f8](https://github.com/AliMD/alwatr/commit/d0372f805a45d6fd6571b50821529068cec7d424))
|
|
109
|
+
- **token:** new package files ([fe620e0](https://github.com/AliMD/alwatr/commit/fe620e0d9f84c4e6d8e0eed47d6b398e218429ad))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 S. Ali Mihandoost
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Alwatr Crypto - `@alwatr/crypto`
|
|
2
|
+
|
|
3
|
+
Secure authentication HOTP token generator (the HMAC-based One-Time Password algorithm) and crypto utils written in tiny TypeScript module.
|
|
4
|
+
|
|
5
|
+
## References
|
|
6
|
+
|
|
7
|
+
- [RFC 4226](http://tools.ietf.org/html/rfc4226). HMAC-Based One-Time Password Algorithm (HOTP)
|
|
8
|
+
- [RFC 6238](http://tools.ietf.org/html/rfc6238). Time-Based One-Time Password Algorithm (TOTP)
|
|
9
|
+
- [HMAC: Keyed-Hashing for Message Authentication](https://tools.ietf.org/html/rfc2104). (February 1997). Network Working Group.
|
|
10
|
+
- [HMAC and Key Derivation](https://cryptobook.nakov.com/mac-and-key-derivation/hmac-and-key-derivation). Practical Cryptography for Developers.
|
|
11
|
+
- [HMAC Generator/Tester Tool](https://www.freeformatter.com/hmac-generator.html). FreeFormatter.
|
|
12
|
+
- [How API Request Signing Works (And How to Implement HMAC in NodeJS)](https://blog.andrewhoang.me/how-api-request-signing-works-and-how-to-implement-it-in-nodejs-2/). (2016). Andrew Hoang.
|
|
13
|
+
- [Implement HMAC Authentication](https://support.google.com/admanager/answer/7637490?hl=en). Google Ad Manager Help.
|
package/hash.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { type BinaryLike } from 'node:crypto';
|
|
3
|
+
import type { HashGeneratorConfig } from './type.js';
|
|
4
|
+
/**
|
|
5
|
+
* Secure **self-validate** hash generator.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AlwatrHashGenerator {
|
|
8
|
+
config: HashGeneratorConfig;
|
|
9
|
+
constructor(config?: HashGeneratorConfig);
|
|
10
|
+
/**
|
|
11
|
+
* Generate simple hash from random data.
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
*
|
|
15
|
+
* ```js
|
|
16
|
+
* const clientId = hashGenerator.random();
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
random(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Generate **self-validate** hash from random data.
|
|
22
|
+
*
|
|
23
|
+
* Example:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* const userId = hashGenerator.randomSelfValidate();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
randomSelfValidate(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Generate simple hash from data.
|
|
32
|
+
*
|
|
33
|
+
* Example:
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const crcHash = hashGenerator.generate(downloadedData);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
generate(data: BinaryLike): string;
|
|
40
|
+
/**
|
|
41
|
+
* Generate crc hash.
|
|
42
|
+
*/
|
|
43
|
+
_generateCrc(data: BinaryLike): string;
|
|
44
|
+
/**
|
|
45
|
+
* Generate **self-validate** hash from data.
|
|
46
|
+
*
|
|
47
|
+
* Example:
|
|
48
|
+
*
|
|
49
|
+
* ```js
|
|
50
|
+
* const userId = hashGenerator.generateSelfValidate(randomData);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
generateSelfValidate(data: BinaryLike): string;
|
|
54
|
+
/**
|
|
55
|
+
* Verify `generate(data)` equals to `hash`.
|
|
56
|
+
*
|
|
57
|
+
* Example:
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* if (!hashGenerator.verify(downloadedData, crcHash)) {
|
|
61
|
+
* new Error('data_corrupted');
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
verify(data: BinaryLike, hash: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Verify a **self-validate** hash to check its generated by this class (same options).
|
|
68
|
+
*
|
|
69
|
+
* Example:
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* if (!hashGenerator.verifySelfValidate(user.id)) {
|
|
73
|
+
* new Error('invalid_user');
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
verifySelfValidate(hash: string): boolean;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=hash.d.ts.map
|
package/hash.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":";AAAA,OAAO,EAA0B,KAAK,UAAU,EAAC,MAAM,aAAa,CAAC;AAErE,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAEnD;;GAEG;AACH,qBAAa,mBAAmB;IACX,MAAM,EAAE,mBAAmB;gBAA3B,MAAM,GAAE,mBAA8E;IAEzG;;;;;;;;OAQG;IACH,MAAM,IAAI,MAAM;IAIhB;;;;;;;;OAQG;IACH,kBAAkB,IAAI,MAAM;IAI5B;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAIlC;;OAEG;IACH,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAKtC;;;;;;;;OAQG;IACH,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAM9C;;;;;;;;;;OAUG;IACH,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAI/C;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;CAS1C"}
|
package/hash.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Secure **self-validate** hash generator.
|
|
4
|
+
*/
|
|
5
|
+
export class AlwatrHashGenerator {
|
|
6
|
+
constructor(config = { algorithm: 'sha1', encoding: 'base64url', crcLength: 8 }) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generate simple hash from random data.
|
|
11
|
+
*
|
|
12
|
+
* Example:
|
|
13
|
+
*
|
|
14
|
+
* ```js
|
|
15
|
+
* const clientId = hashGenerator.random();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
random() {
|
|
19
|
+
return this.generate(randomBytes(16));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Generate **self-validate** hash from random data.
|
|
23
|
+
*
|
|
24
|
+
* Example:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const userId = hashGenerator.randomSelfValidate();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
randomSelfValidate() {
|
|
31
|
+
return this.generateSelfValidate(randomBytes(16));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generate simple hash from data.
|
|
35
|
+
*
|
|
36
|
+
* Example:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* const crcHash = hashGenerator.generate(downloadedData);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
generate(data) {
|
|
43
|
+
return createHash(this.config.algorithm).update(data).digest(this.config.encoding);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Generate crc hash.
|
|
47
|
+
*/
|
|
48
|
+
_generateCrc(data) {
|
|
49
|
+
const crc = this.generate(data);
|
|
50
|
+
return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.substring(0, this.config.crcLength);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Generate **self-validate** hash from data.
|
|
54
|
+
*
|
|
55
|
+
* Example:
|
|
56
|
+
*
|
|
57
|
+
* ```js
|
|
58
|
+
* const userId = hashGenerator.generateSelfValidate(randomData);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
generateSelfValidate(data) {
|
|
62
|
+
const mainHash = this.generate(data);
|
|
63
|
+
const crcHash = this._generateCrc(mainHash);
|
|
64
|
+
return mainHash + crcHash;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Verify `generate(data)` equals to `hash`.
|
|
68
|
+
*
|
|
69
|
+
* Example:
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* if (!hashGenerator.verify(downloadedData, crcHash)) {
|
|
73
|
+
* new Error('data_corrupted');
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
verify(data, hash) {
|
|
78
|
+
return hash === this.generate(data);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Verify a **self-validate** hash to check its generated by this class (same options).
|
|
82
|
+
*
|
|
83
|
+
* Example:
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* if (!hashGenerator.verifySelfValidate(user.id)) {
|
|
87
|
+
* new Error('invalid_user');
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
verifySelfValidate(hash) {
|
|
92
|
+
const gapPos = this.config.crcLength == null || this.config.crcLength < 1
|
|
93
|
+
? hash.length / 2
|
|
94
|
+
: hash.length - this.config.crcLength;
|
|
95
|
+
const mainHash = hash.substring(0, gapPos);
|
|
96
|
+
const crcHash = hash.substring(gapPos);
|
|
97
|
+
return crcHash === this._generateCrc(mainHash);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=hash.js.map
|
package/hash.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,WAAW,EAAkB,MAAM,aAAa,CAAC;AAIrE;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAC9B,YAAmB,SAA8B,EAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,EAAC;QAAtF,WAAM,GAAN,MAAM,CAAgF;IAAG,CAAC;IAE7G;;;;;;;;OAQG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,kBAAkB;QAChB,OAAO,IAAI,CAAC,oBAAoB,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAgB;QACvB,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACrF,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,IAAgB;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpH,CAAC;IAED;;;;;;;;OAQG;IACH,oBAAoB,CAAC,IAAgB;QACnC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACrC,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC5C,OAAO,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;OAUG;IACH,MAAM,CAAC,IAAgB,EAAE,IAAY;QACnC,OAAO,IAAI,KAAK,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,IAAY;QAC7B,MAAM,MAAM,GACV,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC;YACxD,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC;YACjB,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;QAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAC3C,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QACvC,OAAO,OAAO,KAAK,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;IACjD,CAAC;CACF","sourcesContent":["import {createHash, randomBytes, type BinaryLike} from 'node:crypto';\n\nimport type {HashGeneratorConfig} from './type.js';\n\n/**\n * Secure **self-validate** hash generator.\n */\nexport class AlwatrHashGenerator {\n constructor(public config: HashGeneratorConfig = {algorithm: 'sha1', encoding: 'base64url', crcLength: 8}) {}\n\n /**\n * Generate simple hash from random data.\n *\n * Example:\n *\n * ```js\n * const clientId = hashGenerator.random();\n * ```\n */\n random(): string {\n return this.generate(randomBytes(16));\n }\n\n /**\n * Generate **self-validate** hash from random data.\n *\n * Example:\n *\n * ```ts\n * const userId = hashGenerator.randomSelfValidate();\n * ```\n */\n randomSelfValidate(): string {\n return this.generateSelfValidate(randomBytes(16));\n }\n\n /**\n * Generate simple hash from data.\n *\n * Example:\n *\n * ```ts\n * const crcHash = hashGenerator.generate(downloadedData);\n * ```\n */\n generate(data: BinaryLike): string {\n return createHash(this.config.algorithm).update(data).digest(this.config.encoding);\n }\n\n /**\n * Generate crc hash.\n */\n _generateCrc(data: BinaryLike): string {\n const crc = this.generate(data);\n return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.substring(0, this.config.crcLength);\n }\n\n /**\n * Generate **self-validate** hash from data.\n *\n * Example:\n *\n * ```js\n * const userId = hashGenerator.generateSelfValidate(randomData);\n * ```\n */\n generateSelfValidate(data: BinaryLike): string {\n const mainHash = this.generate(data);\n const crcHash = this._generateCrc(mainHash);\n return mainHash + crcHash;\n }\n\n /**\n * Verify `generate(data)` equals to `hash`.\n *\n * Example:\n *\n * ```ts\n * if (!hashGenerator.verify(downloadedData, crcHash)) {\n * new Error('data_corrupted');\n * }\n * ```\n */\n verify(data: BinaryLike, hash: string): boolean {\n return hash === this.generate(data);\n }\n\n /**\n * Verify a **self-validate** hash to check its generated by this class (same options).\n *\n * Example:\n *\n * ```ts\n * if (!hashGenerator.verifySelfValidate(user.id)) {\n * new Error('invalid_user');\n * }\n * ```\n */\n verifySelfValidate(hash: string): boolean {\n const gapPos =\n this.config.crcLength == null || this.config.crcLength < 1\n ? hash.length / 2\n : hash.length - this.config.crcLength;\n const mainHash = hash.substring(0, gapPos);\n const crcHash = hash.substring(gapPos);\n return crcHash === this._generateCrc(mainHash);\n }\n}\n"]}
|
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { globalAlwatr } from '@alwatr/logger';
|
|
2
|
+
export * from './hash.js';
|
|
3
|
+
export * from './token.js';
|
|
4
|
+
export * from './user.js';
|
|
5
|
+
export * from './type.js';
|
|
6
|
+
globalAlwatr.registeredList.push({
|
|
7
|
+
name: '@alwatr/crypto',
|
|
8
|
+
version: _ALWATR_VERSION_,
|
|
9
|
+
});
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAE1B,YAAY,CAAC,cAAc,CAAC,IAAI,CAAC;IAC/B,IAAI,EAAE,gBAAgB;IACtB,OAAO,EAAE,gBAAgB;CAC1B,CAAC,CAAC","sourcesContent":["import {globalAlwatr} from '@alwatr/logger';\n\nexport * from './hash.js';\nexport * from './token.js';\nexport * from './user.js';\nexport * from './type.js';\n\nglobalAlwatr.registeredList.push({\n name: '@alwatr/crypto',\n version: _ALWATR_VERSION_,\n});\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alwatr/crypto",
|
|
3
|
+
"version": "0.31.0",
|
|
4
|
+
"description": "Secure authentication HOTP token generator (the HMAC-based One-Time Password algorithm) and crypto utils written in tiny TypeScript module.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"crypto",
|
|
7
|
+
"hash",
|
|
8
|
+
"token",
|
|
9
|
+
"authentication",
|
|
10
|
+
"auth",
|
|
11
|
+
"access",
|
|
12
|
+
"token",
|
|
13
|
+
"hmac",
|
|
14
|
+
"time",
|
|
15
|
+
"otp",
|
|
16
|
+
"hotp",
|
|
17
|
+
"otp-token",
|
|
18
|
+
"typescript",
|
|
19
|
+
"esm",
|
|
20
|
+
"alwatr"
|
|
21
|
+
],
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "index.d.ts",
|
|
25
|
+
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"files": [
|
|
28
|
+
"**/*.{d.ts.map,d.ts,js.map,js,html,md}"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/AliMD/alwatr",
|
|
36
|
+
"directory": "core/crypto"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/AliMD/alwatr/tree/main/core/crypto#readme",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/AliMD/alwatr/issues"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@alwatr/logger": "^0.31.0",
|
|
44
|
+
"@alwatr/math": "^0.31.0",
|
|
45
|
+
"tslib": "^2.5.0"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "896e64b58eed6e9048e870557ecf399d42705612"
|
|
48
|
+
}
|
package/token.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { TokenGeneratorConfig, TokenStatus } from './type.js';
|
|
2
|
+
/**
|
|
3
|
+
* Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).
|
|
4
|
+
*/
|
|
5
|
+
export declare class AlwatrTokenGenerator {
|
|
6
|
+
config: TokenGeneratorConfig;
|
|
7
|
+
protected _duration: number | null;
|
|
8
|
+
get epoch(): number;
|
|
9
|
+
constructor(config: TokenGeneratorConfig);
|
|
10
|
+
protected _generate(data: string, epoch: number): string;
|
|
11
|
+
/**
|
|
12
|
+
* Generate HOTP token from data base on special duration.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
generate(data: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Token verification.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
verify(data: string, token: string): TokenStatus;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=token.d.ts.map
|
package/token.d.ts.map
ADDED
|
@@ -0,0 +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;IAOZ,MAAM,EAAE,oBAAoB;IAN/C,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAEnC,IAAI,KAAK,IAAI,MAAM,CAElB;gBAEkB,MAAM,EAAE,oBAAoB;IAI/C,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAMxD;;;;;;OAMG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAI9B;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW;CAejD"}
|
package/token.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createHmac } from 'node:crypto';
|
|
2
|
+
import { parseDuration } from '@alwatr/math';
|
|
3
|
+
/**
|
|
4
|
+
* Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).
|
|
5
|
+
*/
|
|
6
|
+
export class AlwatrTokenGenerator {
|
|
7
|
+
get epoch() {
|
|
8
|
+
return this._duration == null ? 0 : Math.floor(Date.now() / this._duration);
|
|
9
|
+
}
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
this._duration = config.duration == null ? null : parseDuration(config.duration);
|
|
13
|
+
}
|
|
14
|
+
_generate(data, epoch) {
|
|
15
|
+
return createHmac(this.config.algorithm, data)
|
|
16
|
+
.update(data + epoch)
|
|
17
|
+
.digest(this.config.encoding);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Generate HOTP token from data base on special duration.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* user.auth = tokenGenerator.generate(`${user.id}-${user.role}`);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
generate(data) {
|
|
27
|
+
return this._generate(data, this.epoch);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Token verification.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
verify(data, token) {
|
|
37
|
+
const epoch = this.epoch;
|
|
38
|
+
if (token === this._generate(data, epoch)) {
|
|
39
|
+
return 'valid';
|
|
40
|
+
}
|
|
41
|
+
else if (this._duration == null) {
|
|
42
|
+
return 'invalid';
|
|
43
|
+
}
|
|
44
|
+
else if (token === this._generate(data, epoch - 1)) {
|
|
45
|
+
return 'expired';
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
return 'invalid';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=token.js.map
|
package/token.js.map
ADDED
|
@@ -0,0 +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,MAAM,CAAC,QAAQ,CAAC,CAAC;IACnF,CAAC;IAES,SAAS,CAAC,IAAY,EAAE,KAAa;QAC7C,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC;aACzC,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC;aACpB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACpC,CAAC;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,IAAY;QACnB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,IAAY,EAAE,KAAa;QAChC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,IAAI,KAAK,KAAK,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE;YACzC,OAAO,OAAO,CAAC;SAChB;aACI,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,EAAE;YAC/B,OAAO,SAAS,CAAC;SAClB;aACI,IAAI,KAAK,KAAK,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC,EAAE;YAClD,OAAO,SAAS,CAAC;SAClB;aACI;YACH,OAAO,SAAS,CAAC;SAClB;IACH,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 get epoch(): number {\n return this._duration == null ? 0 : Math.floor(Date.now() / this._duration);\n }\n\n constructor(public config: TokenGeneratorConfig) {\n this._duration = config.duration == null ? null : parseDuration(config.duration);\n }\n\n protected _generate(data: string, epoch: number): string {\n return createHmac(this.config.algorithm, data)\n .update(data + epoch)\n .digest(this.config.encoding);\n }\n\n /**\n * Generate HOTP token from data base on special duration.\n *\n * ```ts\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 * Token verification.\n *\n * ```ts\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"]}
|
package/type.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { DurationString } from '@alwatr/math';
|
|
2
|
+
export type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';
|
|
3
|
+
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
|
+
//# sourceMappingURL=type.d.ts.map
|
package/type.d.ts.map
ADDED
|
@@ -0,0 +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,QAAQ,GAAG,QAAQ,CAAC;AACzF,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,WAAW,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEvE,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC;AAC1D,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,SAAS,CAAC;AAG7C,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;;OAIG;IACH,QAAQ,EAAE,cAAc,GAAG,IAAI,CAAC;IAEhC;;OAEG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;CAC1B;AAED,MAAM,WAAW,mBAAmB;IAClC;;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,iBAAiB;IAChC,WAAW,EAAE,oBAAoB,CAAC;IAClC,UAAU,EAAE,mBAAmB,CAAC;CACjC"}
|
package/type.js
ADDED
package/type.js.map
ADDED
|
@@ -0,0 +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\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 UserFactoryConfig {\n tokenConfig: TokenGeneratorConfig;\n hashConfig: HashGeneratorConfig;\n}\n"]}
|
package/user.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AlwatrHashGenerator } from './hash.js';
|
|
2
|
+
import { AlwatrTokenGenerator } from './token.js';
|
|
3
|
+
import type { HashGeneratorConfig, TokenGeneratorConfig, TokenStatus } from './type.js';
|
|
4
|
+
/**
|
|
5
|
+
* Secure User ID/Token Factory (generate and validate authentication process).
|
|
6
|
+
*/
|
|
7
|
+
export declare class AlwatrUserFactory {
|
|
8
|
+
protected _tokenGenerator: AlwatrTokenGenerator;
|
|
9
|
+
protected _hashGenerator: AlwatrHashGenerator;
|
|
10
|
+
constructor(tokenConfig: TokenGeneratorConfig, hashConfig?: HashGeneratorConfig);
|
|
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: Array<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: Array<string | number | boolean>, token: string): TokenStatus;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=user.d.ts.map
|
package/user.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAEhD,OAAO,KAAK,EAAC,mBAAmB,EAAE,oBAAoB,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEtF;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,SAAS,CAAC,eAAe,uBAAC;IAC1B,SAAS,CAAC,cAAc,sBAAC;gBAGrB,WAAW,EAAE,oBAAoB,EACjC,UAAU,CAAC,EAAE,mBAAmB;IAMpC;;;;;;;;;;;OAWG;IACH,UAAU,IAAI,MAAM;IAIpB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI7B;;;;;;;;OAQG;IACH,aAAa,CAAC,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GAAG,MAAM;IAIrE;;;;;;;;;;OAUG;IACH,WAAW,CAAC,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW;CAGxF"}
|
package/user.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { AlwatrHashGenerator } from './hash.js';
|
|
2
|
+
import { AlwatrTokenGenerator } from './token.js';
|
|
3
|
+
/**
|
|
4
|
+
* Secure User ID/Token Factory (generate and validate authentication process).
|
|
5
|
+
*/
|
|
6
|
+
export class AlwatrUserFactory {
|
|
7
|
+
constructor(tokenConfig, hashConfig) {
|
|
8
|
+
this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig);
|
|
9
|
+
this._hashGenerator = new AlwatrHashGenerator(hashConfig);
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["src/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAIhD;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAI5B,YACI,WAAiC,EACjC,UAAgC;QAElC,IAAI,CAAC,eAAe,GAAG,IAAI,oBAAoB,CAAC,WAAW,CAAC,CAAC;QAC7D,IAAI,CAAC,cAAc,GAAG,IAAI,mBAAmB,CAAC,UAAU,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;;;;;OAWG;IACH,UAAU;QACR,OAAO,GAAG,GAAG,IAAI,CAAC,cAAc,CAAC,kBAAkB,EAAE,CAAC;IACxD,CAAC;IAED;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAU;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC,kBAAkB,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IACjE,CAAC;IAED;;;;;;;;OAQG;IACH,aAAa,CAAC,YAA8C;QAC1D,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;;;;;OAUG;IACH,WAAW,CAAC,YAA8C,EAAE,KAAa;QACvE,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;IACjE,CAAC;CACF","sourcesContent":["import {AlwatrHashGenerator} from './hash.js';\nimport {AlwatrTokenGenerator} from './token.js';\n\nimport type {HashGeneratorConfig, TokenGeneratorConfig, TokenStatus} from './type.js';\n\n/**\n * Secure User ID/Token Factory (generate and validate authentication process).\n */\nexport class AlwatrUserFactory {\n protected _tokenGenerator;\n protected _hashGenerator;\n\n constructor(\n tokenConfig: TokenGeneratorConfig,\n hashConfig?: HashGeneratorConfig,\n ) {\n this._tokenGenerator = new AlwatrTokenGenerator(tokenConfig);\n this._hashGenerator = new AlwatrHashGenerator(hashConfig);\n }\n\n /**\n * Generate new self-verifiable user-id.\n *\n * Example:\n *\n * ```ts\n * const newUser = {\n * id: userFactory.generateId(),\n * ...\n * }\n * ```\n */\n generateId(): string {\n return 'U' + this._hashGenerator.randomSelfValidate();\n }\n\n /**\n * Validate user-id without token.\n *\n * Example:\n *\n * ```ts\n * if (!userFactory.verifyId(user.id)) {\n * new Error('invalid_user');\n * }\n * ```\n */\n verifyId(id: string): boolean {\n return this._hashGenerator.verifySelfValidate(id.substring(1));\n }\n\n /**\n * Generate user auth token.\n *\n * Example:\n *\n * ```ts\n * const userToken = userFactory.generateToken([user.id, user.lpe]);\n * ```\n */\n generateToken(uniquelyList: Array<string | number | boolean>): string {\n return this._tokenGenerator.generate(uniquelyList.join());\n }\n\n /**\n * Verify user auth token.\n *\n * Example:\n *\n * ```ts\n * if (!userFactory.verifyToken([user.id, user.lpe], userToken)) {\n * new error('invalid_token');\n * }\n * ```\n */\n verifyToken(uniquelyList: Array<string | number | boolean>, token: string): TokenStatus {\n return this._tokenGenerator.verify(uniquelyList.join(), token);\n }\n}\n"]}
|