@warlock.js/auth 4.0.5 → 4.0.25
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/cjs/commands/auth-cleanup-command.d.ts +10 -0
- package/cjs/commands/auth-cleanup-command.d.ts.map +1 -0
- package/cjs/commands/auth-cleanup-command.js +29 -0
- package/cjs/commands/auth-cleanup-command.js.map +1 -0
- package/cjs/commands/jwt-secret-generator-command.d.ts.map +1 -1
- package/cjs/commands/jwt-secret-generator-command.js +5 -1
- package/cjs/commands/jwt-secret-generator-command.js.map +1 -1
- package/cjs/contracts/types.d.ts +100 -1
- package/cjs/contracts/types.d.ts.map +1 -1
- package/cjs/contracts/types.js +20 -0
- package/cjs/contracts/types.js.map +1 -0
- package/cjs/index.d.ts +2 -1
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +1 -1
- package/cjs/middleware/auth.middleware.d.ts.map +1 -1
- package/cjs/middleware/auth.middleware.js +14 -37
- package/cjs/middleware/auth.middleware.js.map +1 -1
- package/cjs/models/auth.d.ts +24 -2
- package/cjs/models/auth.d.ts.map +1 -1
- package/cjs/models/auth.js +39 -40
- package/cjs/models/auth.js.map +1 -1
- package/cjs/models/casts/cast-password.d.ts.map +1 -1
- package/cjs/models/casts/cast-password.js +2 -2
- package/cjs/models/casts/cast-password.js.map +1 -1
- package/cjs/models/index.d.ts +1 -1
- package/cjs/models/index.d.ts.map +1 -1
- package/cjs/models/refresh-token/index.d.ts +2 -0
- package/cjs/models/refresh-token/index.d.ts.map +1 -0
- package/cjs/models/refresh-token/migration.d.ts.map +1 -0
- package/cjs/models/refresh-token/refresh-token.d.ts +32 -0
- package/cjs/models/refresh-token/refresh-token.d.ts.map +1 -0
- package/cjs/models/refresh-token/refresh-token.js +52 -0
- package/cjs/models/refresh-token/refresh-token.js.map +1 -0
- package/cjs/services/auth-events.d.ts +84 -0
- package/cjs/services/auth-events.d.ts.map +1 -0
- package/cjs/services/auth-events.js +65 -0
- package/cjs/services/auth-events.js.map +1 -0
- package/cjs/services/auth.service.d.ts +78 -0
- package/cjs/services/auth.service.d.ts.map +1 -0
- package/cjs/services/auth.service.js +265 -0
- package/cjs/services/auth.service.js.map +1 -0
- package/cjs/services/generate-jwt-secret.d.ts.map +1 -1
- package/cjs/services/generate-jwt-secret.js +4 -4
- package/cjs/services/generate-jwt-secret.js.map +1 -1
- package/cjs/services/index.d.ts +2 -1
- package/cjs/services/index.d.ts.map +1 -1
- package/cjs/services/jwt.d.ts +2 -2
- package/cjs/services/jwt.d.ts.map +1 -1
- package/cjs/services/jwt.js +4 -4
- package/cjs/services/jwt.js.map +1 -1
- package/cjs/utils/auth-error-codes.d.ts +18 -0
- package/cjs/utils/auth-error-codes.d.ts.map +1 -0
- package/cjs/utils/auth-error-codes.js +18 -0
- package/cjs/utils/auth-error-codes.js.map +1 -0
- package/cjs/utils/duration.d.ts +45 -0
- package/cjs/utils/duration.d.ts.map +1 -0
- package/cjs/utils/duration.js +93 -0
- package/cjs/utils/duration.js.map +1 -0
- package/cjs/utils/index.d.ts +3 -0
- package/cjs/utils/index.d.ts.map +1 -0
- package/esm/commands/auth-cleanup-command.d.ts +10 -0
- package/esm/commands/auth-cleanup-command.d.ts.map +1 -0
- package/esm/commands/auth-cleanup-command.js +29 -0
- package/esm/commands/auth-cleanup-command.js.map +1 -0
- package/esm/commands/jwt-secret-generator-command.d.ts.map +1 -1
- package/esm/commands/jwt-secret-generator-command.js +5 -1
- package/esm/commands/jwt-secret-generator-command.js.map +1 -1
- package/esm/contracts/types.d.ts +100 -1
- package/esm/contracts/types.d.ts.map +1 -1
- package/esm/contracts/types.js +20 -0
- package/esm/contracts/types.js.map +1 -0
- package/esm/index.d.ts +2 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -1
- package/esm/middleware/auth.middleware.d.ts.map +1 -1
- package/esm/middleware/auth.middleware.js +14 -37
- package/esm/middleware/auth.middleware.js.map +1 -1
- package/esm/models/auth.d.ts +24 -2
- package/esm/models/auth.d.ts.map +1 -1
- package/esm/models/auth.js +38 -39
- package/esm/models/auth.js.map +1 -1
- package/esm/models/casts/cast-password.d.ts.map +1 -1
- package/esm/models/casts/cast-password.js +2 -2
- package/esm/models/casts/cast-password.js.map +1 -1
- package/esm/models/index.d.ts +1 -1
- package/esm/models/index.d.ts.map +1 -1
- package/esm/models/refresh-token/index.d.ts +2 -0
- package/esm/models/refresh-token/index.d.ts.map +1 -0
- package/esm/models/refresh-token/migration.d.ts.map +1 -0
- package/esm/models/refresh-token/refresh-token.d.ts +32 -0
- package/esm/models/refresh-token/refresh-token.d.ts.map +1 -0
- package/esm/models/refresh-token/refresh-token.js +52 -0
- package/esm/models/refresh-token/refresh-token.js.map +1 -0
- package/esm/services/auth-events.d.ts +84 -0
- package/esm/services/auth-events.d.ts.map +1 -0
- package/esm/services/auth-events.js +65 -0
- package/esm/services/auth-events.js.map +1 -0
- package/esm/services/auth.service.d.ts +78 -0
- package/esm/services/auth.service.d.ts.map +1 -0
- package/esm/services/auth.service.js +265 -0
- package/esm/services/auth.service.js.map +1 -0
- package/esm/services/generate-jwt-secret.d.ts.map +1 -1
- package/esm/services/generate-jwt-secret.js +5 -5
- package/esm/services/generate-jwt-secret.js.map +1 -1
- package/esm/services/index.d.ts +2 -1
- package/esm/services/index.d.ts.map +1 -1
- package/esm/services/jwt.d.ts +2 -2
- package/esm/services/jwt.d.ts.map +1 -1
- package/esm/services/jwt.js +4 -4
- package/esm/services/jwt.js.map +1 -1
- package/esm/utils/auth-error-codes.d.ts +18 -0
- package/esm/utils/auth-error-codes.d.ts.map +1 -0
- package/esm/utils/auth-error-codes.js +18 -0
- package/esm/utils/auth-error-codes.js.map +1 -0
- package/esm/utils/duration.d.ts +45 -0
- package/esm/utils/duration.d.ts.map +1 -0
- package/esm/utils/duration.js +93 -0
- package/esm/utils/duration.js.map +1 -0
- package/esm/utils/index.d.ts +3 -0
- package/esm/utils/index.d.ts.map +1 -0
- package/package.json +12 -9
- package/cjs/commands/index.d.ts +0 -2
- package/cjs/commands/index.d.ts.map +0 -1
- package/cjs/controllers/guest-login.d.ts +0 -3
- package/cjs/controllers/guest-login.d.ts.map +0 -1
- package/cjs/controllers/guest-login.js +0 -5
- package/cjs/controllers/guest-login.js.map +0 -1
- package/cjs/controllers/index.d.ts +0 -2
- package/cjs/controllers/index.d.ts.map +0 -1
- package/cjs/middleware/auth-middleware.d.ts +0 -2
- package/cjs/middleware/auth-middleware.d.ts.map +0 -1
- package/cjs/models/guest/guest.d.ts +0 -26
- package/cjs/models/guest/guest.d.ts.map +0 -1
- package/cjs/models/guest/guest.js +0 -39
- package/cjs/models/guest/guest.js.map +0 -1
- package/cjs/models/guest/index.d.ts +0 -3
- package/cjs/models/guest/index.d.ts.map +0 -1
- package/cjs/models/guest/migration.d.ts.map +0 -1
- package/cjs/models/guest/migration.js +0 -10
- package/cjs/models/guest/migration.js.map +0 -1
- package/cjs/services/generate-guest-token.d.ts +0 -2
- package/cjs/services/generate-guest-token.d.ts.map +0 -1
- package/cjs/services/generate-guest-token.js +0 -11
- package/cjs/services/generate-guest-token.js.map +0 -1
- package/esm/commands/index.d.ts +0 -2
- package/esm/commands/index.d.ts.map +0 -1
- package/esm/controllers/guest-login.d.ts +0 -3
- package/esm/controllers/guest-login.d.ts.map +0 -1
- package/esm/controllers/guest-login.js +0 -5
- package/esm/controllers/guest-login.js.map +0 -1
- package/esm/controllers/index.d.ts +0 -2
- package/esm/controllers/index.d.ts.map +0 -1
- package/esm/middleware/auth-middleware.d.ts +0 -2
- package/esm/middleware/auth-middleware.d.ts.map +0 -1
- package/esm/models/guest/guest.d.ts +0 -26
- package/esm/models/guest/guest.d.ts.map +0 -1
- package/esm/models/guest/guest.js +0 -39
- package/esm/models/guest/guest.js.map +0 -1
- package/esm/models/guest/index.d.ts +0 -3
- package/esm/models/guest/index.d.ts.map +0 -1
- package/esm/models/guest/migration.d.ts.map +0 -1
- package/esm/models/guest/migration.js +0 -10
- package/esm/models/guest/migration.js.map +0 -1
- package/esm/services/generate-guest-token.d.ts +0 -2
- package/esm/services/generate-guest-token.d.ts.map +0 -1
- package/esm/services/generate-guest-token.js +0 -11
- package/esm/services/generate-guest-token.js.map +0 -1
- /package/cjs/models/{guest → refresh-token}/migration.d.ts +0 -0
- /package/esm/models/{guest → refresh-token}/migration.d.ts +0 -0
package/cjs/services/jwt.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
'use strict';var
|
|
2
|
-
const getAlgorithm = () =>
|
|
3
|
-
const getRefreshSecretKey = () =>
|
|
1
|
+
'use strict';var core=require('@warlock.js/core'),fastJwt=require('fast-jwt');const getSecretKey = () => core.config.key("auth.jwt.secret");
|
|
2
|
+
const getAlgorithm = () => core.config.key("auth.jwt.algorithm");
|
|
3
|
+
const getRefreshSecretKey = () => core.config.key("auth.jwt.refresh.secret");
|
|
4
4
|
// Assuming there's a separate config for refresh token validity, for example, '7d' for 7 days
|
|
5
|
-
const getRefreshTokenValidity = () =>
|
|
5
|
+
const getRefreshTokenValidity = () => core.config.key("auth.jwt.refresh.expiresIn");
|
|
6
6
|
const jwt = {
|
|
7
7
|
/**
|
|
8
8
|
* Generate a new JWT token for the user.
|
package/cjs/services/jwt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.js","sources":["../../src/services/jwt.ts"],"sourcesContent":[null],"names":["config","createSigner","createVerifier"],"mappings":"
|
|
1
|
+
{"version":3,"file":"jwt.js","sources":["../../src/services/jwt.ts"],"sourcesContent":[null],"names":["config","createSigner","createVerifier"],"mappings":"8EASA,MAAM,YAAY,GAAG,MAAMA,WAAM,CAAC,GAAG,CAAC,iBAAiB,CAAW,CAAC;AACnE,MAAM,YAAY,GAAG,MAAMA,WAAM,CAAC,GAAG,CAAC,oBAAoB,CAAc,CAAC;AAEzE,MAAM,mBAAmB,GAAG,MAAMA,WAAM,CAAC,GAAG,CAAC,yBAAyB,CAAW,CAAC;AAClF;AACA,MAAM,uBAAuB,G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AE,EAC3B,UAAU,GAAG,CAAC,YAAY,EAAE,CAAC,EAC7B,GAAG,OAAO,EAAA,GAC4B,EAAE,EAAA;AAE1C,QAAA,MAAM,MAAM,GAAGC,sBAAc,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AAC/D,QAAA,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC,CAAC;KAC5B;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare enum AuthErrorCodes {
|
|
2
|
+
/**
|
|
3
|
+
* Missing Access Token Error Code EC001
|
|
4
|
+
* EC001 = Missing Access Token
|
|
5
|
+
*/
|
|
6
|
+
MissingAccessToken = "EC001",
|
|
7
|
+
/**
|
|
8
|
+
* Invalid Access Token Error Code EC002
|
|
9
|
+
* EC002 = Invalid Access Token
|
|
10
|
+
*/
|
|
11
|
+
InvalidAccessToken = "EC002",
|
|
12
|
+
/**
|
|
13
|
+
* Unauthorized Error Code EC003
|
|
14
|
+
* EC003 = Unauthorized
|
|
15
|
+
*/
|
|
16
|
+
Unauthorized = "EC003"
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=auth-error-codes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-error-codes.d.ts","sourceRoot":"","sources":["../../src/utils/auth-error-codes.ts"],"names":[],"mappings":"AAAA,oBAAY,cAAc;IACxB;;;OAGG;IACH,kBAAkB,UAAU;IAC5B;;;OAGG;IACH,kBAAkB,UAAU;IAC5B;;;OAGG;IACH,YAAY,UAAU;CACvB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';exports.AuthErrorCodes=void 0;
|
|
2
|
+
(function (AuthErrorCodes) {
|
|
3
|
+
/**
|
|
4
|
+
* Missing Access Token Error Code EC001
|
|
5
|
+
* EC001 = Missing Access Token
|
|
6
|
+
*/
|
|
7
|
+
AuthErrorCodes["MissingAccessToken"] = "EC001";
|
|
8
|
+
/**
|
|
9
|
+
* Invalid Access Token Error Code EC002
|
|
10
|
+
* EC002 = Invalid Access Token
|
|
11
|
+
*/
|
|
12
|
+
AuthErrorCodes["InvalidAccessToken"] = "EC002";
|
|
13
|
+
/**
|
|
14
|
+
* Unauthorized Error Code EC003
|
|
15
|
+
* EC003 = Unauthorized
|
|
16
|
+
*/
|
|
17
|
+
AuthErrorCodes["Unauthorized"] = "EC003";
|
|
18
|
+
})(exports.AuthErrorCodes || (exports.AuthErrorCodes = {}));//# sourceMappingURL=auth-error-codes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-error-codes.js","sources":["../../src/utils/auth-error-codes.ts"],"sourcesContent":[null],"names":["AuthErrorCodes"],"mappings":"aAAYA,8BAgBX;AAhBD,CAAA,UAAY,cAAc,EAAA;AACxB;;;AAGG;AACH,IAAA,cAAA,CAAA,oBAAA,CAAA,GAAA,OAA4B,CAAA;AAC5B;;;AAGG;AACH,IAAA,cAAA,CAAA,oBAAA,CAAA,GAAA,OAA4B,CAAA;AAC5B;;;AAGG;AACH,IAAA,cAAA,CAAA,cAAA,CAAA,GAAA,OAAsB,CAAA;AACxB,CAAC,EAhBWA,sBAAc,KAAdA,sBAAc,GAgBzB,EAAA,CAAA,CAAA"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { NO_EXPIRATION } from "../contracts/types";
|
|
2
|
+
/**
|
|
3
|
+
* Duration object for specifying time periods
|
|
4
|
+
* All units are additive (e.g., { days: 1, hours: 6 } = 30 hours)
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* { hours: 1 } // 1 hour
|
|
9
|
+
* { days: 7, hours: 12 } // 7.5 days
|
|
10
|
+
* { minutes: 30 } // 30 minutes
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export type Duration = {
|
|
14
|
+
milliseconds?: number;
|
|
15
|
+
seconds?: number;
|
|
16
|
+
minutes?: number;
|
|
17
|
+
hours?: number;
|
|
18
|
+
days?: number;
|
|
19
|
+
weeks?: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Expiration value type - can be a Duration object, string format, or NO_EXPIRATION
|
|
23
|
+
*/
|
|
24
|
+
export type ExpiresIn = Duration | typeof NO_EXPIRATION | string | number;
|
|
25
|
+
/**
|
|
26
|
+
* Parse duration to milliseconds
|
|
27
|
+
* Supports Duration object, string format ("1d 2h 30m"), or number (raw ms)
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* parseExpirationToMs({ hours: 1 }) // 3600000
|
|
32
|
+
* parseExpirationToMs({ days: 1 }) // 86400000
|
|
33
|
+
* parseExpirationToMs("1h") // 3600000
|
|
34
|
+
* parseExpirationToMs("1d 2h 30m") // 95400000
|
|
35
|
+
* parseExpirationToMs(3600000) // 3600000
|
|
36
|
+
* parseExpirationToMs(NO_EXPIRATION) // undefined
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function parseExpirationToMs(expiration: ExpiresIn | undefined, defaultMs?: number): number | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Convert ExpiresIn to a value suitable for jwt.generate (string or number)
|
|
42
|
+
* Returns undefined if NO_EXPIRATION
|
|
43
|
+
*/
|
|
44
|
+
export declare function toJwtExpiresIn(expiration: ExpiresIn | undefined, defaultMs?: number): string | undefined;
|
|
45
|
+
//# sourceMappingURL=duration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../src/utils/duration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,QAAQ,GAAG;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,OAAO,aAAa,GAAG,MAAM,GAAG,MAAM,CAAC;AAE1E;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,SAAS,GAAG,SAAS,EACjC,SAAS,GAAE,MAAgB,GAC1B,MAAM,GAAG,SAAS,CAmBpB;AAuDD;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,SAAS,GAAG,SAAS,EACjC,SAAS,GAAE,MAAgB,GAC1B,MAAM,GAAG,SAAS,CAMpB"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';var types=require('../contracts/types.js');/**
|
|
2
|
+
* Parse duration to milliseconds
|
|
3
|
+
* Supports Duration object, string format ("1d 2h 30m"), or number (raw ms)
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* parseExpirationToMs({ hours: 1 }) // 3600000
|
|
8
|
+
* parseExpirationToMs({ days: 1 }) // 86400000
|
|
9
|
+
* parseExpirationToMs("1h") // 3600000
|
|
10
|
+
* parseExpirationToMs("1d 2h 30m") // 95400000
|
|
11
|
+
* parseExpirationToMs(3600000) // 3600000
|
|
12
|
+
* parseExpirationToMs(NO_EXPIRATION) // undefined
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function parseExpirationToMs(expiration, defaultMs = 3600000) {
|
|
16
|
+
if (expiration === undefined) {
|
|
17
|
+
return defaultMs;
|
|
18
|
+
}
|
|
19
|
+
if (expiration === types.NO_EXPIRATION) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
if (typeof expiration === "number") {
|
|
23
|
+
return expiration;
|
|
24
|
+
}
|
|
25
|
+
if (typeof expiration === "string") {
|
|
26
|
+
return parseStringDuration(expiration);
|
|
27
|
+
}
|
|
28
|
+
// It's a Duration object
|
|
29
|
+
return parseDurationObject(expiration);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parse a Duration object to milliseconds
|
|
33
|
+
*/
|
|
34
|
+
function parseDurationObject(duration) {
|
|
35
|
+
let ms = 0;
|
|
36
|
+
if (duration.milliseconds)
|
|
37
|
+
ms += duration.milliseconds;
|
|
38
|
+
if (duration.seconds)
|
|
39
|
+
ms += duration.seconds * 1000;
|
|
40
|
+
if (duration.minutes)
|
|
41
|
+
ms += duration.minutes * 60 * 1000;
|
|
42
|
+
if (duration.hours)
|
|
43
|
+
ms += duration.hours * 60 * 60 * 1000;
|
|
44
|
+
if (duration.days)
|
|
45
|
+
ms += duration.days * 24 * 60 * 60 * 1000;
|
|
46
|
+
if (duration.weeks)
|
|
47
|
+
ms += duration.weeks * 7 * 24 * 60 * 60 * 1000;
|
|
48
|
+
return ms;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Parse a string duration to milliseconds
|
|
52
|
+
* Supports formats: "1h", "7d", "30m", "90s", "1d 2h 30m"
|
|
53
|
+
*/
|
|
54
|
+
function parseStringDuration(str) {
|
|
55
|
+
let totalMs = 0;
|
|
56
|
+
const parts = str.trim().split(/\s+/);
|
|
57
|
+
for (const part of parts) {
|
|
58
|
+
const match = part.match(/^(\d+(?:\.\d+)?)([smhdw])$/i);
|
|
59
|
+
if (!match)
|
|
60
|
+
continue;
|
|
61
|
+
const value = parseFloat(match[1]);
|
|
62
|
+
const unit = match[2].toLowerCase();
|
|
63
|
+
switch (unit) {
|
|
64
|
+
case "s":
|
|
65
|
+
totalMs += value * 1000;
|
|
66
|
+
break;
|
|
67
|
+
case "m":
|
|
68
|
+
totalMs += value * 60 * 1000;
|
|
69
|
+
break;
|
|
70
|
+
case "h":
|
|
71
|
+
totalMs += value * 60 * 60 * 1000;
|
|
72
|
+
break;
|
|
73
|
+
case "d":
|
|
74
|
+
totalMs += value * 24 * 60 * 60 * 1000;
|
|
75
|
+
break;
|
|
76
|
+
case "w":
|
|
77
|
+
totalMs += value * 7 * 24 * 60 * 60 * 1000;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return totalMs || 3600000; // Default to 1 hour if nothing parsed
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Convert ExpiresIn to a value suitable for jwt.generate (string or number)
|
|
85
|
+
* Returns undefined if NO_EXPIRATION
|
|
86
|
+
*/
|
|
87
|
+
function toJwtExpiresIn(expiration, defaultMs = 3600000) {
|
|
88
|
+
const ms = parseExpirationToMs(expiration, defaultMs);
|
|
89
|
+
if (ms === undefined)
|
|
90
|
+
return undefined;
|
|
91
|
+
// Convert ms to seconds for JWT (more common format)
|
|
92
|
+
return Math.floor(ms / 1000) + "s";
|
|
93
|
+
}exports.parseExpirationToMs=parseExpirationToMs;exports.toJwtExpiresIn=toJwtExpiresIn;//# sourceMappingURL=duration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duration.js","sources":["../../src/utils/duration.ts"],"sourcesContent":[null],"names":["NO_EXPIRATION"],"mappings":"wDA2BA;;;;;;;;;;;;;AAaG;SACa,mBAAmB,CACjC,UAAiC,EACjC,YAAoB,OAAO,EAAA;IAE3B,IAAI,UAAU,KAAK,SAAS,EAAE;AAC5B,QAAA,OAAO,SAAS,CAAC;AAClB,KAAA;IAED,IAAI,UAAU,KAAKA,mBAAa,EAAE;AAChC,QAAA,OAAO,SAAS,CAAC;AAClB,KAAA;AAED,IAAA,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;AAClC,QAAA,OAAO,UAAU,CAAC;AACnB,KAAA;AAED,IAAA,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;AAClC,QAAA,OAAO,mBAAmB,CAAC,UAAU,CAAC,CAAC;AACxC,KAAA;;AAGD,IAAA,OAAO,mBAAmB,CAAC,UA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I,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAEpC,QAAA,QAAQ,IAAI;AACV,YAAA,KAAK,GAAG;AACN,gBAAA,OAAO,IAAI,KAAK,GAAG,IAAI,CAAC;gBACxB,MAAM;AACR,YAAA,KAAK,GAAG;AACN,gBAAA,OAAO,IAAI,KAAK,GAAG,EAAE,GAAG,IAAI,CAAC;gBAC7B,MAAM;AACR,YAAA,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;gBAClC,MAAM;AACR,YAAA,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;gBACvC,MAAM;AACR,YAAA,KAAK,GAAG;AACN,gBAAA,OAAO,IAAI,KAAK,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;gBAC3C,MAAM;AACT,SAAA;AACF,KAAA;AAED,IAAA,OAAO,OAAO,IAAI,OAAO,CAAC;AAC5B,CAAC;AAED;;;AAGG;SACa,cAAc,CAC5B,UAAiC,EACjC,YAAoB,OAAO,EAAA;IAE3B,MAAM,EAAE,GAAG,mBAAmB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;IACtD,IAAI,EAAE,KAAK,SAAS;AAAE,QAAA,OAAO,SAAS,CAAC;;IAGvC,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC;AACrC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-cleanup-command.d.ts","sourceRoot":"","sources":["../../src/commands/auth-cleanup-command.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,QAqBzC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {colors}from'@mongez/copper';import {command}from'@warlock.js/core';import {authService}from'../services/auth.service.js';/**
|
|
2
|
+
* Register the auth:cleanup CLI command
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```bash
|
|
6
|
+
* warlock auth:cleanup
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
function registerAuthCleanupCommand() {
|
|
10
|
+
return command({
|
|
11
|
+
name: "auth.cleanup",
|
|
12
|
+
description: "Remove expired refresh tokens from the database",
|
|
13
|
+
preload: {
|
|
14
|
+
env: true,
|
|
15
|
+
config: ["auth", "database"],
|
|
16
|
+
connectors: ["database"],
|
|
17
|
+
},
|
|
18
|
+
action: async () => {
|
|
19
|
+
console.log(colors.cyan("🧹 Cleaning up expired tokens..."));
|
|
20
|
+
const count = await authService.cleanupExpiredTokens();
|
|
21
|
+
if (count === 0) {
|
|
22
|
+
console.log(colors.green("✅ No expired tokens found."));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.log(colors.green(`✅ Removed ${count} expired token(s).`));
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}export{registerAuthCleanupCommand};//# sourceMappingURL=auth-cleanup-command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-cleanup-command.js","sources":["../../src/commands/auth-cleanup-command.ts"],"sourcesContent":[null],"names":[],"mappings":"iIAIA;;;;;;;AAOG;SACa,0BAA0B,GAAA;AACxC,IAAA,OAAO,OAAO,CAAC;AACb,QAAA,IAAI,EAAE,cAAc;AACpB,QAAA,WAAW,EAAE,iDAAiD;AAC9D,QAAA,OAAO,EAAE;AACP,YAAA,GAAG,EAAE,IAAI;AACT,YAAA,MAAM,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC;YAC5B,UAAU,EAAE,CAAC,UAAU,CAAC;AACzB,SAAA;QACD,MAAM,EAAE,YAAW;YACjB,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC,CAAC;AAE7D,YAAA,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,oBAAoB,EAAE,CAAC;YAEvD,IAAI,KAAK,KAAK,CAAC,EAAE;gBACf,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC;AACzD,aAAA;AAAM,iBAAA;AACL,gBAAA,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA,UAAA,EAAa,KAAK,CAAA,kBAAA,CAAoB,CAAC,CAAC,CAAC;AACnE,aAAA;SACF;AACF,KAAA,CAAC,CAAC;AACL"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt-secret-generator-command.d.ts","sourceRoot":"","sources":["../../src/commands/jwt-secret-generator-command.ts"],"names":[],"mappings":"AAGA,wBAAgB,iCAAiC,
|
|
1
|
+
{"version":3,"file":"jwt-secret-generator-command.d.ts","sourceRoot":"","sources":["../../src/commands/jwt-secret-generator-command.ts"],"names":[],"mappings":"AAGA,wBAAgB,iCAAiC,QAMhD"}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import {command}from'@warlock.js/core';import {generateJWTSecret}from'../services/generate-jwt-secret.js';function registerJWTSecretGeneratorCommand() {
|
|
2
|
-
return command(
|
|
2
|
+
return command({
|
|
3
|
+
name: "jwt.generate",
|
|
4
|
+
description: "Generate JWT Secret key in .env file",
|
|
5
|
+
action: generateJWTSecret,
|
|
6
|
+
});
|
|
3
7
|
}export{registerJWTSecretGeneratorCommand};//# sourceMappingURL=jwt-secret-generator-command.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt-secret-generator-command.js","sources":["../../src/commands/jwt-secret-generator-command.ts"],"sourcesContent":[null],"names":[],"mappings":"mHAGgB,iCAAiC,GAAA;
|
|
1
|
+
{"version":3,"file":"jwt-secret-generator-command.js","sources":["../../src/commands/jwt-secret-generator-command.ts"],"sourcesContent":[null],"names":[],"mappings":"mHAGgB,iCAAiC,GAAA;AAC/C,IAAA,OAAO,OAAO,CAAC;AACb,QAAA,IAAI,EAAE,cAAc;AACpB,QAAA,WAAW,EAAE,sCAAsC;AACnD,QAAA,MAAM,EAAE,iBAAiB;AAC1B,KAAA,CAAC,CAAC;AACL"}
|
package/esm/contracts/types.d.ts
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
import { type Algorithm } from "fast-jwt";
|
|
2
2
|
import type { Auth } from "../models/auth";
|
|
3
|
+
import type { Duration, ExpiresIn } from "../utils/duration";
|
|
4
|
+
/**
|
|
5
|
+
* Symbol to indicate no expiration for tokens
|
|
6
|
+
* Use this when you explicitly want tokens to never expire
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // src/config/auth.ts
|
|
11
|
+
* import { NO_EXPIRATION, type AuthConfigurations } from "@warlock.js/auth";
|
|
12
|
+
*
|
|
13
|
+
* const authConfigurations: AuthConfigurations = {
|
|
14
|
+
* jwt: {
|
|
15
|
+
* secret: env("JWT_SECRET"),
|
|
16
|
+
* expiresIn: NO_EXPIRATION, // Token never expires
|
|
17
|
+
* },
|
|
18
|
+
* };
|
|
19
|
+
*
|
|
20
|
+
* export default authConfigurations;
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare const NO_EXPIRATION: unique symbol;
|
|
3
24
|
export type AuthConfigurations = {
|
|
4
25
|
/**
|
|
5
26
|
* Define all user types
|
|
@@ -12,11 +33,50 @@ export type AuthConfigurations = {
|
|
|
12
33
|
* JWT configurations
|
|
13
34
|
*/
|
|
14
35
|
jwt: {
|
|
36
|
+
/**
|
|
37
|
+
* JWT secret key for signing access tokens
|
|
38
|
+
*/
|
|
15
39
|
secret: string;
|
|
40
|
+
/**
|
|
41
|
+
* JWT algorithm
|
|
42
|
+
* @default "HS256"
|
|
43
|
+
*/
|
|
16
44
|
algorithm?: Algorithm;
|
|
45
|
+
/**
|
|
46
|
+
* Access token expiration time
|
|
47
|
+
* Supports Duration object, string format, or NO_EXPIRATION
|
|
48
|
+
* @example { hours: 1 }, { days: 7, hours: 12 }, "1h", "1d 2h", NO_EXPIRATION
|
|
49
|
+
* @default { hours: 1 }
|
|
50
|
+
*/
|
|
51
|
+
expiresIn?: ExpiresIn;
|
|
52
|
+
/**
|
|
53
|
+
* Refresh token configurations
|
|
54
|
+
*/
|
|
17
55
|
refresh?: {
|
|
56
|
+
/**
|
|
57
|
+
* Separate secret for refresh tokens (recommended for security)
|
|
58
|
+
* If not provided, falls back to main JWT secret
|
|
59
|
+
*/
|
|
18
60
|
secret?: string;
|
|
19
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Refresh token expiration time
|
|
63
|
+
* Supports Duration object or string format
|
|
64
|
+
* @example { days: 7 }, { weeks: 1 }, "7d", "1w"
|
|
65
|
+
* @default { days: 7 }
|
|
66
|
+
*/
|
|
67
|
+
expiresIn?: Duration | string | number;
|
|
68
|
+
/**
|
|
69
|
+
* Enable token rotation (issue new refresh token on each use)
|
|
70
|
+
* Old refresh token is invalidated after use
|
|
71
|
+
* @default true
|
|
72
|
+
*/
|
|
73
|
+
rotation?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Maximum number of active refresh tokens per user
|
|
76
|
+
* When exceeded, oldest tokens are revoked
|
|
77
|
+
* @default 5
|
|
78
|
+
*/
|
|
79
|
+
maxPerUser?: number;
|
|
20
80
|
};
|
|
21
81
|
};
|
|
22
82
|
/**
|
|
@@ -32,4 +92,43 @@ export type AuthConfigurations = {
|
|
|
32
92
|
salt?: number;
|
|
33
93
|
};
|
|
34
94
|
};
|
|
95
|
+
/**
|
|
96
|
+
* Token pair returned after login or token refresh
|
|
97
|
+
*/
|
|
98
|
+
export type TokenPair = {
|
|
99
|
+
/**
|
|
100
|
+
* JWT access token (short-lived)
|
|
101
|
+
*/
|
|
102
|
+
accessToken: string;
|
|
103
|
+
/**
|
|
104
|
+
* JWT refresh token (long-lived)
|
|
105
|
+
*/
|
|
106
|
+
refreshToken: string;
|
|
107
|
+
/**
|
|
108
|
+
* Access token expiration time in seconds or time string
|
|
109
|
+
*/
|
|
110
|
+
expiresIn: number | string;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Device information for session tracking
|
|
114
|
+
*/
|
|
115
|
+
export type DeviceInfo = {
|
|
116
|
+
/**
|
|
117
|
+
* User agent string from request
|
|
118
|
+
*/
|
|
119
|
+
userAgent?: string;
|
|
120
|
+
/**
|
|
121
|
+
* Client IP address
|
|
122
|
+
*/
|
|
123
|
+
ip?: string;
|
|
124
|
+
/**
|
|
125
|
+
* Optional device identifier
|
|
126
|
+
*/
|
|
127
|
+
deviceId?: string;
|
|
128
|
+
/**
|
|
129
|
+
* Token family ID (for rotation tracking)
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
familyId?: string;
|
|
133
|
+
};
|
|
35
134
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/contracts/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/contracts/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE7D;;;;;;;;;;;;;;;;;;GAkBG;AACH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symbol to indicate no expiration for tokens
|
|
3
|
+
* Use this when you explicitly want tokens to never expire
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* // src/config/auth.ts
|
|
8
|
+
* import { NO_EXPIRATION, type AuthConfigurations } from "@warlock.js/auth";
|
|
9
|
+
*
|
|
10
|
+
* const authConfigurations: AuthConfigurations = {
|
|
11
|
+
* jwt: {
|
|
12
|
+
* secret: env("JWT_SECRET"),
|
|
13
|
+
* expiresIn: NO_EXPIRATION, // Token never expires
|
|
14
|
+
* },
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* export default authConfigurations;
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
const NO_EXPIRATION = Symbol("NO_EXPIRATION");export{NO_EXPIRATION};//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sources":["../../src/contracts/types.ts"],"sourcesContent":[null],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;AAkBG;MACU,aAAa,GAAG,MAAM,CAAC,eAAe"}
|
package/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
export * from "./commands/auth-cleanup-command";
|
|
1
2
|
export * from "./commands/jwt-secret-generator-command";
|
|
2
3
|
export * from "./contracts";
|
|
3
|
-
export * from "./controllers";
|
|
4
4
|
export * from "./middleware";
|
|
5
5
|
export * from "./models";
|
|
6
6
|
export * from "./services";
|
|
7
|
+
export * from "./utils";
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,yCAAyC,CAAC;AACxD,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC"}
|
package/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{registerJWTSecretGeneratorCommand}from'./commands/jwt-secret-generator-command.js';export{
|
|
1
|
+
export{registerAuthCleanupCommand}from'./commands/auth-cleanup-command.js';export{registerJWTSecretGeneratorCommand}from'./commands/jwt-secret-generator-command.js';export{NO_EXPIRATION}from'./contracts/types.js';export{authMiddleware}from'./middleware/auth.middleware.js';export{AccessToken}from'./models/access-token/access-token.js';import'./models/access-token/migration.js';export{Auth}from'./models/auth.js';export{castPassword}from'./models/casts/cast-password.js';export{RefreshToken}from'./models/refresh-token/refresh-token.js';export{authEvents}from'./services/auth-events.js';export{authService}from'./services/auth.service.js';export{generateJWTSecret}from'./services/generate-jwt-secret.js';export{jwt}from'./services/jwt.js';export{AuthErrorCodes}from'./utils/auth-error-codes.js';export{parseExpirationToMs,toJwtExpiresIn}from'./utils/duration.js';//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.middleware.ts"],"names":[],"mappings":"AAMA,wBAAgB,cAAc,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"auth.middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.middleware.ts"],"names":[],"mappings":"AAMA,wBAAgB,cAAc,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,cAuFjE"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import config
|
|
1
|
+
import {t,config}from'@warlock.js/core';import {log}from'@warlock.js/logger';import {AccessToken}from'../models/access-token/access-token.js';import'../models/access-token/migration.js';import {jwt}from'../services/jwt.js';import {AuthErrorCodes}from'../utils/auth-error-codes.js';function authMiddleware(allowedUserType) {
|
|
2
2
|
const allowedTypes = !allowedUserType
|
|
3
3
|
? []
|
|
4
4
|
: Array.isArray(allowedUserType)
|
|
@@ -11,20 +11,22 @@ import config from'@mongez/config';import {log}from'@warlock.js/logger';import {
|
|
|
11
11
|
return;
|
|
12
12
|
if (!authorizationValue) {
|
|
13
13
|
return response.unauthorized({
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
error: t("auth.errors.missingAccessToken"),
|
|
15
|
+
errorCode: AuthErrorCodes.MissingAccessToken,
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
// get current user jwt
|
|
19
19
|
const user = await jwt.verify(authorizationValue);
|
|
20
|
+
// store decoded access token object in request object
|
|
21
|
+
request.decodedAccessToken = user;
|
|
20
22
|
// use our own jwt verify to verify the token
|
|
21
23
|
const accessToken = await AccessToken.first({
|
|
22
24
|
token: authorizationValue,
|
|
23
25
|
});
|
|
24
26
|
if (!accessToken) {
|
|
25
27
|
return response.unauthorized({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
error: t("auth.errors.invalidAccessToken"),
|
|
29
|
+
errorCode: AuthErrorCodes.InvalidAccessToken,
|
|
28
30
|
});
|
|
29
31
|
}
|
|
30
32
|
// now, we need to get an instance of user using its corresponding model
|
|
@@ -32,14 +34,13 @@ import config from'@mongez/config';import {log}from'@warlock.js/logger';import {
|
|
|
32
34
|
// check if the user type is allowed
|
|
33
35
|
if (allowedTypes.length && !allowedTypes.includes(userType)) {
|
|
34
36
|
return response.unauthorized({
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
error: t("auth.errors.unauthorized"),
|
|
38
|
+
errorCode: AuthErrorCodes.Unauthorized,
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
41
|
// get user model class
|
|
40
|
-
const UserModel = config.
|
|
42
|
+
const UserModel = config.key(`auth.userType.${userType}`);
|
|
41
43
|
if (!UserModel) {
|
|
42
|
-
// TODO: translate this message
|
|
43
44
|
throw new Error(`User type ${userType} is unknown type.`);
|
|
44
45
|
}
|
|
45
46
|
// get user model instance
|
|
@@ -47,8 +48,8 @@ import config from'@mongez/config';import {log}from'@warlock.js/logger';import {
|
|
|
47
48
|
if (!currentUser) {
|
|
48
49
|
accessToken.destroy();
|
|
49
50
|
return response.unauthorized({
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
error: t("auth.errors.invalidAccessToken"),
|
|
52
|
+
errorCode: AuthErrorCodes.InvalidAccessToken,
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
// update last access
|
|
@@ -63,34 +64,10 @@ import config from'@mongez/config';import {log}from'@warlock.js/logger';import {
|
|
|
63
64
|
// unset current user
|
|
64
65
|
request.clearCurrentUser();
|
|
65
66
|
return response.unauthorized({
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
error: t("auth.errors.invalidAccessToken"),
|
|
68
|
+
errorCode: AuthErrorCodes.InvalidAccessToken,
|
|
68
69
|
});
|
|
69
70
|
}
|
|
70
71
|
};
|
|
71
|
-
if (allowedUserType) {
|
|
72
|
-
const userAccessTokenKey = `${allowedUserType}AccessToken`;
|
|
73
|
-
const userAccessTokenKeyNameHeader = `${allowedUserType}AccessTokenHeader`;
|
|
74
|
-
auth.postman = {
|
|
75
|
-
onCollectingVariables(variables) {
|
|
76
|
-
if (variables.find(variable => variable.key === userAccessTokenKeyNameHeader))
|
|
77
|
-
return;
|
|
78
|
-
variables.push({
|
|
79
|
-
key: userAccessTokenKey,
|
|
80
|
-
value: "YOUR_TOKEN_HERE",
|
|
81
|
-
});
|
|
82
|
-
variables.push({
|
|
83
|
-
key: userAccessTokenKeyNameHeader,
|
|
84
|
-
value: `Bearer {{${userAccessTokenKey}}}`,
|
|
85
|
-
});
|
|
86
|
-
},
|
|
87
|
-
onAddingRequest({ request }) {
|
|
88
|
-
request.header.push({
|
|
89
|
-
key: "Authorization",
|
|
90
|
-
value: `{{${userAccessTokenKeyNameHeader}}}`,
|
|
91
|
-
});
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
72
|
return auth;
|
|
96
73
|
}export{authMiddleware};//# sourceMappingURL=auth.middleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.middleware.js","sources":["../../src/middleware/auth.middleware.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.middleware.js","sources":["../../src/middleware/auth.middleware.ts"],"sourcesContent":[null],"names":[],"mappings":"yRAMM,SAAU,cAAc,CAAC,eAAmC,EAAA;IAChE,MAAM,YAAY,GAAG,CAAC,eAAe;AACnC,UAAE,EAAE;AACJ,UAAE,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC;AAC9B,cAAE,eAAe;AACjB,cAAE,CAAC,eAAe,CAAC,CAAC;IAExB,MAAM,IAAI,GAAe,OAAO,OAAgB,EAAE,QAAkB,KAAI;QACtE,IAAI;AACF,YAAA,MAAM,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC;AAEtD,YAAA,IAAI,CAAC,YAAY,CAAC,MAAM,IAAI,CAAC,kBAAkB;gBAAE,OAAO;YAExD,IAAI,CAAC,kBAAkB,EAAE;gBACvB,OAAO,QAAQ,CAAC,YAAY,CAAC;AAC3B,oBAAA,KAAK,EAAE,CAAC,CAAC,gCAAgC,CAAC;oBAC1C,SAAS,EAAE,cAAc,CAAC,kBAAkB;AAC7C,iBAAA,CAAC,CAAC;AACJ,aAAA;;YAGD,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC;;AAGlD,YAAA,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC;;AAElC,YAAA,MAAM,WAAW,GAAG,MAAM,WAAW,CAAC,KAAK,CAAC;AAC1C,gBAAA,KAAK,EAAE,kBAAkB;AAC1B,aAAA,CAAC,CAAC;YAEH,IAAI,CAAC,WAAW,EAAE;gBAChB,OAAO,QAAQ,CAAC,YAAY,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