itrm-tools 1.4.7 → 1.4.9
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/dist/api/request/TemporalMemoryManager.d.ts +3 -1
- package/dist/api/request/TemporalMemoryManager.js +22 -4
- package/dist/api/request/TemporalMemoryManager.js.map +1 -1
- package/dist/api/request/crud/common/ServiceSessionManager.d.ts +11 -3
- package/dist/api/request/crud/common/ServiceSessionManager.js +107 -97
- package/dist/api/request/crud/common/ServiceSessionManager.js.map +1 -1
- package/dist/cryptography/password/PasswordUtils.d.ts +1 -0
- package/dist/cryptography/password/PasswordUtils.js +20 -17
- package/dist/cryptography/password/PasswordUtils.js.map +1 -1
- package/package.json +5 -5
|
@@ -6,7 +6,9 @@ export declare class TemporalMemoryManager<P> {
|
|
|
6
6
|
constructor(duration?: number, revisionTime?: number);
|
|
7
7
|
generateKey(...values: unknown[]): string;
|
|
8
8
|
getValue(key: string): P | undefined;
|
|
9
|
-
saveValue(key: string, value: P): P;
|
|
9
|
+
saveValue(key: string, value: P, duration?: number): P;
|
|
10
|
+
remove(key: string): void;
|
|
11
|
+
removeByPrefix(prefix: string): number;
|
|
10
12
|
cleanAll(): void;
|
|
11
13
|
cleanExpired(): void;
|
|
12
14
|
}
|
|
@@ -23,14 +23,32 @@ class TemporalMemoryManager {
|
|
|
23
23
|
}
|
|
24
24
|
getValue(key) {
|
|
25
25
|
this.cleanExpired();
|
|
26
|
-
|
|
26
|
+
const entry = this.memory[key];
|
|
27
|
+
if (!entry)
|
|
28
|
+
return undefined;
|
|
29
|
+
if (entry.expirationTime <= Date.now()) {
|
|
27
30
|
delete this.memory[key];
|
|
28
|
-
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
return entry.payload;
|
|
29
34
|
}
|
|
30
|
-
saveValue(key, value) {
|
|
31
|
-
this.memory[key] = { payload: value, expirationTime: Date.now() + this.duration };
|
|
35
|
+
saveValue(key, value, duration) {
|
|
36
|
+
this.memory[key] = { payload: value, expirationTime: Date.now() + (duration ?? this.duration) };
|
|
32
37
|
return value;
|
|
33
38
|
}
|
|
39
|
+
remove(key) {
|
|
40
|
+
delete this.memory[key];
|
|
41
|
+
}
|
|
42
|
+
removeByPrefix(prefix) {
|
|
43
|
+
let removed = 0;
|
|
44
|
+
for (const key of Object.keys(this.memory)) {
|
|
45
|
+
if (key.startsWith(prefix)) {
|
|
46
|
+
delete this.memory[key];
|
|
47
|
+
removed++;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return removed;
|
|
51
|
+
}
|
|
34
52
|
cleanAll() {
|
|
35
53
|
for (let key of Object.keys(this.memory))
|
|
36
54
|
delete this.memory[key];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TemporalMemoryManager.js","sourceRoot":"","sources":["../../../src/api/request/TemporalMemoryManager.ts"],"names":[],"mappings":";;;AAAA,MAAa,qBAAqB;IACb,YAAY,CAAS;IACrB,QAAQ,CAAS;IAE1B,MAAM,GAA+D,EAAE,CAAC;IACxE,gBAAgB,GAAW,CAAC,CAAC;IAErC,YAAY,QAAiB,EAAE,YAAqB;QAChD,IAAI,CAAC,YAAY,GAAG,YAAY,IAAI,CAAC,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;QACrD,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC;IAEM,WAAW,CAAC,GAAG,MAAiB;QACnC,OAAO,MAAM;aACR,GAAG,CAAC,GAAG,CAAC,EAAE;YACP,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS;gBACjC,OAAO,MAAM,CAAC;iBACb,IAAI,OAAO,GAAG,KAAK,QAAQ;gBAC5B,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;YAC/B,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC,CAAC;aACD,IAAI,CAAC,G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,CAAC,cAAc,GAAG,GAAG;oBACrC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACpC,CAAC;IACL,CAAC;CACJ;AArED,sDAqEC"}
|
|
@@ -9,21 +9,29 @@ export declare class ServiceSessionManager {
|
|
|
9
9
|
private readonly role;
|
|
10
10
|
private readonly application;
|
|
11
11
|
private readonly platform;
|
|
12
|
+
private readonly keysValidation;
|
|
13
|
+
private readonly tokenValidation;
|
|
12
14
|
private keys?;
|
|
13
15
|
private authToken?;
|
|
14
|
-
constructor(service: string, role: string, application: string, platform: string, referer: string, secretKey: string, sessionServiceUrl: string);
|
|
16
|
+
constructor(service: string, role: string, application: string, platform: string, referer: string, secretKey: string, sessionServiceUrl: string, validationCacheDuration?: number);
|
|
15
17
|
requestHeaders(): Promise<AxiosRequestConfig>;
|
|
18
|
+
private resolveHeaders;
|
|
19
|
+
private withRetry;
|
|
20
|
+
private ensureKeys;
|
|
21
|
+
private isCurrentKeysValid;
|
|
16
22
|
private tryUseExistingToken;
|
|
23
|
+
private cachedValidation;
|
|
24
|
+
private validateCredentials;
|
|
25
|
+
private validateToken;
|
|
17
26
|
private requestNewToken;
|
|
18
|
-
private checkKeys;
|
|
19
27
|
private requestKeys;
|
|
20
|
-
private isCurrentKeysValid;
|
|
21
28
|
private handleTokenExpiration;
|
|
22
29
|
private exponentialBackoff;
|
|
23
30
|
private isUnauthorizedError;
|
|
24
31
|
buildHeaders(additionalHeaders?: Record<string, string>): {
|
|
25
32
|
headers: Record<string, string>;
|
|
26
33
|
};
|
|
34
|
+
private buildServiceHeaders;
|
|
27
35
|
encryptPayload(payload: string): string;
|
|
28
36
|
decryptPayload(payload: string): string;
|
|
29
37
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ServiceSessionManager = void 0;
|
|
4
|
+
const TemporalMemoryManager_1 = require("../../TemporalMemoryManager");
|
|
4
5
|
const AesCipheringAlgorithm_1 = require("../../../../cryptography/algorithm/AesCipheringAlgorithm");
|
|
5
6
|
const ApiKeySigner_1 = require("../../../../cryptography/sign/ApiKeySigner");
|
|
6
7
|
const RequestAxiosCall_1 = require("../../RequestAxiosCall");
|
|
7
8
|
const APIRequest_1 = require("../../../APIRequest");
|
|
8
9
|
const MAX_RETRIES = 3;
|
|
9
10
|
const RETRY_DELAY_MS = 100; // Base delay for exponential backoff
|
|
11
|
+
const DEFAULT_VALIDATION_CACHE_MS = 30 * 1000;
|
|
10
12
|
class ServiceSessionManager {
|
|
11
13
|
cipheringAlgorithm;
|
|
12
14
|
apiKeySigner;
|
|
@@ -17,9 +19,11 @@ class ServiceSessionManager {
|
|
|
17
19
|
role;
|
|
18
20
|
application;
|
|
19
21
|
platform;
|
|
22
|
+
keysValidation;
|
|
23
|
+
tokenValidation;
|
|
20
24
|
keys;
|
|
21
25
|
authToken;
|
|
22
|
-
constructor(service, role, application, platform, referer, secretKey, sessionServiceUrl) {
|
|
26
|
+
constructor(service, role, application, platform, referer, secretKey, sessionServiceUrl, validationCacheDuration = DEFAULT_VALIDATION_CACHE_MS) {
|
|
23
27
|
this.cipheringAlgorithm = new AesCipheringAlgorithm_1.AesCipheringAlgorithm();
|
|
24
28
|
this.apiKeySigner = new ApiKeySigner_1.ApiKeySigner();
|
|
25
29
|
this.serviceReferer = referer;
|
|
@@ -29,104 +33,120 @@ class ServiceSessionManager {
|
|
|
29
33
|
this.role = role;
|
|
30
34
|
this.application = application;
|
|
31
35
|
this.platform = platform;
|
|
36
|
+
this.keysValidation = new TemporalMemoryManager_1.TemporalMemoryManager(validationCacheDuration);
|
|
37
|
+
this.tokenValidation = new TemporalMemoryManager_1.TemporalMemoryManager(validationCacheDuration);
|
|
32
38
|
}
|
|
33
39
|
async requestHeaders() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
else
|
|
46
|
-
console.warn(`[${this.application}/${this.service}] Failed to obtain or validate keys (attempt ${attempts + 1}/${MAX_RETRIES})`);
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
console.error(`[${this.application}/${this.service}] Unexpected error in requestHeaders (attempt ${attempts + 1}/${MAX_RETRIES}):`, error);
|
|
50
|
-
}
|
|
51
|
-
attempts++;
|
|
52
|
-
await this.exponentialBackoff(attempts);
|
|
40
|
+
const headers = await this.withRetry((attempt) => this.resolveHeaders(attempt));
|
|
41
|
+
if (!headers)
|
|
42
|
+
console.error(`[${this.application}/${this.service}] Failed to obtain auth headers after ${MAX_RETRIES} attempts`);
|
|
43
|
+
return headers ?? {};
|
|
44
|
+
}
|
|
45
|
+
async resolveHeaders(attempt) {
|
|
46
|
+
const keysReady = await this.ensureKeys();
|
|
47
|
+
if (!keysReady) {
|
|
48
|
+
console.warn(`[${this.application}/${this.service}] Failed to obtain or validate keys (attempt ${attempt}/${MAX_RETRIES})`);
|
|
49
|
+
return undefined;
|
|
53
50
|
}
|
|
54
|
-
|
|
55
|
-
return
|
|
51
|
+
const existing = await this.tryUseExistingToken();
|
|
52
|
+
return existing ?? this.requestNewToken(attempt);
|
|
53
|
+
}
|
|
54
|
+
async withRetry(operation) {
|
|
55
|
+
let result;
|
|
56
|
+
for (let attempt = 1; attempt <= MAX_RETRIES && result === undefined; attempt++) {
|
|
57
|
+
result = await operation(attempt);
|
|
58
|
+
if (result === undefined)
|
|
59
|
+
await this.exponentialBackoff(attempt);
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
async ensureKeys() {
|
|
64
|
+
this.keys = this.keys ?? await this.requestKeys();
|
|
65
|
+
if (this.keys && await this.isCurrentKeysValid())
|
|
66
|
+
return true;
|
|
67
|
+
this.keys = await this.requestKeys();
|
|
68
|
+
return this.keys !== undefined;
|
|
56
69
|
}
|
|
57
|
-
async
|
|
70
|
+
async isCurrentKeysValid() {
|
|
71
|
+
if (!this.keys)
|
|
72
|
+
return false;
|
|
73
|
+
const key = `${this.application}:${this.service}:${this.keys.publicKey}`;
|
|
74
|
+
const valid = await this.cachedValidation(this.keysValidation, key, "Key validation", () => this.validateCredentials());
|
|
75
|
+
return valid === true;
|
|
76
|
+
}
|
|
77
|
+
async tryUseExistingToken() {
|
|
78
|
+
if (!this.authToken)
|
|
79
|
+
return undefined;
|
|
80
|
+
const key = `${this.application}:${this.service}:${this.authToken}`;
|
|
81
|
+
const valid = await this.cachedValidation(this.tokenValidation, key, "Token validation", () => this.validateToken());
|
|
82
|
+
if (valid === true)
|
|
83
|
+
return this.buildServiceHeaders();
|
|
84
|
+
this.authToken = undefined;
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
async cachedValidation(cache, key, description, validate) {
|
|
88
|
+
const cached = cache.getValue(key);
|
|
89
|
+
if (cached !== undefined)
|
|
90
|
+
return cached;
|
|
58
91
|
try {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
path: `/service/validate/token`,
|
|
64
|
-
}, this.keys)}`
|
|
65
|
-
}));
|
|
66
|
-
if (response.success && response.payload) {
|
|
67
|
-
console.debug(`[${this.application}/${this.service}] Existing token is still valid`);
|
|
68
|
-
return {
|
|
69
|
-
headers: {
|
|
70
|
-
Referer: this.encryptPayload(this.serviceReferer),
|
|
71
|
-
Authorization: `Bearer ${this.authToken}`,
|
|
72
|
-
Platform: this.platform,
|
|
73
|
-
Application: this.application,
|
|
74
|
-
Service: this.service,
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
}
|
|
92
|
+
const value = await validate();
|
|
93
|
+
if (value !== undefined)
|
|
94
|
+
cache.saveValue(key, value);
|
|
95
|
+
return value;
|
|
79
96
|
}
|
|
80
97
|
catch (error) {
|
|
81
98
|
if (this.isUnauthorizedError(error)) {
|
|
82
|
-
|
|
83
|
-
this.
|
|
99
|
+
cache.remove(key);
|
|
100
|
+
console.warn(`[${this.application}/${this.service}] ${description} returned 401, re-authenticating`);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
console.error(`[${this.application}/${this.service}] ${description} failed:`, error);
|
|
84
104
|
}
|
|
105
|
+
return undefined;
|
|
85
106
|
}
|
|
86
|
-
return undefined;
|
|
87
107
|
}
|
|
88
|
-
async
|
|
108
|
+
async validateCredentials() {
|
|
109
|
+
const response = await RequestAxiosCall_1.RequestAxiosCall.post(`${this.sessionServiceUrl}/service/validate/credentials`, { payload: this.encryptPayload(`${this.application}:${this.service}:${this.keys?.publicKey}`) }, this.buildHeaders());
|
|
110
|
+
return Boolean(response.success && response.payload);
|
|
111
|
+
}
|
|
112
|
+
async validateToken() {
|
|
113
|
+
const response = await RequestAxiosCall_1.RequestAxiosCall.post(`${this.sessionServiceUrl}/service/validate/token`, { payload: this.encryptPayload(`${this.application}:${this.service}:${this.authToken}`) }, this.buildHeaders({
|
|
114
|
+
authorization: `${this.application} ${this.apiKeySigner.sign({
|
|
115
|
+
method: APIRequest_1.RequestMethod.POST,
|
|
116
|
+
path: `/service/validate/token`,
|
|
117
|
+
}, this.keys)}`
|
|
118
|
+
}));
|
|
119
|
+
return Boolean(response.success && response.payload);
|
|
120
|
+
}
|
|
121
|
+
async requestNewToken(attempt) {
|
|
122
|
+
if (!this.keys)
|
|
123
|
+
return undefined;
|
|
89
124
|
try {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
headers: {
|
|
103
|
-
Referer: this.encryptPayload(this.serviceReferer),
|
|
104
|
-
Authorization: `Bearer ${response.payload}`,
|
|
105
|
-
Platform: this.platform,
|
|
106
|
-
Application: this.application,
|
|
107
|
-
Service: this.service,
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
console.warn(`[${this.application}/${this.service}] Token request returned unsuccessful response (attempt ${attemptNumber + 1}/${MAX_RETRIES})`);
|
|
125
|
+
const tokenUrl = `${this.sessionServiceUrl}/service/token?role=${encodeURIComponent(this.role)}&application=${encodeURIComponent(this.application)}&platform=${encodeURIComponent(this.platform)}&service=${encodeURIComponent(this.service)}`;
|
|
126
|
+
const response = await RequestAxiosCall_1.RequestAxiosCall.get(tokenUrl, this.buildHeaders({
|
|
127
|
+
authorization: `${this.application} ${this.apiKeySigner.sign({
|
|
128
|
+
method: APIRequest_1.RequestMethod.GET,
|
|
129
|
+
path: `/service/token?role=${encodeURIComponent(this.role)}&application=${encodeURIComponent(this.application)}&platform=${encodeURIComponent(this.platform)}&service=${encodeURIComponent(this.service)}`,
|
|
130
|
+
}, this.keys)}`
|
|
131
|
+
}));
|
|
132
|
+
if (response.success && response.payload) {
|
|
133
|
+
this.authToken = response.payload;
|
|
134
|
+
this.tokenValidation.saveValue(`${this.application}:${this.service}:${this.authToken}`, true);
|
|
135
|
+
console.debug(`[${this.application}/${this.service}] Successfully obtained new auth token`);
|
|
136
|
+
return this.buildServiceHeaders();
|
|
112
137
|
}
|
|
138
|
+
console.warn(`[${this.application}/${this.service}] Token request returned unsuccessful response (attempt ${attempt}/${MAX_RETRIES})`);
|
|
113
139
|
}
|
|
114
140
|
catch (error) {
|
|
115
141
|
if (this.isUnauthorizedError(error)) {
|
|
116
|
-
console.warn(`[${this.application}/${this.service}] Token request returned 401, refreshing credentials (attempt ${
|
|
142
|
+
console.warn(`[${this.application}/${this.service}] Token request returned 401, refreshing credentials (attempt ${attempt}/${MAX_RETRIES})`);
|
|
117
143
|
await this.handleTokenExpiration();
|
|
118
144
|
}
|
|
119
145
|
else
|
|
120
|
-
console.error(`[${this.application}/${this.service}] Error during token request (attempt ${
|
|
146
|
+
console.error(`[${this.application}/${this.service}] Error during token request (attempt ${attempt}/${MAX_RETRIES}):`, error);
|
|
121
147
|
}
|
|
122
148
|
return undefined;
|
|
123
149
|
}
|
|
124
|
-
async checkKeys() {
|
|
125
|
-
this.keys = this.keys ?? await this.requestKeys();
|
|
126
|
-
if (!(await this.isCurrentKeysValid()))
|
|
127
|
-
this.keys = await this.requestKeys();
|
|
128
|
-
return this.keys !== undefined;
|
|
129
|
-
}
|
|
130
150
|
async requestKeys() {
|
|
131
151
|
try {
|
|
132
152
|
const response = await RequestAxiosCall_1.RequestAxiosCall.get(`${this.sessionServiceUrl}/service/credentials?service=${encodeURIComponent(this.service)}&application=${encodeURIComponent(this.application)}`, this.buildHeaders());
|
|
@@ -145,30 +165,12 @@ class ServiceSessionManager {
|
|
|
145
165
|
}
|
|
146
166
|
return undefined;
|
|
147
167
|
}
|
|
148
|
-
async isCurrentKeysValid() {
|
|
149
|
-
if (this.keys) {
|
|
150
|
-
try {
|
|
151
|
-
const response = await RequestAxiosCall_1.RequestAxiosCall.post(`${this.sessionServiceUrl}/service/validate/credentials`, {
|
|
152
|
-
payload: this.encryptPayload(`${this.application}:${this.service}:${this.keys?.publicKey}`),
|
|
153
|
-
}, this.buildHeaders());
|
|
154
|
-
return response.success && response.payload;
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
if (this.isUnauthorizedError(error)) {
|
|
158
|
-
console.warn(`[${this.application}/${this.service}] Key validation returned 401, keys are expired or invalid`);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
console.error(`[${this.application}/${this.service}] Key validation failed, will attempt to refetch:`, error);
|
|
162
|
-
}
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
168
|
async handleTokenExpiration() {
|
|
169
169
|
console.info(`[${this.application}/${this.service}] Clearing expired credentials and keys`);
|
|
170
170
|
this.authToken = undefined;
|
|
171
171
|
this.keys = undefined;
|
|
172
|
+
this.keysValidation.cleanAll();
|
|
173
|
+
this.tokenValidation.cleanAll();
|
|
172
174
|
}
|
|
173
175
|
async exponentialBackoff(attemptNumber) {
|
|
174
176
|
if (attemptNumber < MAX_RETRIES) {
|
|
@@ -196,6 +198,14 @@ class ServiceSessionManager {
|
|
|
196
198
|
}
|
|
197
199
|
return { headers };
|
|
198
200
|
}
|
|
201
|
+
buildServiceHeaders() {
|
|
202
|
+
return this.buildHeaders({
|
|
203
|
+
authorization: `Bearer ${this.authToken}`,
|
|
204
|
+
platform: this.platform,
|
|
205
|
+
application: this.application,
|
|
206
|
+
service: this.service,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
199
209
|
encryptPayload(payload) {
|
|
200
210
|
return this.cipheringAlgorithm.encrypt(payload, this.serviceSecretKey);
|
|
201
211
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceSessionManager.js","sourceRoot":"","sources":["../../../../../src/api/request/crud/common/ServiceSessionManager.ts"],"names":[],"mappings":";;;AACA,oGAAiG;AACjG,6EAA0E;AAE1E,6DAA0D;AAE1D,oDAAoD;AAEpD,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,cAAc,GAAG,GAAG,CAAC,CAAC,qCAAqC;
|
|
1
|
+
{"version":3,"file":"ServiceSessionManager.js","sourceRoot":"","sources":["../../../../../src/api/request/crud/common/ServiceSessionManager.ts"],"names":[],"mappings":";;;AACA,uEAAoE;AACpE,oGAAiG;AACjG,6EAA0E;AAE1E,6DAA0D;AAE1D,oDAAoD;AAEpD,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,cAAc,GAAG,GAAG,CAAC,CAAC,qCAAqC;AACjE,MAAM,2BAA2B,GAAG,EAAE,GAAG,IAAI,CAAC;AAE9C,MAAa,qBAAqB;IACf,kBAAkB,CAAwB;IAC1C,YAAY,CAAe;IAC3B,cAAc,CAAS;IACvB,gBAAgB,CAAS;IACzB,iBAAiB,CAAS;IAC1B,OAAO,CAAS;IAChB,IAAI,CAAS;IACb,WAAW,CAAS;IACpB,QAAQ,CAAS;I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AAe;QACnC,OAAO,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC;IACzE,CAAC;CACF;AAtQD,sDAsQC"}
|
|
@@ -1,34 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PasswordUtils = void 0;
|
|
4
|
-
const
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const SALT_BYTES = 16;
|
|
6
|
+
const KEY_BYTES = 64;
|
|
7
|
+
const HASH_ALGORITHM = "sha256";
|
|
8
|
+
const DEFAULT_ITERATIONS = 600_000;
|
|
5
9
|
class PasswordUtils {
|
|
6
10
|
iterations;
|
|
7
|
-
constructor(iterations =
|
|
11
|
+
constructor(iterations = DEFAULT_ITERATIONS) {
|
|
8
12
|
this.iterations = iterations;
|
|
9
13
|
}
|
|
10
14
|
async hashPassword(password) {
|
|
11
|
-
const salt =
|
|
12
|
-
const hash =
|
|
13
|
-
|
|
14
|
-
iterations: this.iterations,
|
|
15
|
-
hasher: crypto_js_1.algo.SHA256,
|
|
16
|
-
});
|
|
17
|
-
return `${crypto_js_1.enc.Hex.stringify(salt)}:${crypto_js_1.enc.Hex.stringify(hash)}`;
|
|
15
|
+
const salt = (0, node_crypto_1.randomBytes)(SALT_BYTES);
|
|
16
|
+
const hash = await this.derive(password, salt);
|
|
17
|
+
return `${salt.toString("hex")}:${hash.toString("hex")}`;
|
|
18
18
|
}
|
|
19
19
|
async comparePasswords(password, storedHash) {
|
|
20
|
-
const [saltHex, originalHashHex] = storedHash.split(
|
|
20
|
+
const [saltHex, originalHashHex] = storedHash.split(":");
|
|
21
21
|
if (!saltHex || !originalHashHex) {
|
|
22
22
|
throw new Error("Invalid stored hash format. Expected 'salt:hash'.");
|
|
23
23
|
}
|
|
24
|
-
const salt =
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
const salt = Buffer.from(saltHex, "hex");
|
|
25
|
+
const expected = Buffer.from(originalHashHex, "hex");
|
|
26
|
+
if (salt.length !== SALT_BYTES || expected.length !== KEY_BYTES)
|
|
27
|
+
return false;
|
|
28
|
+
const actual = await this.derive(password, salt);
|
|
29
|
+
return (0, node_crypto_1.timingSafeEqual)(actual, expected);
|
|
30
|
+
}
|
|
31
|
+
derive(password, salt) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
(0, node_crypto_1.pbkdf2)(password, salt, this.iterations, KEY_BYTES, HASH_ALGORITHM, (error, key) => error ? reject(error) : resolve(key));
|
|
29
34
|
});
|
|
30
|
-
const inputPasswordHashHex = crypto_js_1.enc.Hex.stringify(hash);
|
|
31
|
-
return inputPasswordHashHex === originalHashHex;
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
exports.PasswordUtils = PasswordUtils;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PasswordUtils.js","sourceRoot":"","sources":["../../../src/cryptography/password/PasswordUtils.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"PasswordUtils.js","sourceRoot":"","sources":["../../../src/cryptography/password/PasswordUtils.ts"],"names":[],"mappings":";;;AAAA,6CAAmE;AAEnE,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,cAAc,GAAG,QAAQ,CAAC;AAChC,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAEnC,MAAa,aAAa;IACP,UAAU,CAAS;IAEpC,YAAY,aAAqB,kBAAkB;QACjD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,QAAgB;QACxC,MAAM,IAAI,GAAG,IAAA,yBAAW,EAAC,UAAU,CAAC,CAAC;QACrC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,I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package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itrm-tools",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@types/mocha": "^10.0.10",
|
|
47
47
|
"@types/morgan": "^1.9.10",
|
|
48
48
|
"@types/multer": "^2.2.0",
|
|
49
|
-
"@types/node": "^26.6.
|
|
49
|
+
"@types/node": "^26.6.6",
|
|
50
50
|
"@types/swagger-ui-express": "^4.1.8",
|
|
51
51
|
"@types/uuid": "^10.0.0",
|
|
52
52
|
"@types/ws": "^8.18.1",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"jest-puppeteer": "^11.0.0",
|
|
56
56
|
"mongodb-memory-server": "^11.3.0",
|
|
57
57
|
"puppeteer": "^25.12.0",
|
|
58
|
-
"redis": "^6.1
|
|
58
|
+
"redis": "^6.3.1",
|
|
59
59
|
"rimraf": "^6.1.3",
|
|
60
60
|
"sqlite3": "^6.0.1",
|
|
61
61
|
"ts-jest": "^29.4.13",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"dotenv": "^17.3.1",
|
|
75
75
|
"express": "^5.2.1",
|
|
76
76
|
"https": "^1.0.0",
|
|
77
|
-
"jose": "^6.2.
|
|
77
|
+
"jose": "^6.2.15",
|
|
78
78
|
"jsrsasign": "^11.1.3",
|
|
79
79
|
"mongoose": "^9.10.2",
|
|
80
80
|
"morgan": "^1.11.0",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"sequelize": "^6.37.8",
|
|
85
85
|
"swagger-ui-express": "^5.0.1",
|
|
86
86
|
"uuid": "^14.0.1",
|
|
87
|
-
"ws": "^8.
|
|
87
|
+
"ws": "^8.22.1"
|
|
88
88
|
},
|
|
89
89
|
"overrides": {
|
|
90
90
|
"tar": "^7.5.21",
|