b23-lib 1.2.9 → 1.2.11
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/index.d.mts +41 -21
- package/dist/index.d.ts +41 -21
- package/dist/index.js +53 -47
- package/dist/index.mjs +53 -47
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -236,25 +236,28 @@ declare const Schema: {
|
|
|
236
236
|
};
|
|
237
237
|
|
|
238
238
|
type StringifiedJSONArray = string;
|
|
239
|
-
|
|
240
|
-
maxTokenAge
|
|
241
|
-
userPrivateKeys
|
|
242
|
-
userPublicKeys
|
|
243
|
-
anonymousPrivateKeys
|
|
244
|
-
anonymousPublicKeys
|
|
245
|
-
systemPrivateKeys
|
|
246
|
-
systemPublicKeys
|
|
247
|
-
adminPrivateKeys
|
|
248
|
-
adminPublicKeys
|
|
249
|
-
}
|
|
250
|
-
declare const DefaultAuthUtilityConfig: AuthUtilityConfig
|
|
239
|
+
interface AuthUtilityConfig {
|
|
240
|
+
maxTokenAge: string;
|
|
241
|
+
userPrivateKeys: StringifiedJSONArray;
|
|
242
|
+
userPublicKeys: StringifiedJSONArray;
|
|
243
|
+
anonymousPrivateKeys: StringifiedJSONArray;
|
|
244
|
+
anonymousPublicKeys: StringifiedJSONArray;
|
|
245
|
+
systemPrivateKeys: StringifiedJSONArray;
|
|
246
|
+
systemPublicKeys: StringifiedJSONArray;
|
|
247
|
+
adminPrivateKeys: StringifiedJSONArray;
|
|
248
|
+
adminPublicKeys: StringifiedJSONArray;
|
|
249
|
+
}
|
|
250
|
+
declare const DefaultAuthUtilityConfig: Readonly<AuthUtilityConfig>;
|
|
251
251
|
type AuthTokenType = 'Anon' | 'User' | 'System' | 'Admin';
|
|
252
|
-
|
|
253
|
-
allowAnonymous
|
|
254
|
-
allowSystem
|
|
255
|
-
allowUser
|
|
256
|
-
}
|
|
257
|
-
declare const DefaultAuthMiddlewareConfig: AuthMiddlewareConfig
|
|
252
|
+
interface AuthMiddlewareConfig {
|
|
253
|
+
allowAnonymous: boolean;
|
|
254
|
+
allowSystem: boolean;
|
|
255
|
+
allowUser: boolean;
|
|
256
|
+
}
|
|
257
|
+
declare const DefaultAuthMiddlewareConfig: Readonly<AuthMiddlewareConfig>;
|
|
258
|
+
/**
|
|
259
|
+
* A utility class for JWT authentication and authorization.
|
|
260
|
+
*/
|
|
258
261
|
declare class AuthUtility {
|
|
259
262
|
private maxTokenAge;
|
|
260
263
|
private userPrivateKeys;
|
|
@@ -265,7 +268,15 @@ declare class AuthUtility {
|
|
|
265
268
|
private systemPublicKeys;
|
|
266
269
|
private adminPrivateKeys;
|
|
267
270
|
private adminPublicKeys;
|
|
268
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Initializes the AuthUtility class with a configuration.
|
|
273
|
+
* @param config The configuration for the utility (optional).
|
|
274
|
+
*/
|
|
275
|
+
constructor(config?: Partial<AuthUtilityConfig>);
|
|
276
|
+
/**
|
|
277
|
+
* Logs warnings if the number of keys exceeds recommended limits.
|
|
278
|
+
*/
|
|
279
|
+
private logWarnings;
|
|
269
280
|
private createSignedJWT;
|
|
270
281
|
private verifySignedJWT;
|
|
271
282
|
createAnonymousToken(id: string, additionalData?: object): Promise<string>;
|
|
@@ -276,7 +287,11 @@ declare class AuthUtility {
|
|
|
276
287
|
verifySystemToken(token: string): Promise<jose.JWTPayload>;
|
|
277
288
|
createAdminToken(id: string, additionalData?: object): Promise<string>;
|
|
278
289
|
verifyAdminToken(token: string): Promise<jose.JWTPayload>;
|
|
279
|
-
|
|
290
|
+
/**
|
|
291
|
+
* Middleware for handling JWT authentication.
|
|
292
|
+
* @param config Configuration for middleware behavior.
|
|
293
|
+
*/
|
|
294
|
+
AuthMiddleware(config?: Partial<AuthMiddlewareConfig>): (req: any, res: any, next: any) => Promise<void>;
|
|
280
295
|
}
|
|
281
296
|
|
|
282
297
|
declare const Utils: {
|
|
@@ -304,6 +319,11 @@ declare const ResponseUtility: {
|
|
|
304
319
|
};
|
|
305
320
|
};
|
|
306
321
|
|
|
322
|
+
type ErrorType = {
|
|
323
|
+
status: number;
|
|
324
|
+
statusText: string;
|
|
325
|
+
error: any;
|
|
326
|
+
};
|
|
307
327
|
type SuccessType = {
|
|
308
328
|
status: number;
|
|
309
329
|
statusText: string;
|
|
@@ -319,4 +339,4 @@ declare const Logger: {
|
|
|
319
339
|
logInvalidPayload: (functionName: string, errorMessage: string) => void;
|
|
320
340
|
};
|
|
321
341
|
|
|
322
|
-
export { type AuthMiddlewareConfig, type AuthTokenType, AuthUtility, type AuthUtilityConfig, DefaultAuthMiddlewareConfig, DefaultAuthUtilityConfig, DynamoDBUtility as DynamoDB, Fetch, Logger, ResponseUtility, Schema, Utils };
|
|
342
|
+
export { type AuthMiddlewareConfig, type AuthTokenType, AuthUtility, type AuthUtilityConfig, DefaultAuthMiddlewareConfig, DefaultAuthUtilityConfig, DynamoDBUtility as DynamoDB, type ErrorType, Fetch, Logger, ResponseUtility, Schema, type SuccessType, Utils };
|
package/dist/index.d.ts
CHANGED
|
@@ -236,25 +236,28 @@ declare const Schema: {
|
|
|
236
236
|
};
|
|
237
237
|
|
|
238
238
|
type StringifiedJSONArray = string;
|
|
239
|
-
|
|
240
|
-
maxTokenAge
|
|
241
|
-
userPrivateKeys
|
|
242
|
-
userPublicKeys
|
|
243
|
-
anonymousPrivateKeys
|
|
244
|
-
anonymousPublicKeys
|
|
245
|
-
systemPrivateKeys
|
|
246
|
-
systemPublicKeys
|
|
247
|
-
adminPrivateKeys
|
|
248
|
-
adminPublicKeys
|
|
249
|
-
}
|
|
250
|
-
declare const DefaultAuthUtilityConfig: AuthUtilityConfig
|
|
239
|
+
interface AuthUtilityConfig {
|
|
240
|
+
maxTokenAge: string;
|
|
241
|
+
userPrivateKeys: StringifiedJSONArray;
|
|
242
|
+
userPublicKeys: StringifiedJSONArray;
|
|
243
|
+
anonymousPrivateKeys: StringifiedJSONArray;
|
|
244
|
+
anonymousPublicKeys: StringifiedJSONArray;
|
|
245
|
+
systemPrivateKeys: StringifiedJSONArray;
|
|
246
|
+
systemPublicKeys: StringifiedJSONArray;
|
|
247
|
+
adminPrivateKeys: StringifiedJSONArray;
|
|
248
|
+
adminPublicKeys: StringifiedJSONArray;
|
|
249
|
+
}
|
|
250
|
+
declare const DefaultAuthUtilityConfig: Readonly<AuthUtilityConfig>;
|
|
251
251
|
type AuthTokenType = 'Anon' | 'User' | 'System' | 'Admin';
|
|
252
|
-
|
|
253
|
-
allowAnonymous
|
|
254
|
-
allowSystem
|
|
255
|
-
allowUser
|
|
256
|
-
}
|
|
257
|
-
declare const DefaultAuthMiddlewareConfig: AuthMiddlewareConfig
|
|
252
|
+
interface AuthMiddlewareConfig {
|
|
253
|
+
allowAnonymous: boolean;
|
|
254
|
+
allowSystem: boolean;
|
|
255
|
+
allowUser: boolean;
|
|
256
|
+
}
|
|
257
|
+
declare const DefaultAuthMiddlewareConfig: Readonly<AuthMiddlewareConfig>;
|
|
258
|
+
/**
|
|
259
|
+
* A utility class for JWT authentication and authorization.
|
|
260
|
+
*/
|
|
258
261
|
declare class AuthUtility {
|
|
259
262
|
private maxTokenAge;
|
|
260
263
|
private userPrivateKeys;
|
|
@@ -265,7 +268,15 @@ declare class AuthUtility {
|
|
|
265
268
|
private systemPublicKeys;
|
|
266
269
|
private adminPrivateKeys;
|
|
267
270
|
private adminPublicKeys;
|
|
268
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Initializes the AuthUtility class with a configuration.
|
|
273
|
+
* @param config The configuration for the utility (optional).
|
|
274
|
+
*/
|
|
275
|
+
constructor(config?: Partial<AuthUtilityConfig>);
|
|
276
|
+
/**
|
|
277
|
+
* Logs warnings if the number of keys exceeds recommended limits.
|
|
278
|
+
*/
|
|
279
|
+
private logWarnings;
|
|
269
280
|
private createSignedJWT;
|
|
270
281
|
private verifySignedJWT;
|
|
271
282
|
createAnonymousToken(id: string, additionalData?: object): Promise<string>;
|
|
@@ -276,7 +287,11 @@ declare class AuthUtility {
|
|
|
276
287
|
verifySystemToken(token: string): Promise<jose.JWTPayload>;
|
|
277
288
|
createAdminToken(id: string, additionalData?: object): Promise<string>;
|
|
278
289
|
verifyAdminToken(token: string): Promise<jose.JWTPayload>;
|
|
279
|
-
|
|
290
|
+
/**
|
|
291
|
+
* Middleware for handling JWT authentication.
|
|
292
|
+
* @param config Configuration for middleware behavior.
|
|
293
|
+
*/
|
|
294
|
+
AuthMiddleware(config?: Partial<AuthMiddlewareConfig>): (req: any, res: any, next: any) => Promise<void>;
|
|
280
295
|
}
|
|
281
296
|
|
|
282
297
|
declare const Utils: {
|
|
@@ -304,6 +319,11 @@ declare const ResponseUtility: {
|
|
|
304
319
|
};
|
|
305
320
|
};
|
|
306
321
|
|
|
322
|
+
type ErrorType = {
|
|
323
|
+
status: number;
|
|
324
|
+
statusText: string;
|
|
325
|
+
error: any;
|
|
326
|
+
};
|
|
307
327
|
type SuccessType = {
|
|
308
328
|
status: number;
|
|
309
329
|
statusText: string;
|
|
@@ -319,4 +339,4 @@ declare const Logger: {
|
|
|
319
339
|
logInvalidPayload: (functionName: string, errorMessage: string) => void;
|
|
320
340
|
};
|
|
321
341
|
|
|
322
|
-
export { type AuthMiddlewareConfig, type AuthTokenType, AuthUtility, type AuthUtilityConfig, DefaultAuthMiddlewareConfig, DefaultAuthUtilityConfig, DynamoDBUtility as DynamoDB, Fetch, Logger, ResponseUtility, Schema, Utils };
|
|
342
|
+
export { type AuthMiddlewareConfig, type AuthTokenType, AuthUtility, type AuthUtilityConfig, DefaultAuthMiddlewareConfig, DefaultAuthUtilityConfig, DynamoDBUtility as DynamoDB, type ErrorType, Fetch, Logger, ResponseUtility, Schema, type SuccessType, Utils };
|
package/dist/index.js
CHANGED
|
@@ -697,40 +697,49 @@ var AuthUtility = class {
|
|
|
697
697
|
systemPublicKeys;
|
|
698
698
|
adminPrivateKeys;
|
|
699
699
|
adminPublicKeys;
|
|
700
|
-
|
|
700
|
+
/**
|
|
701
|
+
* Initializes the AuthUtility class with a configuration.
|
|
702
|
+
* @param config The configuration for the utility (optional).
|
|
703
|
+
*/
|
|
704
|
+
constructor(config = DefaultAuthUtilityConfig) {
|
|
705
|
+
const {
|
|
706
|
+
maxTokenAge,
|
|
707
|
+
userPrivateKeys,
|
|
708
|
+
userPublicKeys,
|
|
709
|
+
anonymousPrivateKeys,
|
|
710
|
+
anonymousPublicKeys,
|
|
711
|
+
systemPrivateKeys,
|
|
712
|
+
systemPublicKeys,
|
|
713
|
+
adminPrivateKeys,
|
|
714
|
+
adminPublicKeys
|
|
715
|
+
} = { ...DefaultAuthUtilityConfig, ...config };
|
|
701
716
|
this.maxTokenAge = maxTokenAge;
|
|
702
717
|
this.userPrivateKeys = JSON.parse(userPrivateKeys);
|
|
703
|
-
if (this.userPrivateKeys.length > 3) {
|
|
704
|
-
Logger_default.logWarning("AuthUtility", "More than 1 user private key provided. The last key will be used for signing.");
|
|
705
|
-
}
|
|
706
718
|
this.userPublicKeys = JSON.parse(userPublicKeys);
|
|
707
|
-
if (this.userPublicKeys.length > 3) {
|
|
708
|
-
Logger_default.logWarning("AuthUtility", "More than 3 user public keys provided. This is not recommended.");
|
|
709
|
-
}
|
|
710
719
|
this.anonymousPrivateKeys = JSON.parse(anonymousPrivateKeys);
|
|
711
|
-
if (this.anonymousPrivateKeys.length > 1) {
|
|
712
|
-
Logger_default.logWarning("AuthUtility", "More than 1 anonymous private key provided. The last key will be used for signing.");
|
|
713
|
-
}
|
|
714
720
|
this.anonymousPublicKeys = JSON.parse(anonymousPublicKeys);
|
|
715
|
-
if (this.anonymousPublicKeys.length > 3) {
|
|
716
|
-
Logger_default.logWarning("AuthUtility", "More than 3 anonymous public keys provided. This is not recommended.");
|
|
717
|
-
}
|
|
718
721
|
this.systemPrivateKeys = JSON.parse(systemPrivateKeys);
|
|
719
|
-
if (this.systemPrivateKeys.length > 1) {
|
|
720
|
-
Logger_default.logWarning("AuthUtility", "More than 1 system private key provided. The last key will be used for signing.");
|
|
721
|
-
}
|
|
722
722
|
this.systemPublicKeys = JSON.parse(systemPublicKeys);
|
|
723
|
-
if (this.systemPublicKeys.length > 3) {
|
|
724
|
-
Logger_default.logWarning("AuthUtility", "More than 3 system public keys provided. This is not recommended.");
|
|
725
|
-
}
|
|
726
723
|
this.adminPrivateKeys = JSON.parse(adminPrivateKeys);
|
|
727
|
-
if (this.adminPrivateKeys.length > 1) {
|
|
728
|
-
Logger_default.logWarning("AuthUtility", "More than 1 admin private key provided. The last key will be used for signing.");
|
|
729
|
-
}
|
|
730
724
|
this.adminPublicKeys = JSON.parse(adminPublicKeys);
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
725
|
+
this.logWarnings();
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Logs warnings if the number of keys exceeds recommended limits.
|
|
729
|
+
*/
|
|
730
|
+
logWarnings() {
|
|
731
|
+
const warn = (type, keys, limit) => keys.length > limit && Logger_default.logWarning(
|
|
732
|
+
"AuthUtility",
|
|
733
|
+
`More than ${limit} ${type} keys provided. This is not recommended.`
|
|
734
|
+
);
|
|
735
|
+
warn("user private", this.userPrivateKeys, 3);
|
|
736
|
+
warn("user public", this.userPublicKeys, 3);
|
|
737
|
+
warn("anonymous private", this.anonymousPrivateKeys, 1);
|
|
738
|
+
warn("anonymous public", this.anonymousPublicKeys, 3);
|
|
739
|
+
warn("system private", this.systemPrivateKeys, 1);
|
|
740
|
+
warn("system public", this.systemPublicKeys, 3);
|
|
741
|
+
warn("admin private", this.adminPrivateKeys, 1);
|
|
742
|
+
warn("admin public", this.adminPublicKeys, 3);
|
|
734
743
|
}
|
|
735
744
|
async createSignedJWT(payload, privateKeyString, expiration) {
|
|
736
745
|
const privateKey = await (0, import_jose.importPKCS8)(privateKeyString, "RS256");
|
|
@@ -814,50 +823,47 @@ var AuthUtility = class {
|
|
|
814
823
|
(0, import_assert.default)(payload.type === "Admin", ErrorTypes_default.INVALID_AUTH_TYPE);
|
|
815
824
|
return payload;
|
|
816
825
|
}
|
|
817
|
-
|
|
826
|
+
/**
|
|
827
|
+
* Middleware for handling JWT authentication.
|
|
828
|
+
* @param config Configuration for middleware behavior.
|
|
829
|
+
*/
|
|
830
|
+
AuthMiddleware(config = DefaultAuthMiddlewareConfig) {
|
|
831
|
+
const { allowAnonymous, allowSystem, allowUser } = { ...DefaultAuthMiddlewareConfig, ...config };
|
|
818
832
|
return async (req, res, next) => {
|
|
819
833
|
try {
|
|
820
|
-
const [authType, token] = req.get("Authorization")?.split(" ");
|
|
821
|
-
if (!token)
|
|
822
|
-
throw new Error(ErrorTypes_default.INVALID_TOKEN);
|
|
823
|
-
}
|
|
834
|
+
const [authType, token] = req.get("Authorization")?.split(" ") || [];
|
|
835
|
+
if (!token) throw new Error(ErrorTypes_default.INVALID_TOKEN);
|
|
824
836
|
let payload;
|
|
825
837
|
switch (authType) {
|
|
826
838
|
case "Anon":
|
|
827
|
-
if (!allowAnonymous)
|
|
828
|
-
throw response_default.generateError(403, ErrorTypes_default.ANONYMOUS_SESSION_NOT_ALLOWED);
|
|
829
|
-
}
|
|
839
|
+
if (!allowAnonymous) throw response_default.generateError(403, ErrorTypes_default.ANONYMOUS_SESSION_NOT_ALLOWED);
|
|
830
840
|
payload = await this.verifyAnonymousToken(token);
|
|
831
841
|
break;
|
|
832
842
|
case "User":
|
|
833
|
-
if (!allowUser)
|
|
834
|
-
throw response_default.generateError(403, ErrorTypes_default.USER_SESSION_NOT_ALLOWED);
|
|
835
|
-
}
|
|
843
|
+
if (!allowUser) throw response_default.generateError(403, ErrorTypes_default.USER_SESSION_NOT_ALLOWED);
|
|
836
844
|
payload = await this.verifyUserToken(token);
|
|
837
845
|
break;
|
|
838
846
|
case "System":
|
|
839
|
-
if (!allowSystem)
|
|
840
|
-
throw response_default.generateError(403, ErrorTypes_default.SYSTEM_SESSION_NOT_ALLOWED);
|
|
841
|
-
}
|
|
847
|
+
if (!allowSystem) throw response_default.generateError(403, ErrorTypes_default.SYSTEM_SESSION_NOT_ALLOWED);
|
|
842
848
|
payload = await this.verifySystemToken(token);
|
|
843
|
-
Logger_default.logMessage("AuthMiddleware",
|
|
849
|
+
Logger_default.logMessage("AuthMiddleware", `System Name - ${payload.id}`);
|
|
844
850
|
break;
|
|
845
851
|
case "Admin":
|
|
846
852
|
payload = await this.verifyAdminToken(token);
|
|
847
|
-
Logger_default.logMessage("AuthMiddleware",
|
|
853
|
+
Logger_default.logMessage("AuthMiddleware", `Admin Id - ${payload.id}`);
|
|
848
854
|
break;
|
|
849
855
|
default:
|
|
850
856
|
throw response_default.generateError(403, ErrorTypes_default.INVALID_AUTH_TYPE);
|
|
851
857
|
}
|
|
852
|
-
res.locals.auth = {
|
|
853
|
-
authType,
|
|
854
|
-
token,
|
|
855
|
-
...payload
|
|
856
|
-
};
|
|
858
|
+
res.locals.auth = { authType, token, ...payload };
|
|
857
859
|
next();
|
|
858
860
|
} catch (error) {
|
|
859
861
|
Logger_default.logError("AuthMiddleware", import_util2.default.inspect(error));
|
|
860
|
-
response_default.handleException(
|
|
862
|
+
response_default.handleException(
|
|
863
|
+
"AuthMiddleware",
|
|
864
|
+
response_default.generateError(401, error.error || ErrorTypes_default.TOKEN_EXPIRED, true),
|
|
865
|
+
res
|
|
866
|
+
);
|
|
861
867
|
}
|
|
862
868
|
};
|
|
863
869
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -668,40 +668,49 @@ var AuthUtility = class {
|
|
|
668
668
|
systemPublicKeys;
|
|
669
669
|
adminPrivateKeys;
|
|
670
670
|
adminPublicKeys;
|
|
671
|
-
|
|
671
|
+
/**
|
|
672
|
+
* Initializes the AuthUtility class with a configuration.
|
|
673
|
+
* @param config The configuration for the utility (optional).
|
|
674
|
+
*/
|
|
675
|
+
constructor(config = DefaultAuthUtilityConfig) {
|
|
676
|
+
const {
|
|
677
|
+
maxTokenAge,
|
|
678
|
+
userPrivateKeys,
|
|
679
|
+
userPublicKeys,
|
|
680
|
+
anonymousPrivateKeys,
|
|
681
|
+
anonymousPublicKeys,
|
|
682
|
+
systemPrivateKeys,
|
|
683
|
+
systemPublicKeys,
|
|
684
|
+
adminPrivateKeys,
|
|
685
|
+
adminPublicKeys
|
|
686
|
+
} = { ...DefaultAuthUtilityConfig, ...config };
|
|
672
687
|
this.maxTokenAge = maxTokenAge;
|
|
673
688
|
this.userPrivateKeys = JSON.parse(userPrivateKeys);
|
|
674
|
-
if (this.userPrivateKeys.length > 3) {
|
|
675
|
-
Logger_default.logWarning("AuthUtility", "More than 1 user private key provided. The last key will be used for signing.");
|
|
676
|
-
}
|
|
677
689
|
this.userPublicKeys = JSON.parse(userPublicKeys);
|
|
678
|
-
if (this.userPublicKeys.length > 3) {
|
|
679
|
-
Logger_default.logWarning("AuthUtility", "More than 3 user public keys provided. This is not recommended.");
|
|
680
|
-
}
|
|
681
690
|
this.anonymousPrivateKeys = JSON.parse(anonymousPrivateKeys);
|
|
682
|
-
if (this.anonymousPrivateKeys.length > 1) {
|
|
683
|
-
Logger_default.logWarning("AuthUtility", "More than 1 anonymous private key provided. The last key will be used for signing.");
|
|
684
|
-
}
|
|
685
691
|
this.anonymousPublicKeys = JSON.parse(anonymousPublicKeys);
|
|
686
|
-
if (this.anonymousPublicKeys.length > 3) {
|
|
687
|
-
Logger_default.logWarning("AuthUtility", "More than 3 anonymous public keys provided. This is not recommended.");
|
|
688
|
-
}
|
|
689
692
|
this.systemPrivateKeys = JSON.parse(systemPrivateKeys);
|
|
690
|
-
if (this.systemPrivateKeys.length > 1) {
|
|
691
|
-
Logger_default.logWarning("AuthUtility", "More than 1 system private key provided. The last key will be used for signing.");
|
|
692
|
-
}
|
|
693
693
|
this.systemPublicKeys = JSON.parse(systemPublicKeys);
|
|
694
|
-
if (this.systemPublicKeys.length > 3) {
|
|
695
|
-
Logger_default.logWarning("AuthUtility", "More than 3 system public keys provided. This is not recommended.");
|
|
696
|
-
}
|
|
697
694
|
this.adminPrivateKeys = JSON.parse(adminPrivateKeys);
|
|
698
|
-
if (this.adminPrivateKeys.length > 1) {
|
|
699
|
-
Logger_default.logWarning("AuthUtility", "More than 1 admin private key provided. The last key will be used for signing.");
|
|
700
|
-
}
|
|
701
695
|
this.adminPublicKeys = JSON.parse(adminPublicKeys);
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
696
|
+
this.logWarnings();
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Logs warnings if the number of keys exceeds recommended limits.
|
|
700
|
+
*/
|
|
701
|
+
logWarnings() {
|
|
702
|
+
const warn = (type, keys, limit) => keys.length > limit && Logger_default.logWarning(
|
|
703
|
+
"AuthUtility",
|
|
704
|
+
`More than ${limit} ${type} keys provided. This is not recommended.`
|
|
705
|
+
);
|
|
706
|
+
warn("user private", this.userPrivateKeys, 3);
|
|
707
|
+
warn("user public", this.userPublicKeys, 3);
|
|
708
|
+
warn("anonymous private", this.anonymousPrivateKeys, 1);
|
|
709
|
+
warn("anonymous public", this.anonymousPublicKeys, 3);
|
|
710
|
+
warn("system private", this.systemPrivateKeys, 1);
|
|
711
|
+
warn("system public", this.systemPublicKeys, 3);
|
|
712
|
+
warn("admin private", this.adminPrivateKeys, 1);
|
|
713
|
+
warn("admin public", this.adminPublicKeys, 3);
|
|
705
714
|
}
|
|
706
715
|
async createSignedJWT(payload, privateKeyString, expiration) {
|
|
707
716
|
const privateKey = await importPKCS8(privateKeyString, "RS256");
|
|
@@ -785,50 +794,47 @@ var AuthUtility = class {
|
|
|
785
794
|
assert(payload.type === "Admin", ErrorTypes_default.INVALID_AUTH_TYPE);
|
|
786
795
|
return payload;
|
|
787
796
|
}
|
|
788
|
-
|
|
797
|
+
/**
|
|
798
|
+
* Middleware for handling JWT authentication.
|
|
799
|
+
* @param config Configuration for middleware behavior.
|
|
800
|
+
*/
|
|
801
|
+
AuthMiddleware(config = DefaultAuthMiddlewareConfig) {
|
|
802
|
+
const { allowAnonymous, allowSystem, allowUser } = { ...DefaultAuthMiddlewareConfig, ...config };
|
|
789
803
|
return async (req, res, next) => {
|
|
790
804
|
try {
|
|
791
|
-
const [authType, token] = req.get("Authorization")?.split(" ");
|
|
792
|
-
if (!token)
|
|
793
|
-
throw new Error(ErrorTypes_default.INVALID_TOKEN);
|
|
794
|
-
}
|
|
805
|
+
const [authType, token] = req.get("Authorization")?.split(" ") || [];
|
|
806
|
+
if (!token) throw new Error(ErrorTypes_default.INVALID_TOKEN);
|
|
795
807
|
let payload;
|
|
796
808
|
switch (authType) {
|
|
797
809
|
case "Anon":
|
|
798
|
-
if (!allowAnonymous)
|
|
799
|
-
throw response_default.generateError(403, ErrorTypes_default.ANONYMOUS_SESSION_NOT_ALLOWED);
|
|
800
|
-
}
|
|
810
|
+
if (!allowAnonymous) throw response_default.generateError(403, ErrorTypes_default.ANONYMOUS_SESSION_NOT_ALLOWED);
|
|
801
811
|
payload = await this.verifyAnonymousToken(token);
|
|
802
812
|
break;
|
|
803
813
|
case "User":
|
|
804
|
-
if (!allowUser)
|
|
805
|
-
throw response_default.generateError(403, ErrorTypes_default.USER_SESSION_NOT_ALLOWED);
|
|
806
|
-
}
|
|
814
|
+
if (!allowUser) throw response_default.generateError(403, ErrorTypes_default.USER_SESSION_NOT_ALLOWED);
|
|
807
815
|
payload = await this.verifyUserToken(token);
|
|
808
816
|
break;
|
|
809
817
|
case "System":
|
|
810
|
-
if (!allowSystem)
|
|
811
|
-
throw response_default.generateError(403, ErrorTypes_default.SYSTEM_SESSION_NOT_ALLOWED);
|
|
812
|
-
}
|
|
818
|
+
if (!allowSystem) throw response_default.generateError(403, ErrorTypes_default.SYSTEM_SESSION_NOT_ALLOWED);
|
|
813
819
|
payload = await this.verifySystemToken(token);
|
|
814
|
-
Logger_default.logMessage("AuthMiddleware",
|
|
820
|
+
Logger_default.logMessage("AuthMiddleware", `System Name - ${payload.id}`);
|
|
815
821
|
break;
|
|
816
822
|
case "Admin":
|
|
817
823
|
payload = await this.verifyAdminToken(token);
|
|
818
|
-
Logger_default.logMessage("AuthMiddleware",
|
|
824
|
+
Logger_default.logMessage("AuthMiddleware", `Admin Id - ${payload.id}`);
|
|
819
825
|
break;
|
|
820
826
|
default:
|
|
821
827
|
throw response_default.generateError(403, ErrorTypes_default.INVALID_AUTH_TYPE);
|
|
822
828
|
}
|
|
823
|
-
res.locals.auth = {
|
|
824
|
-
authType,
|
|
825
|
-
token,
|
|
826
|
-
...payload
|
|
827
|
-
};
|
|
829
|
+
res.locals.auth = { authType, token, ...payload };
|
|
828
830
|
next();
|
|
829
831
|
} catch (error) {
|
|
830
832
|
Logger_default.logError("AuthMiddleware", util2.inspect(error));
|
|
831
|
-
response_default.handleException(
|
|
833
|
+
response_default.handleException(
|
|
834
|
+
"AuthMiddleware",
|
|
835
|
+
response_default.generateError(401, error.error || ErrorTypes_default.TOKEN_EXPIRED, true),
|
|
836
|
+
res
|
|
837
|
+
);
|
|
832
838
|
}
|
|
833
839
|
};
|
|
834
840
|
}
|