@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
|
@@ -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
|
+
'use strict';var copper=require('@mongez/copper'),core=require('@warlock.js/core'),auth_service=require('../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 core.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(copper.colors.cyan("🧹 Cleaning up expired tokens..."));
|
|
20
|
+
const count = await auth_service.authService.cleanupExpiredTokens();
|
|
21
|
+
if (count === 0) {
|
|
22
|
+
console.log(copper.colors.green("✅ No expired tokens found."));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.log(copper.colors.green(`✅ Removed ${count} expired token(s).`));
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}exports.registerAuthCleanupCommand=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":["command","colors","authService"],"mappings":"uIAIA;;;;;;;AAOG;SACa,0BAA0B,GAAA;AACxC,IAAA,OAAOA,YAAO,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,CAACC,aAAM,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC,CAAC;AAE7D,YAAA,MAAM,KAAK,GAAG,MAAMC,wBAAW,CAAC,oBAAoB,EAAE,CAAC;YAEvD,IAAI,KAAK,KAAK,CAAC,EAAE;gBACf,OAAO,CAAC,GAAG,CAACD,aAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC;AACzD,aAAA;AAAM,iBAAA;AACL,gBAAA,OAAO,CAAC,GAAG,CAACA,aAAM,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
|
'use strict';var core=require('@warlock.js/core'),generateJwtSecret=require('../services/generate-jwt-secret.js');function registerJWTSecretGeneratorCommand() {
|
|
2
|
-
return core.command(
|
|
2
|
+
return core.command({
|
|
3
|
+
name: "jwt.generate",
|
|
4
|
+
description: "Generate JWT Secret key in .env file",
|
|
5
|
+
action: generateJwtSecret.generateJWTSecret,
|
|
6
|
+
});
|
|
3
7
|
}exports.registerJWTSecretGeneratorCommand=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":["command","generateJWTSecret"],"mappings":"2HAGgB,iCAAiC,GAAA;
|
|
1
|
+
{"version":3,"file":"jwt-secret-generator-command.js","sources":["../../src/commands/jwt-secret-generator-command.ts"],"sourcesContent":[null],"names":["command","generateJWTSecret"],"mappings":"2HAGgB,iCAAiC,GAAA;AAC/C,IAAA,OAAOA,YAAO,CAAC;AACb,QAAA,IAAI,EAAE,cAAc;AACpB,QAAA,WAAW,EAAE,sCAAsC;AACnD,QAAA,MAAM,EAAEC,mCAAiB;AAC1B,KAAA,CAAC,CAAC;AACL"}
|
package/cjs/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,eAAO,MAAM,aAAa,eAA0B,CAAC;AAErD,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,QAAQ,EAAE;QACR,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,IAAI,CAAC;KACjC,CAAC;IACF;;OAEG;IACH,GAAG,EAAE;QACH;;WAEG;QACH,MAAM,EAAE,MAAM,CAAC;QACf;;;WAGG;QACH,SAAS,CAAC,EAAE,SAAS,CAAC;QACtB;;;;;WAKG;QACH,SAAS,CAAC,EAAE,SAAS,CAAC;QACtB;;WAEG;QACH,OAAO,CAAC,EAAE;YACR;;;eAGG;YACH,MAAM,CAAC,EAAE,MAAM,CAAC;YAChB;;;;;eAKG;YACH,SAAS,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;YACvC;;;;eAIG;YACH,QAAQ,CAAC,EAAE,OAAO,CAAC;YACnB;;;;eAIG;YACH,UAAU,CAAC,EAAE,MAAM,CAAC;SACrB,CAAC;KACH,CAAC;IACF;;OAEG;IACH,QAAQ,CAAC,EAAE;QACT;;;;;WAKG;QACH,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;CACH,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';/**
|
|
2
|
+
* 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");exports.NO_EXPIRATION=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/cjs/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/cjs/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/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var jwtSecretGeneratorCommand=require('./commands/jwt-secret-generator-command.js'),
|
|
1
|
+
'use strict';var authCleanupCommand=require('./commands/auth-cleanup-command.js'),jwtSecretGeneratorCommand=require('./commands/jwt-secret-generator-command.js'),types=require('./contracts/types.js'),auth_middleware=require('./middleware/auth.middleware.js'),accessToken=require('./models/access-token/access-token.js');require('./models/access-token/migration.js');var auth=require('./models/auth.js'),castPassword=require('./models/casts/cast-password.js'),refreshToken=require('./models/refresh-token/refresh-token.js'),authEvents=require('./services/auth-events.js'),auth_service=require('./services/auth.service.js'),generateJwtSecret=require('./services/generate-jwt-secret.js'),jwt=require('./services/jwt.js'),authErrorCodes=require('./utils/auth-error-codes.js'),duration=require('./utils/duration.js');exports.registerAuthCleanupCommand=authCleanupCommand.registerAuthCleanupCommand;exports.registerJWTSecretGeneratorCommand=jwtSecretGeneratorCommand.registerJWTSecretGeneratorCommand;exports.NO_EXPIRATION=types.NO_EXPIRATION;exports.authMiddleware=auth_middleware.authMiddleware;exports.AccessToken=accessToken.AccessToken;exports.Auth=auth.Auth;exports.castPassword=castPassword.castPassword;exports.RefreshToken=refreshToken.RefreshToken;exports.authEvents=authEvents.authEvents;exports.authService=auth_service.authService;exports.generateJWTSecret=generateJwtSecret.generateJWTSecret;exports.jwt=jwt.jwt;Object.defineProperty(exports,'AuthErrorCodes',{enumerable:true,get:function(){return authErrorCodes.AuthErrorCodes}});exports.parseExpirationToMs=duration.parseExpirationToMs;exports.toJwtExpiresIn=duration.toJwtExpiresIn;//# 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
|
-
'use strict';var
|
|
1
|
+
'use strict';var core=require('@warlock.js/core'),logger=require('@warlock.js/logger'),accessToken=require('../models/access-token/access-token.js');require('../models/access-token/migration.js');var jwt=require('../services/jwt.js'),authErrorCodes=require('../utils/auth-error-codes.js');function authMiddleware(allowedUserType) {
|
|
2
2
|
const allowedTypes = !allowedUserType
|
|
3
3
|
? []
|
|
4
4
|
: Array.isArray(allowedUserType)
|
|
@@ -11,20 +11,22 @@
|
|
|
11
11
|
return;
|
|
12
12
|
if (!authorizationValue) {
|
|
13
13
|
return response.unauthorized({
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
error: core.t("auth.errors.missingAccessToken"),
|
|
15
|
+
errorCode: authErrorCodes.AuthErrorCodes.MissingAccessToken,
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
// get current user jwt
|
|
19
19
|
const user = await jwt.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$1 = await accessToken.AccessToken.first({
|
|
22
24
|
token: authorizationValue,
|
|
23
25
|
});
|
|
24
26
|
if (!accessToken$1) {
|
|
25
27
|
return response.unauthorized({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
error: core.t("auth.errors.invalidAccessToken"),
|
|
29
|
+
errorCode: authErrorCodes.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 @@
|
|
|
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: core.t("auth.errors.unauthorized"),
|
|
38
|
+
errorCode: authErrorCodes.AuthErrorCodes.Unauthorized,
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
41
|
// get user model class
|
|
40
|
-
const UserModel =
|
|
42
|
+
const UserModel = core.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 @@
|
|
|
47
48
|
if (!currentUser) {
|
|
48
49
|
accessToken$1.destroy();
|
|
49
50
|
return response.unauthorized({
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
error: core.t("auth.errors.invalidAccessToken"),
|
|
52
|
+
errorCode: authErrorCodes.AuthErrorCodes.InvalidAccessToken,
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
// update last access
|
|
@@ -63,34 +64,10 @@
|
|
|
63
64
|
// unset current user
|
|
64
65
|
request.clearCurrentUser();
|
|
65
66
|
return response.unauthorized({
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
error: core.t("auth.errors.invalidAccessToken"),
|
|
68
|
+
errorCode: authErrorCodes.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
|
}exports.authMiddleware=authMiddleware;//# sourceMappingURL=auth.middleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.middleware.js","sources":["../../src/middleware/auth.middleware.ts"],"sourcesContent":[null],"names":["jwt","accessToken","AccessToken","config","log"],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.middleware.js","sources":["../../src/middleware/auth.middleware.ts"],"sourcesContent":[null],"names":["t","AuthErrorCodes","jwt","accessToken","AccessToken","config","log"],"mappings":"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"}
|
package/cjs/models/auth.d.ts
CHANGED
|
@@ -1,18 +1,40 @@
|
|
|
1
1
|
import type { ChildModel } from "@warlock.js/cascade";
|
|
2
2
|
import { Model } from "@warlock.js/cascade";
|
|
3
|
+
import type { DeviceInfo, TokenPair } from "../contracts/types";
|
|
4
|
+
import type { RefreshToken } from "./refresh-token/refresh-token";
|
|
3
5
|
export declare abstract class Auth extends Model {
|
|
4
6
|
/**
|
|
5
7
|
* Get user type
|
|
6
8
|
*/
|
|
7
9
|
abstract get userType(): string;
|
|
8
10
|
/**
|
|
9
|
-
*
|
|
11
|
+
* Get access token payload
|
|
12
|
+
*/
|
|
13
|
+
accessTokenPayload(): any;
|
|
14
|
+
/**
|
|
15
|
+
* Create both access and refresh tokens
|
|
16
|
+
*/
|
|
17
|
+
createTokenPair(deviceInfo?: DeviceInfo): Promise<TokenPair>;
|
|
18
|
+
/**
|
|
19
|
+
* Generate access token
|
|
10
20
|
*/
|
|
11
21
|
generateAccessToken(data?: any): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Generate refresh token
|
|
24
|
+
*/
|
|
25
|
+
generateRefreshToken(deviceInfo?: DeviceInfo): Promise<RefreshToken>;
|
|
12
26
|
/**
|
|
13
27
|
* Remove current access token
|
|
14
28
|
*/
|
|
15
29
|
removeAccessToken(token: string): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Revoke all tokens (logout from all devices)
|
|
32
|
+
*/
|
|
33
|
+
revokeAllTokens(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Get active sessions
|
|
36
|
+
*/
|
|
37
|
+
activeSessions(): Promise<RefreshToken[]>;
|
|
16
38
|
/**
|
|
17
39
|
* Attempt to login the user
|
|
18
40
|
*/
|
|
@@ -20,6 +42,6 @@ export declare abstract class Auth extends Model {
|
|
|
20
42
|
/**
|
|
21
43
|
* Confirm password
|
|
22
44
|
*/
|
|
23
|
-
confirmPassword(password: string):
|
|
45
|
+
confirmPassword(password: string): boolean;
|
|
24
46
|
}
|
|
25
47
|
//# sourceMappingURL=auth.d.ts.map
|
package/cjs/models/auth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/models/auth.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/models/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAElE,8BAAsB,IAAK,SAAQ,KAAK;IACtC;;OAEG;IACH,aAAoB,QAAQ,IAAI,MAAM,CAAC;IAEvC;;OAEG;IACI,kBAAkB;IAMzB;;OAEG;IACU,eAAe,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;IAIzE;;OAEG;IACU,mBAAmB,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;IAI7D;;OAEG;IACU,oBAAoB,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAIjF;;OAEG;IACU,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI5D;;OAEG;IACU,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7C;;OAEG;IACU,cAAc,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;IAItD;;OAEG;WACiB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAIjF;;OAEG;IACI,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;CAGlD"}
|
package/cjs/models/auth.js
CHANGED
|
@@ -1,59 +1,58 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var cascade=require('@warlock.js/cascade');require('@mongez/events');var auth_service=require('../services/auth.service.js');require('@mongez/fs'),require('@mongez/reinforcements'),require('@warlock.js/core'),require('@warlock.js/logger'),require('fast-jwt');class Auth extends cascade.Model {
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Get access token payload
|
|
4
|
+
*/
|
|
5
|
+
accessTokenPayload() {
|
|
6
|
+
// Dynamically import to avoid circular dependency
|
|
7
|
+
const { authService } = require("../services/auth.service");
|
|
8
|
+
return authService.buildAccessTokenPayload(this);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create both access and refresh tokens
|
|
12
|
+
*/
|
|
13
|
+
async createTokenPair(deviceInfo) {
|
|
14
|
+
return auth_service.authService.createTokenPair(this, deviceInfo);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate access token
|
|
4
18
|
*/
|
|
5
19
|
async generateAccessToken(data) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
userType: this.userType,
|
|
14
|
-
createdAt: Date.now(),
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
// use our own jwt generator to generate a token for the guest
|
|
18
|
-
const token = await jwt.jwt.generate(data);
|
|
19
|
-
// store token and the auth model data in the access token model
|
|
20
|
-
// note that we didn't make it sync because we don't want to wait for the token to be stored in the database
|
|
21
|
-
// as nothing depends on it
|
|
22
|
-
accessToken.AccessToken.create({
|
|
23
|
-
token,
|
|
24
|
-
user: data,
|
|
25
|
-
});
|
|
26
|
-
return token;
|
|
20
|
+
return auth_service.authService.generateAccessToken(this, data);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Generate refresh token
|
|
24
|
+
*/
|
|
25
|
+
async generateRefreshToken(deviceInfo) {
|
|
26
|
+
return auth_service.authService.createRefreshToken(this, deviceInfo);
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Remove current access token
|
|
30
30
|
*/
|
|
31
31
|
async removeAccessToken(token) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
return auth_service.authService.removeAccessToken(this, token);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Revoke all tokens (logout from all devices)
|
|
36
|
+
*/
|
|
37
|
+
async revokeAllTokens() {
|
|
38
|
+
return auth_service.authService.revokeAllTokens(this);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get active sessions
|
|
42
|
+
*/
|
|
43
|
+
async activeSessions() {
|
|
44
|
+
return auth_service.authService.getActiveSessions(this);
|
|
36
45
|
}
|
|
37
46
|
/**
|
|
38
47
|
* Attempt to login the user
|
|
39
48
|
*/
|
|
40
49
|
static async attempt(data) {
|
|
41
|
-
|
|
42
|
-
const { password, ...otherData } = data;
|
|
43
|
-
const user = (await this.first(otherData));
|
|
44
|
-
if (!user) {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
// now verify the password
|
|
48
|
-
if (!user.confirmPassword(password)) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
return user;
|
|
50
|
+
return auth_service.authService.attemptLogin(this, data);
|
|
52
51
|
}
|
|
53
52
|
/**
|
|
54
53
|
* Confirm password
|
|
55
54
|
*/
|
|
56
|
-
confirmPassword(password
|
|
57
|
-
return
|
|
55
|
+
confirmPassword(password) {
|
|
56
|
+
return auth_service.authService.verifyPassword(this.get("password"), password);
|
|
58
57
|
}
|
|
59
58
|
}exports.Auth=Auth;//# sourceMappingURL=auth.js.map
|
package/cjs/models/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sources":["../../src/models/auth.ts"],"sourcesContent":[null],"names":["Model","
|
|
1
|
+
{"version":3,"file":"auth.js","sources":["../../src/models/auth.ts"],"sourcesContent":[null],"names":["Model","authService"],"mappings":"gRAMM,MAAgB,IAAK,SAAQA,aAAK,CAAA;AAMtC;;AAEG;IACI,kBAAkB,GAAA;;QAEvB,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC5D,QAAA,OAAO,WAAW,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;KAClD;AAED;;AAEG;IACI,MAAM,eAAe,CAAC,UAAuB,EAAA;QAClD,OAAOC,wBAAW,CAAC,eAAe,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;KACtD;AAED;;AAEG;IACI,MAAM,mBAAmB,CAAC,IAAU,EAAA;QACzC,OAAOA,wBAAW,CAAC,mBAAmB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;KACpD;AAED;;AAEG;IACI,MAAM,oBAAoB,CAAC,UAAuB,EAAA;QACvD,OAAOA,wBAAW,CAAC,kBAAkB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;KACzD;AAED;;AAEG;IACI,MAAM,iBAAiB,CAAC,KAAa,EAAA;QAC1C,OAAOA,wBAAW,CAAC,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;KACnD;AAED;;AAEG;AACI,IAAA,MAAM,eAAe,GAAA;AAC1B,QAAA,OAAOA,wBAAW,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;KAC1C;AAED;;AAEG;AACI,IAAA,MAAM,cAAc,GAAA;AACzB,QAAA,OAAOA,wBAAW,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;KAC5C;AAED;;AAEG;AACI,IAAA,aAAa,OAAO,CAAyB,IAAS,EAAA;QAC3D,OAAOA,wBAAW,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;KAC7C;AAED;;AAEG;AACI,IAAA,eAAe,CAAC,QAAgB,EAAA;AACrC,QAAA,OAAOA,wBAAW,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,CAAC;KACnE;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cast-password.d.ts","sourceRoot":"","sources":["../../../src/models/casts/cast-password.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cast-password.d.ts","sourceRoot":"","sources":["../../../src/models/casts/cast-password.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAGjD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,OAIpE"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var password=require('@mongez/password'),core=require('@warlock.js/core');/**
|
|
2
2
|
* Cast password on model save
|
|
3
3
|
* If the password is not changed, keep it as is
|
|
4
4
|
*/
|
|
5
5
|
function castPassword(value, column, model) {
|
|
6
6
|
return value
|
|
7
|
-
? password.hash(String(value),
|
|
7
|
+
? password.hash(String(value), core.config.key("auth.password.salt", 12))
|
|
8
8
|
: model.getInitial(column);
|
|
9
9
|
}exports.castPassword=castPassword;//# sourceMappingURL=cast-password.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cast-password.js","sources":["../../../src/models/casts/cast-password.ts"],"sourcesContent":[null],"names":["hash","config"],"mappings":"
|
|
1
|
+
{"version":3,"file":"cast-password.js","sources":["../../../src/models/casts/cast-password.ts"],"sourcesContent":[null],"names":["hash","config"],"mappings":"uFAIA;;;AAGG;SACa,YAAY,CAAC,KAAU,EAAE,MAAc,EAAE,KAAY,EAAA;AACnE,IAAA,OAAO,KAAK;AACV,UAAEA,aAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAEC,WAAM,CAAC,GAAG,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC;AAC3D,UAAE,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;AAC/B"}
|
package/cjs/models/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/models/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/models/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/models/refresh-token/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.d.ts","sourceRoot":"","sources":["../../../src/models/refresh-token/migration.ts"],"names":[],"mappings":";AAGA,wBAeG"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Model, type Casts } from "@warlock.js/cascade";
|
|
2
|
+
export declare class RefreshToken extends Model {
|
|
3
|
+
/**
|
|
4
|
+
* {@inheritDoc}
|
|
5
|
+
*/
|
|
6
|
+
static collection: string;
|
|
7
|
+
/**
|
|
8
|
+
* {@inheritDoc}
|
|
9
|
+
*/
|
|
10
|
+
protected casts: Casts;
|
|
11
|
+
/**
|
|
12
|
+
* Check if token is expired
|
|
13
|
+
*/
|
|
14
|
+
get isExpired(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Check if token is revoked
|
|
17
|
+
*/
|
|
18
|
+
get isRevoked(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Check if token is valid (not expired and not revoked)
|
|
21
|
+
*/
|
|
22
|
+
get isValid(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Revoke this token
|
|
25
|
+
*/
|
|
26
|
+
revoke(): Promise<this>;
|
|
27
|
+
/**
|
|
28
|
+
* Mark token as used (update lastUsedAt)
|
|
29
|
+
*/
|
|
30
|
+
markAsUsed(): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=refresh-token.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refresh-token.d.ts","sourceRoot":"","sources":["../../../src/models/refresh-token/refresh-token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAExD,qBAAa,YAAa,SAAQ,KAAK;IACrC;;OAEG;IACH,OAAc,UAAU,SAAmB;IAE3C;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK,CASpB;IAEF;;OAEG;IACH,IAAW,SAAS,IAAI,OAAO,CAI9B;IAED;;OAEG;IACH,IAAW,SAAS,IAAI,OAAO,CAE9B;IAED;;OAEG;IACH,IAAW,OAAO,IAAI,OAAO,CAE5B;IAED;;OAEG;IACU,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAIpC;;OAEG;IACU,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;CAGzC"}
|