itrm-tools 1.4.6 → 1.4.8
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/sign/ApiKeySigner.d.ts +3 -0
- package/dist/cryptography/sign/ApiKeySigner.js +19 -8
- package/dist/cryptography/sign/ApiKeySigner.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,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G,CAAC,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,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CAAC,OAAe;QACnC,OAAO,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC;IACzE,CAAC;CACF;AAtQD,sDAsQC"}
|
|
@@ -8,6 +8,9 @@ export interface ApiSignValues {
|
|
|
8
8
|
encryptedBody?: string;
|
|
9
9
|
}
|
|
10
10
|
export declare class ApiKeySigner implements Signer<ApiSignValues, IKey, string> {
|
|
11
|
+
private readonly windowMilliseconds;
|
|
12
|
+
/** `windowSeconds` decides how old a nonce may be; without it, five minutes. */
|
|
13
|
+
constructor(windowSeconds?: number);
|
|
11
14
|
sign(input: ApiSignValues, keys: IKey): string;
|
|
12
15
|
verify(input: ApiSignValues, keys: IKey, signature: string): boolean;
|
|
13
16
|
private buildSignature;
|
|
@@ -2,21 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ApiKeySigner = void 0;
|
|
4
4
|
const crypto_js_1 = require("crypto-js");
|
|
5
|
+
/** Five minutes: enough for the clock skew between stations, short enough to bound a replay. */
|
|
6
|
+
const DEFAULT_NONCE_WINDOW_SECONDS = 300;
|
|
5
7
|
class ApiKeySigner {
|
|
8
|
+
windowMilliseconds;
|
|
9
|
+
/** `windowSeconds` decides how old a nonce may be; without it, five minutes. */
|
|
10
|
+
constructor(windowSeconds) {
|
|
11
|
+
const usable = windowSeconds !== undefined && Number.isFinite(windowSeconds) && windowSeconds > 0;
|
|
12
|
+
this.windowMilliseconds = (usable ? windowSeconds : DEFAULT_NONCE_WINDOW_SECONDS) * 1000;
|
|
13
|
+
}
|
|
6
14
|
sign(input, keys) {
|
|
15
|
+
// Milliseconds, as always: two stations may sign in the same millisecond and the verifier
|
|
16
|
+
// accepts both, because it no longer requires the nonce to grow.
|
|
7
17
|
let nonce = Date.now();
|
|
8
18
|
return `${keys.publicKey}:${nonce}:${this.buildSignature(input, nonce, keys.signKey)}`;
|
|
9
19
|
}
|
|
10
20
|
verify(input, keys, signature) {
|
|
11
|
-
|
|
12
|
-
if (values
|
|
13
|
-
|
|
21
|
+
const values = this.extractSignatureDetails(signature);
|
|
22
|
+
if (values !== undefined) {
|
|
23
|
+
// Inside the window and with a matching signature; the nonce need not grow, so two
|
|
24
|
+
// stations share a key without a race, and outside the window a captured request is old.
|
|
25
|
+
const age = Date.now() - values.nonce;
|
|
26
|
+
const matches = Math.abs(age) <= this.windowMilliseconds
|
|
27
|
+
&& this.buildSignature(input, values.nonce, keys.signKey) === values.signature;
|
|
14
28
|
if (matches) {
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
if (keys.lastNonce < values.nonce) {
|
|
18
|
-
keys.lastNonce = values.nonce;
|
|
19
|
-
}
|
|
29
|
+
// `lastNonce` is kept for the records, never to reject a request.
|
|
30
|
+
keys.lastNonce = Math.max(keys.lastNonce ?? 0, values.nonce);
|
|
20
31
|
return true;
|
|
21
32
|
}
|
|
22
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiKeySigner.js","sourceRoot":"","sources":["../../../src/cryptography/sign/ApiKeySigner.ts"],"names":[],"mappings":";;;AAEA,yCAA4C;AAU5C,MAAa,YAAY;
|
|
1
|
+
{"version":3,"file":"ApiKeySigner.js","sourceRoot":"","sources":["../../../src/cryptography/sign/ApiKeySigner.ts"],"names":[],"mappings":";;;AAEA,yCAA4C;AAU5C,gGAAgG;AAChG,MAAM,4BAA4B,GAAG,GAAG,CAAC;AAEzC,MAAa,YAAY;IACJ,kBAAkB,CAAS;IAE5C,gFAAgF;IAChF,YAAY,aAAsB;QAC9B,MAAM,MAAM,GAAG,aAAa,KAAK,SAAS,IAAI,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,aAAa,GAAG,CAAC,CAAC;QAElG,IAAI,CAAC,kBAAkB,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,4BAA4B,CAAC,GAAG,IAAI,CAAC;IAC7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IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACxH,MAAM,IAAI,GAAG,IAAA,sBAAU,EAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1C,OAAO,eAAG,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAEO,uBAAuB,CAAC,SAAiB;QAC7C,IAAI,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAClB,OAAO;gBACH,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;gBACnB,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACzB,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;aACtB,CAAC;QACN,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ;AArDD,oCAqDC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itrm-tools",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
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",
|