@venturekit/auth 0.0.0-dev.20260415181804 → 0.0.0-dev.20260427211132
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/cognito/config.d.ts +8 -1
- package/dist/cognito/config.d.ts.map +1 -1
- package/dist/cognito/config.js +9 -2
- package/dist/cognito/config.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/session/index.d.ts +46 -12
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +69 -16
- package/dist/session/index.js.map +1 -1
- package/package.json +2 -2
package/dist/cognito/config.d.ts
CHANGED
|
@@ -15,7 +15,14 @@ export declare const DEFAULT_MFA_CONFIG: MfaConfig;
|
|
|
15
15
|
*/
|
|
16
16
|
export declare const DEFAULT_TOKEN_CONFIG: TokenConfig;
|
|
17
17
|
/**
|
|
18
|
-
* Default Cognito configuration
|
|
18
|
+
* Default Cognito configuration.
|
|
19
|
+
*
|
|
20
|
+
* Security-first defaults:
|
|
21
|
+
* - `selfSignUpEnabled: false` — production apps typically invite users
|
|
22
|
+
* explicitly. Opt in via `{ selfSignUpEnabled: true }` if your product is
|
|
23
|
+
* consumer-facing and anyone can register.
|
|
24
|
+
* - MFA optional (TOTP) — nudges users without forcing them.
|
|
25
|
+
* - Password policy 8+ chars with mixed case and digits; symbols opt-in.
|
|
19
26
|
*/
|
|
20
27
|
export declare const DEFAULT_COGNITO_CONFIG: CognitoConfig;
|
|
21
28
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/cognito/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE/F;;GAEG;AACH,eAAO,MAAM,uBAAuB,EAAE,cAOrC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,SAGhC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,WAIlC,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/cognito/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE/F;;GAEG;AACH,eAAO,MAAM,uBAAuB,EAAE,cAOrC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,SAGhC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,WAIlC,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,sBAAsB,EAAE,aAapC,CAAC;AAEF;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG;IAC/B,cAAc,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;IACzC,GAAG,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;IACzB,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;CAC/B,GACA,aAAa,CAuBf"}
|
package/dist/cognito/config.js
CHANGED
|
@@ -28,7 +28,14 @@ export const DEFAULT_TOKEN_CONFIG = {
|
|
|
28
28
|
refreshTokenValidityDays: 30,
|
|
29
29
|
};
|
|
30
30
|
/**
|
|
31
|
-
* Default Cognito configuration
|
|
31
|
+
* Default Cognito configuration.
|
|
32
|
+
*
|
|
33
|
+
* Security-first defaults:
|
|
34
|
+
* - `selfSignUpEnabled: false` — production apps typically invite users
|
|
35
|
+
* explicitly. Opt in via `{ selfSignUpEnabled: true }` if your product is
|
|
36
|
+
* consumer-facing and anyone can register.
|
|
37
|
+
* - MFA optional (TOTP) — nudges users without forcing them.
|
|
38
|
+
* - Password policy 8+ chars with mixed case and digits; symbols opt-in.
|
|
32
39
|
*/
|
|
33
40
|
export const DEFAULT_COGNITO_CONFIG = {
|
|
34
41
|
signInAliases: {
|
|
@@ -36,7 +43,7 @@ export const DEFAULT_COGNITO_CONFIG = {
|
|
|
36
43
|
phone: false,
|
|
37
44
|
username: false,
|
|
38
45
|
},
|
|
39
|
-
selfSignUpEnabled:
|
|
46
|
+
selfSignUpEnabled: false,
|
|
40
47
|
autoVerifyEmail: true,
|
|
41
48
|
autoVerifyPhone: false,
|
|
42
49
|
passwordPolicy: DEFAULT_PASSWORD_POLICY,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/cognito/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAmB;IACrD,SAAS,EAAE,CAAC;IACZ,gBAAgB,EAAE,IAAI;IACtB,gBAAgB,EAAE,IAAI;IACtB,cAAc,EAAE,IAAI;IACpB,cAAc,EAAE,KAAK;IACrB,wBAAwB,EAAE,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAc;IAC3C,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,CAAC,MAAM,CAAC;CAClB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C,0BAA0B,EAAE,EAAE;IAC9B,sBAAsB,EAAE,EAAE;IAC1B,wBAAwB,EAAE,EAAE;CAC7B,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/cognito/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAmB;IACrD,SAAS,EAAE,CAAC;IACZ,gBAAgB,EAAE,IAAI;IACtB,gBAAgB,EAAE,IAAI;IACtB,cAAc,EAAE,IAA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export { createCognitoConfig, DEFAULT_COGNITO_CONFIG, buildUserPoolConfig, } from './cognito/index.js';
|
|
8
8
|
export type { UserPoolOutputs, UserPoolInfraConfig } from './cognito/user-pool.js';
|
|
9
9
|
export { hasScope, hasAnyScope, hasAllScopes, getScopesForRoles, validateRolesConfig, } from './roles/index.js';
|
|
10
|
-
export {
|
|
10
|
+
export { decodeTokenUnsafe, decodeToken, // deprecated alias of decodeTokenUnsafe
|
|
11
|
+
verifyAndDecode, extractUserFromToken, isTokenExpired, getTokenExpiry, } from './session/index.js';
|
|
11
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,cAAc,kBAAkB,CAAC;AAGjC,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAGnF,OAAO,EACL,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,WAAW,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,cAAc,kBAAkB,CAAC;AAGjC,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAGnF,OAAO,EACL,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,iBAAiB,EACjB,WAAW,EAAE,wCAAwC;AACrD,eAAe,EACf,oBAAoB,EACpB,cAAc,EACd,cAAc,GACf,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,5 +10,6 @@ export { createCognitoConfig, DEFAULT_COGNITO_CONFIG, buildUserPoolConfig, } fro
|
|
|
10
10
|
// Roles
|
|
11
11
|
export { hasScope, hasAnyScope, hasAllScopes, getScopesForRoles, validateRolesConfig, } from './roles/index.js';
|
|
12
12
|
// Session
|
|
13
|
-
export {
|
|
13
|
+
export { decodeTokenUnsafe, decodeToken, // deprecated alias of decodeTokenUnsafe
|
|
14
|
+
verifyAndDecode, extractUserFromToken, isTokenExpired, getTokenExpiry, } from './session/index.js';
|
|
14
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,QAAQ;AACR,cAAc,kBAAkB,CAAC;AAEjC,UAAU;AACV,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAG5B,QAAQ;AACR,OAAO,EACL,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAE1B,UAAU;AACV,OAAO,EACL,WAAW,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,QAAQ;AACR,cAAc,kBAAkB,CAAC;AAEjC,UAAU;AACV,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAG5B,QAAQ;AACR,OAAO,EACL,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAE1B,UAAU;AACV,OAAO,EACL,iBAAiB,EACjB,WAAW,EAAE,wCAAwC;AACrD,eAAe,EACf,oBAAoB,EACpB,cAAc,EACd,cAAc,GACf,MAAM,oBAAoB,CAAC"}
|
package/dist/session/index.d.ts
CHANGED
|
@@ -5,29 +5,63 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { User } from '../types/index.js';
|
|
7
7
|
/**
|
|
8
|
-
* Decode JWT token payload
|
|
8
|
+
* Decode JWT token payload WITHOUT verifying the signature.
|
|
9
9
|
*
|
|
10
|
-
* ⚠️ SECURITY
|
|
11
|
-
*
|
|
10
|
+
* ⚠️ SECURITY: This is deliberately named `decodeTokenUnsafe` so reviewers
|
|
11
|
+
* spot authentication bypasses in code review. **Never** feed the returned
|
|
12
|
+
* claims into an authorization decision without first calling
|
|
13
|
+
* {@link verifyAndDecode} or relying on API Gateway / ALB to do the
|
|
14
|
+
* verification for you.
|
|
12
15
|
*
|
|
13
|
-
*
|
|
14
|
-
* -
|
|
15
|
-
* -
|
|
16
|
-
* -
|
|
16
|
+
* Legitimate use cases:
|
|
17
|
+
* - Reading non-sensitive metadata (issuer, kid) to look up the right JWKS.
|
|
18
|
+
* - Inspecting a token the server already verified once upstream.
|
|
19
|
+
* - Extracting the TTL for local caches.
|
|
20
|
+
*/
|
|
21
|
+
export declare function decodeTokenUnsafe(token: string): Record<string, unknown> | null;
|
|
22
|
+
/**
|
|
23
|
+
* Deprecated alias for {@link decodeTokenUnsafe}. Kept for backward
|
|
24
|
+
* compatibility; prefer the explicit `Unsafe` suffix so reviewers can spot
|
|
25
|
+
* authentication bypasses at a glance.
|
|
17
26
|
*
|
|
18
|
-
*
|
|
27
|
+
* @deprecated Use `decodeTokenUnsafe` or `verifyAndDecode`.
|
|
19
28
|
*/
|
|
20
|
-
export declare
|
|
29
|
+
export declare const decodeToken: typeof decodeTokenUnsafe;
|
|
21
30
|
/**
|
|
22
|
-
*
|
|
31
|
+
* Verify a Cognito/JWKS-signed JWT and return its claims, or `null` if the
|
|
32
|
+
* token is invalid/expired/untrusted. Uses `aws-jwt-verify` under the hood,
|
|
33
|
+
* which is lazily imported so the dependency is optional.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const claims = await verifyAndDecode(token, {
|
|
38
|
+
* userPoolId: process.env.COGNITO_USER_POOL_ID!,
|
|
39
|
+
* clientId: process.env.COGNITO_CLIENT_ID!,
|
|
40
|
+
* tokenUse: 'access',
|
|
41
|
+
* });
|
|
42
|
+
* if (!claims) throw new UnauthorizedError();
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* Requires `aws-jwt-verify` to be installed at the consumer site.
|
|
46
|
+
*/
|
|
47
|
+
export declare function verifyAndDecode(token: string, options: {
|
|
48
|
+
userPoolId: string;
|
|
49
|
+
clientId?: string | string[];
|
|
50
|
+
tokenUse?: 'access' | 'id';
|
|
51
|
+
}): Promise<Record<string, unknown> | null>;
|
|
52
|
+
/**
|
|
53
|
+
* Extract user from ID token claims.
|
|
54
|
+
*
|
|
55
|
+
* ⚠️ Uses the unverified decoder. Callers MUST have already verified the
|
|
56
|
+
* token (e.g. via API Gateway Cognito Authorizer or {@link verifyAndDecode}).
|
|
23
57
|
*/
|
|
24
58
|
export declare function extractUserFromToken(idToken: string): User | null;
|
|
25
59
|
/**
|
|
26
|
-
* Check if a token is expired
|
|
60
|
+
* Check if a token is expired (based on its `exp` claim, without signature verification).
|
|
27
61
|
*/
|
|
28
62
|
export declare function isTokenExpired(token: string): boolean;
|
|
29
63
|
/**
|
|
30
|
-
* Get token expiry time
|
|
64
|
+
* Get token expiry time (from the `exp` claim, without signature verification).
|
|
31
65
|
*/
|
|
32
66
|
export declare function getTokenExpiry(token: string): Date | null;
|
|
33
67
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAkB,MAAM,mBAAmB,CAAC;AAE9D
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAkB,MAAM,mBAAmB,CAAC;AAE9D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAW/E;AAED;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,0BAAoB,CAAC;AAE7C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,eAAe,CACnC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;IACP,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC7B,QAAQ,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;CAC5B,GACA,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CA0BzC;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAwBjE;AAuDD;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAMrD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAKzD"}
|
package/dist/session/index.js
CHANGED
|
@@ -4,19 +4,20 @@
|
|
|
4
4
|
* Utilities for managing user sessions and tokens.
|
|
5
5
|
*/
|
|
6
6
|
/**
|
|
7
|
-
* Decode JWT token payload
|
|
7
|
+
* Decode JWT token payload WITHOUT verifying the signature.
|
|
8
8
|
*
|
|
9
|
-
* ⚠️ SECURITY
|
|
10
|
-
*
|
|
9
|
+
* ⚠️ SECURITY: This is deliberately named `decodeTokenUnsafe` so reviewers
|
|
10
|
+
* spot authentication bypasses in code review. **Never** feed the returned
|
|
11
|
+
* claims into an authorization decision without first calling
|
|
12
|
+
* {@link verifyAndDecode} or relying on API Gateway / ALB to do the
|
|
13
|
+
* verification for you.
|
|
11
14
|
*
|
|
12
|
-
*
|
|
13
|
-
* -
|
|
14
|
-
* -
|
|
15
|
-
* -
|
|
16
|
-
*
|
|
17
|
-
* Never trust decoded claims for authorization without proper verification.
|
|
15
|
+
* Legitimate use cases:
|
|
16
|
+
* - Reading non-sensitive metadata (issuer, kid) to look up the right JWKS.
|
|
17
|
+
* - Inspecting a token the server already verified once upstream.
|
|
18
|
+
* - Extracting the TTL for local caches.
|
|
18
19
|
*/
|
|
19
|
-
export function
|
|
20
|
+
export function decodeTokenUnsafe(token) {
|
|
20
21
|
try {
|
|
21
22
|
const parts = token.split('.');
|
|
22
23
|
if (parts.length !== 3)
|
|
@@ -30,10 +31,62 @@ export function decodeToken(token) {
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
/**
|
|
33
|
-
*
|
|
34
|
+
* Deprecated alias for {@link decodeTokenUnsafe}. Kept for backward
|
|
35
|
+
* compatibility; prefer the explicit `Unsafe` suffix so reviewers can spot
|
|
36
|
+
* authentication bypasses at a glance.
|
|
37
|
+
*
|
|
38
|
+
* @deprecated Use `decodeTokenUnsafe` or `verifyAndDecode`.
|
|
39
|
+
*/
|
|
40
|
+
export const decodeToken = decodeTokenUnsafe;
|
|
41
|
+
/**
|
|
42
|
+
* Verify a Cognito/JWKS-signed JWT and return its claims, or `null` if the
|
|
43
|
+
* token is invalid/expired/untrusted. Uses `aws-jwt-verify` under the hood,
|
|
44
|
+
* which is lazily imported so the dependency is optional.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const claims = await verifyAndDecode(token, {
|
|
49
|
+
* userPoolId: process.env.COGNITO_USER_POOL_ID!,
|
|
50
|
+
* clientId: process.env.COGNITO_CLIENT_ID!,
|
|
51
|
+
* tokenUse: 'access',
|
|
52
|
+
* });
|
|
53
|
+
* if (!claims) throw new UnauthorizedError();
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* Requires `aws-jwt-verify` to be installed at the consumer site.
|
|
57
|
+
*/
|
|
58
|
+
export async function verifyAndDecode(token, options) {
|
|
59
|
+
let CognitoJwtVerifier;
|
|
60
|
+
try {
|
|
61
|
+
// Lazy, peer-optional import — consumers only pay the cost if they use it.
|
|
62
|
+
({ CognitoJwtVerifier } = await import(
|
|
63
|
+
/* @vite-ignore */ 'aws-jwt-verify'));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new Error("verifyAndDecode requires 'aws-jwt-verify'. " +
|
|
67
|
+
"Install it with: pnpm add aws-jwt-verify");
|
|
68
|
+
}
|
|
69
|
+
const verifier = CognitoJwtVerifier.create({
|
|
70
|
+
userPoolId: options.userPoolId,
|
|
71
|
+
tokenUse: options.tokenUse ?? 'access',
|
|
72
|
+
clientId: options.clientId ?? null,
|
|
73
|
+
});
|
|
74
|
+
try {
|
|
75
|
+
const payload = await verifier.verify(token);
|
|
76
|
+
return payload;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Extract user from ID token claims.
|
|
84
|
+
*
|
|
85
|
+
* ⚠️ Uses the unverified decoder. Callers MUST have already verified the
|
|
86
|
+
* token (e.g. via API Gateway Cognito Authorizer or {@link verifyAndDecode}).
|
|
34
87
|
*/
|
|
35
88
|
export function extractUserFromToken(idToken) {
|
|
36
|
-
const claims =
|
|
89
|
+
const claims = decodeTokenUnsafe(idToken);
|
|
37
90
|
if (!claims)
|
|
38
91
|
return null;
|
|
39
92
|
// Determine identifier type and value
|
|
@@ -105,20 +158,20 @@ function extractCustomAttributes(claims) {
|
|
|
105
158
|
return attributes;
|
|
106
159
|
}
|
|
107
160
|
/**
|
|
108
|
-
* Check if a token is expired
|
|
161
|
+
* Check if a token is expired (based on its `exp` claim, without signature verification).
|
|
109
162
|
*/
|
|
110
163
|
export function isTokenExpired(token) {
|
|
111
|
-
const claims =
|
|
164
|
+
const claims = decodeTokenUnsafe(token);
|
|
112
165
|
if (!claims || !claims.exp)
|
|
113
166
|
return true;
|
|
114
167
|
const expiry = claims.exp * 1000;
|
|
115
168
|
return Date.now() >= expiry;
|
|
116
169
|
}
|
|
117
170
|
/**
|
|
118
|
-
* Get token expiry time
|
|
171
|
+
* Get token expiry time (from the `exp` claim, without signature verification).
|
|
119
172
|
*/
|
|
120
173
|
export function getTokenExpiry(token) {
|
|
121
|
-
const claims =
|
|
174
|
+
const claims = decodeTokenUnsafe(token);
|
|
122
175
|
if (!claims || !claims.exp)
|
|
123
176
|
return null;
|
|
124
177
|
return new Date(claims.exp * 1000);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC7C,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC/B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAEpC,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACzB,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACpE,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;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AAa;IAC1C,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACxC,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG;QAAE,OAAO,IAAI,CAAC;IAExC,OAAO,IAAI,IAAI,CAAE,MAAM,CAAC,GAAc,GAAG,IAAI,CAAC,CAAC;AACjD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venturekit/auth",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.20260427211132",
|
|
4
4
|
"description": "Authentication and authorization for VentureKit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@venturekit/core": "0.0.0-dev.
|
|
28
|
+
"@venturekit/core": "0.0.0-dev.20260427211132"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^20.10.0",
|