@eventuras/fides-auth-next 0.2.0 → 0.6.0
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/LICENSE +21 -674
- package/README.md +6 -1
- package/dist/cookie-store.d.ts +10 -0
- package/dist/cookie-store.d.ts.map +1 -0
- package/dist/cookie-store.js +25 -0
- package/dist/cookie-store.js.map +1 -0
- package/dist/cookies.d.ts +18 -41
- package/dist/cookies.d.ts.map +1 -1
- package/dist/cookies.js +31 -29
- package/dist/cookies.js.map +1 -1
- package/dist/heartbeat-handler.d.ts +1 -1
- package/dist/heartbeat-handler.d.ts.map +1 -1
- package/dist/heartbeat-handler.js +12 -36
- package/dist/heartbeat-handler.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -11
- package/dist/oidc-callback.d.ts +5 -5
- package/dist/oidc-callback.d.ts.map +1 -1
- package/dist/oidc-callback.js +16 -38
- package/dist/oidc-callback.js.map +1 -1
- package/dist/oidc-login.d.ts.map +1 -1
- package/dist/oidc-login.js +12 -24
- package/dist/oidc-login.js.map +1 -1
- package/dist/oidc-logout.d.ts +44 -0
- package/dist/oidc-logout.d.ts.map +1 -0
- package/dist/oidc-logout.js +44 -0
- package/dist/oidc-logout.js.map +1 -0
- package/dist/request.js.map +1 -1
- package/dist/session.d.ts +24 -62
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +30 -132
- package/dist/session.js.map +1 -1
- package/dist/store/index.d.ts +2 -7
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +2 -6
- package/package.json +22 -21
- package/dist/store/configure-logger.d.ts +0 -15
- package/dist/store/configure-logger.d.ts.map +0 -1
- package/dist/store/configure-logger.js +0 -25
- package/dist/store/configure-logger.js.map +0 -1
- package/dist/store/hooks.d.ts +0 -64
- package/dist/store/hooks.d.ts.map +0 -1
- package/dist/store/hooks.js +0 -78
- package/dist/store/hooks.js.map +0 -1
- package/dist/store/session-monitor.d.ts +0 -51
- package/dist/store/session-monitor.d.ts.map +0 -1
- package/dist/store/session-monitor.js +0 -92
- package/dist/store/session-monitor.js.map +0 -1
- package/dist/store/store.d.ts +0 -83
- package/dist/store/store.d.ts.map +0 -1
- package/dist/store/store.js +0 -218
- package/dist/store/store.js.map +0 -1
- package/dist/store/use-heartbeat.d.ts +0 -65
- package/dist/store/use-heartbeat.d.ts.map +0 -1
- package/dist/store/use-heartbeat.js +0 -148
- package/dist/store/use-heartbeat.js.map +0 -1
- package/dist/store/use-session-monitor.d.ts +0 -28
- package/dist/store/use-session-monitor.d.ts.map +0 -1
- package/dist/store/use-session-monitor.js +0 -40
- package/dist/store/use-session-monitor.js.map +0 -1
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ const { logout } = useAuthActions();
|
|
|
40
40
|
- **XState Store**: Simple auth state management with automatic session monitoring
|
|
41
41
|
- **Cookie Helpers**: Server-side cookie operations
|
|
42
42
|
- **Session Management**: High-level session functions
|
|
43
|
-
- **OAuth**: Complete OAuth flow with PKCE
|
|
43
|
+
- **OAuth**: Complete OAuth flow with PKCE and RP-initiated logout
|
|
44
44
|
- **TypeScript**: Full type safety
|
|
45
45
|
|
|
46
46
|
## Core Functions
|
|
@@ -55,6 +55,11 @@ import {
|
|
|
55
55
|
getSessionCookie,
|
|
56
56
|
} from "@eventuras/fides-auth-next";
|
|
57
57
|
|
|
58
|
+
// Route handlers
|
|
59
|
+
import { handleOidcLogin } from "@eventuras/fides-auth-next/oidc-login";
|
|
60
|
+
import { handleOidcCallback } from "@eventuras/fides-auth-next/oidc-callback";
|
|
61
|
+
import { handleOidcLogout } from "@eventuras/fides-auth-next/oidc-logout";
|
|
62
|
+
|
|
58
63
|
// Client-side
|
|
59
64
|
import { useAuthStore, useAuthActions } from "./auth-store";
|
|
60
65
|
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CookieStore } from '@eventuras/fides-auth/server';
|
|
2
|
+
/**
|
|
3
|
+
* Builds a {@link CookieStore} backed by Next.js's request cookie store, for the
|
|
4
|
+
* framework-agnostic server helpers in `@eventuras/fides-auth/server`.
|
|
5
|
+
*
|
|
6
|
+
* Must be called where `next/headers` `cookies()` is available: Server Actions,
|
|
7
|
+
* Route Handlers, or Server Components.
|
|
8
|
+
*/
|
|
9
|
+
export declare function nextCookieStore(): Promise<CookieStore>;
|
|
10
|
+
//# sourceMappingURL=cookie-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookie-store.d.ts","sourceRoot":"","sources":["../src/cookie-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAGhE;;;;;;GAMG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,WAAW,CAAC,CAY5D"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { cookies } from "next/headers";
|
|
2
|
+
//#region src/cookie-store.ts
|
|
3
|
+
/**
|
|
4
|
+
* Builds a {@link CookieStore} backed by Next.js's request cookie store, for the
|
|
5
|
+
* framework-agnostic server helpers in `@eventuras/fides-auth/server`.
|
|
6
|
+
*
|
|
7
|
+
* Must be called where `next/headers` `cookies()` is available: Server Actions,
|
|
8
|
+
* Route Handlers, or Server Components.
|
|
9
|
+
*/
|
|
10
|
+
async function nextCookieStore() {
|
|
11
|
+
const store = await cookies();
|
|
12
|
+
return {
|
|
13
|
+
get: (name) => store.get(name)?.value ?? null,
|
|
14
|
+
set: (name, value, options) => {
|
|
15
|
+
store.set(name, value, options);
|
|
16
|
+
},
|
|
17
|
+
delete: (name) => {
|
|
18
|
+
store.delete(name);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { nextCookieStore };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=cookie-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookie-store.js","names":[],"sources":["../src/cookie-store.ts"],"sourcesContent":["import type { CookieStore } from '@eventuras/fides-auth/server';\nimport { cookies } from 'next/headers';\n\n/**\n * Builds a {@link CookieStore} backed by Next.js's request cookie store, for the\n * framework-agnostic server helpers in `@eventuras/fides-auth/server`.\n *\n * Must be called where `next/headers` `cookies()` is available: Server Actions,\n * Route Handlers, or Server Components.\n */\nexport async function nextCookieStore(): Promise<CookieStore> {\n const store = await cookies();\n const cookieStore: CookieStore = {\n get: (name) => store.get(name)?.value ?? null,\n set: (name, value, options) => {\n store.set(name, value, options);\n },\n delete: (name) => {\n store.delete(name);\n },\n };\n return cookieStore;\n}\n"],"mappings":";;;;;;;;;AAUA,eAAsB,kBAAwC;CAC5D,MAAM,QAAQ,MAAM,QAAQ;CAU5B,OAAO;EARL,MAAM,SAAS,MAAM,IAAI,IAAI,CAAC,EAAE,SAAS;EACzC,MAAM,MAAM,OAAO,YAAY;GAC7B,MAAM,IAAI,MAAM,OAAO,OAAO;EAChC;EACA,SAAS,SAAS;GAChB,MAAM,OAAO,IAAI;EACnB;CAEK;AACT"}
|
package/dist/cookies.d.ts
CHANGED
|
@@ -1,44 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* IMPORTANT: These functions can ONLY be used in:
|
|
5
|
-
* - Server Actions (functions marked with 'use server')
|
|
6
|
-
* - Route Handlers (files in app/api directory)
|
|
7
|
-
* - Server Components (async React components)
|
|
8
|
-
*
|
|
9
|
-
* They CANNOT be used in:
|
|
10
|
-
* - Client Components (marked with 'use client')
|
|
11
|
-
* - Middleware (use NextResponse.cookies instead)
|
|
12
|
-
* - Edge Runtime without proper configuration
|
|
13
|
-
*/
|
|
14
|
-
export interface CookieOptions {
|
|
15
|
-
/** Cookie path (default: '/') */
|
|
16
|
-
path?: string;
|
|
17
|
-
/** Max age in seconds */
|
|
18
|
-
maxAge?: number;
|
|
19
|
-
/** SameSite policy (default: 'lax') */
|
|
20
|
-
sameSite?: 'strict' | 'lax' | 'none';
|
|
21
|
-
/** HTTP only flag (default: true) */
|
|
22
|
-
httpOnly?: boolean;
|
|
23
|
-
/** Secure flag (default: true in production) */
|
|
24
|
-
secure?: boolean;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Default cookie options for session cookies.
|
|
28
|
-
* - Secure in production
|
|
29
|
-
* - HTTP only
|
|
30
|
-
* - Lax same-site policy
|
|
31
|
-
* - 30 days max age
|
|
32
|
-
*/
|
|
33
|
-
export declare const defaultSessionCookieOptions: CookieOptions;
|
|
34
|
-
/**
|
|
35
|
-
* Default cookie options for OAuth state/PKCE cookies.
|
|
36
|
-
* - Secure in production
|
|
37
|
-
* - HTTP only
|
|
38
|
-
* - Lax same-site policy
|
|
39
|
-
* - 10 minutes max age (short-lived for security)
|
|
40
|
-
*/
|
|
41
|
-
export declare const defaultOAuthCookieOptions: CookieOptions;
|
|
1
|
+
import { CookieOptions } from '@eventuras/fides-auth/cookies';
|
|
2
|
+
export { COOKIE_INFO_BYTES, COOKIE_MAX_BYTES, CookieTooLargeError, ACCESS_TOKEN_COOKIE_NAME, ID_TOKEN_COOKIE_NAME, defaultSessionCookieOptions, defaultOAuthCookieOptions, cookieByteSize, assertCookieWithinLimit, } from '@eventuras/fides-auth/cookies';
|
|
3
|
+
export type { CookieOptions } from '@eventuras/fides-auth/cookies';
|
|
42
4
|
/**
|
|
43
5
|
* Sets a cookie with the given name and value.
|
|
44
6
|
*
|
|
@@ -113,6 +75,21 @@ export declare function deleteAuthCookies(names: string[]): Promise<void>;
|
|
|
113
75
|
* ```
|
|
114
76
|
*/
|
|
115
77
|
export declare function setSessionCookie(encryptedJwt: string, options?: Partial<CookieOptions>): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Sets the access-token cookie ("session_at") with session cookie settings.
|
|
80
|
+
*
|
|
81
|
+
* @param encryptedJwt - The encrypted JWT wrapping the access token
|
|
82
|
+
* @param options - Optional cookie options (merged with session defaults)
|
|
83
|
+
*/
|
|
84
|
+
export declare function setAccessTokenCookie(encryptedJwt: string, options?: Partial<CookieOptions>): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the access-token cookie ("session_at") value, or null if absent.
|
|
87
|
+
*/
|
|
88
|
+
export declare function getAccessTokenCookie(): Promise<string | null>;
|
|
89
|
+
/**
|
|
90
|
+
* Deletes the access-token cookie ("session_at").
|
|
91
|
+
*/
|
|
92
|
+
export declare function deleteAccessTokenCookie(): Promise<void>;
|
|
116
93
|
/**
|
|
117
94
|
* Gets the current session cookie value.
|
|
118
95
|
* Returns null if no session cookie exists.
|
package/dist/cookies.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../src/cookies.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;
|
|
1
|
+
{"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../src/cookies.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAQH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAQnE,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,wBAAwB,EACxB,oBAAoB,EACpB,2BAA2B,EAC3B,yBAAyB,EACzB,cAAc,EACd,uBAAuB,GACxB,MAAM,+BAA+B,CAAC;AACvC,YAAY,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAInE;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,aAA2C,GACnD,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAgBxE;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAUlE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAatE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,gBAAgB,CACpC,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM,GACnC,OAAO,CAAC,IAAI,CAAC,CAOf;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACxC,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM,GACnC,OAAO,CAAC,IAAI,CAAC,CAKf;AAED;;GAEG;AACH,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAEnE;AAED;;GAEG;AACH,wBAAsB,uBAAuB,IAAI,OAAO,CAAC,IAAI,CAAC,CAE7D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAE/D;AAED;;;;;;;;;GASG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAGzD"}
|
package/dist/cookies.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cookies } from "next/headers";
|
|
2
2
|
import { Logger } from "@eventuras/logger";
|
|
3
|
+
import { ACCESS_TOKEN_COOKIE_NAME, ACCESS_TOKEN_COOKIE_NAME as ACCESS_TOKEN_COOKIE_NAME$1, COOKIE_INFO_BYTES, COOKIE_INFO_BYTES as COOKIE_INFO_BYTES$1, COOKIE_MAX_BYTES, CookieTooLargeError, ID_TOKEN_COOKIE_NAME, assertCookieWithinLimit, assertCookieWithinLimit as assertCookieWithinLimit$1, cookieByteSize, defaultOAuthCookieOptions, defaultSessionCookieOptions, defaultSessionCookieOptions as defaultSessionCookieOptions$1 } from "@eventuras/fides-auth/cookies";
|
|
3
4
|
//#region src/cookies.ts
|
|
4
5
|
/**
|
|
5
6
|
* Cookie helpers for Next.js authentication flows.
|
|
@@ -16,34 +17,6 @@ import { Logger } from "@eventuras/logger";
|
|
|
16
17
|
*/
|
|
17
18
|
var logger = Logger.create({ namespace: "fides-auth-next:cookies" });
|
|
18
19
|
/**
|
|
19
|
-
* Default cookie options for session cookies.
|
|
20
|
-
* - Secure in production
|
|
21
|
-
* - HTTP only
|
|
22
|
-
* - Lax same-site policy
|
|
23
|
-
* - 30 days max age
|
|
24
|
-
*/
|
|
25
|
-
var defaultSessionCookieOptions = {
|
|
26
|
-
path: "/",
|
|
27
|
-
maxAge: 3600 * 24 * 30,
|
|
28
|
-
sameSite: "lax",
|
|
29
|
-
httpOnly: true,
|
|
30
|
-
secure: process.env.NODE_ENV === "production"
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Default cookie options for OAuth state/PKCE cookies.
|
|
34
|
-
* - Secure in production
|
|
35
|
-
* - HTTP only
|
|
36
|
-
* - Lax same-site policy
|
|
37
|
-
* - 10 minutes max age (short-lived for security)
|
|
38
|
-
*/
|
|
39
|
-
var defaultOAuthCookieOptions = {
|
|
40
|
-
path: "/",
|
|
41
|
-
maxAge: 600,
|
|
42
|
-
sameSite: "lax",
|
|
43
|
-
httpOnly: true,
|
|
44
|
-
secure: process.env.NODE_ENV === "production"
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
20
|
* Sets a cookie with the given name and value.
|
|
48
21
|
*
|
|
49
22
|
* @param name - Cookie name
|
|
@@ -68,6 +41,11 @@ async function setAuthCookie(name, value, options = defaultSessionCookieOptions)
|
|
|
68
41
|
...defaultSessionCookieOptions,
|
|
69
42
|
...options
|
|
70
43
|
};
|
|
44
|
+
const size = assertCookieWithinLimit(name, value);
|
|
45
|
+
if (size >= COOKIE_INFO_BYTES) logger.info({
|
|
46
|
+
cookieName: name,
|
|
47
|
+
size
|
|
48
|
+
}, "Cookie approaching browser size limit");
|
|
71
49
|
cookieStore.set(name, value, cookieOptions);
|
|
72
50
|
logger.debug({
|
|
73
51
|
cookieName: name,
|
|
@@ -183,6 +161,30 @@ async function setSessionCookie(encryptedJwt, options = {}) {
|
|
|
183
161
|
logger.info("Session cookie set");
|
|
184
162
|
}
|
|
185
163
|
/**
|
|
164
|
+
* Sets the access-token cookie ("session_at") with session cookie settings.
|
|
165
|
+
*
|
|
166
|
+
* @param encryptedJwt - The encrypted JWT wrapping the access token
|
|
167
|
+
* @param options - Optional cookie options (merged with session defaults)
|
|
168
|
+
*/
|
|
169
|
+
async function setAccessTokenCookie(encryptedJwt, options = {}) {
|
|
170
|
+
await setAuthCookie(ACCESS_TOKEN_COOKIE_NAME, encryptedJwt, {
|
|
171
|
+
...defaultSessionCookieOptions,
|
|
172
|
+
...options
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Gets the access-token cookie ("session_at") value, or null if absent.
|
|
177
|
+
*/
|
|
178
|
+
async function getAccessTokenCookie() {
|
|
179
|
+
return await getAuthCookie(ACCESS_TOKEN_COOKIE_NAME);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Deletes the access-token cookie ("session_at").
|
|
183
|
+
*/
|
|
184
|
+
async function deleteAccessTokenCookie() {
|
|
185
|
+
await deleteAuthCookie(ACCESS_TOKEN_COOKIE_NAME);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
186
188
|
* Gets the current session cookie value.
|
|
187
189
|
* Returns null if no session cookie exists.
|
|
188
190
|
*
|
|
@@ -215,6 +217,6 @@ async function deleteSessionCookie() {
|
|
|
215
217
|
logger.info("Session cookie deleted");
|
|
216
218
|
}
|
|
217
219
|
//#endregion
|
|
218
|
-
export { defaultOAuthCookieOptions, defaultSessionCookieOptions, deleteAuthCookie, deleteAuthCookies, deleteSessionCookie, getAuthCookie, getSessionCookie, setAuthCookie, setSessionCookie };
|
|
220
|
+
export { ACCESS_TOKEN_COOKIE_NAME$1 as ACCESS_TOKEN_COOKIE_NAME, COOKIE_INFO_BYTES$1 as COOKIE_INFO_BYTES, COOKIE_MAX_BYTES, CookieTooLargeError, ID_TOKEN_COOKIE_NAME, assertCookieWithinLimit$1 as assertCookieWithinLimit, cookieByteSize, defaultOAuthCookieOptions, defaultSessionCookieOptions$1 as defaultSessionCookieOptions, deleteAccessTokenCookie, deleteAuthCookie, deleteAuthCookies, deleteSessionCookie, getAccessTokenCookie, getAuthCookie, getSessionCookie, setAccessTokenCookie, setAuthCookie, setSessionCookie };
|
|
219
221
|
|
|
220
222
|
//# sourceMappingURL=cookies.js.map
|
package/dist/cookies.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.js","names":[],"sources":["../src/cookies.ts"],"sourcesContent":["/**\n * Cookie helpers for Next.js authentication flows.\n *\n * IMPORTANT: These functions can ONLY be used in:\n * - Server Actions (functions marked with 'use server')\n * - Route Handlers (files in app/api directory)\n * - Server Components (async React components)\n *\n * They CANNOT be used in:\n * - Client Components (marked with 'use client')\n * - Middleware (use NextResponse.cookies instead)\n * - Edge Runtime without proper configuration\n */\n\nimport {
|
|
1
|
+
{"version":3,"file":"cookies.js","names":[],"sources":["../src/cookies.ts"],"sourcesContent":["/**\n * Cookie helpers for Next.js authentication flows.\n *\n * IMPORTANT: These functions can ONLY be used in:\n * - Server Actions (functions marked with 'use server')\n * - Route Handlers (files in app/api directory)\n * - Server Components (async React components)\n *\n * They CANNOT be used in:\n * - Client Components (marked with 'use client')\n * - Middleware (use NextResponse.cookies instead)\n * - Edge Runtime without proper configuration\n */\n\nimport {\n ACCESS_TOKEN_COOKIE_NAME,\n COOKIE_INFO_BYTES,\n assertCookieWithinLimit,\n defaultSessionCookieOptions,\n} from '@eventuras/fides-auth/cookies';\nimport type { CookieOptions } from '@eventuras/fides-auth/cookies';\nimport { Logger } from '@eventuras/logger';\nimport { cookies } from 'next/headers';\n\n// Re-export the framework-agnostic cookie attributes, limits, and size guard from\n// the core package so existing import sites keep working. This Next adapter only\n// owns the actual cookie I/O (via next/headers); the values and the size check\n// itself live in @eventuras/fides-auth/cookies.\nexport {\n COOKIE_INFO_BYTES,\n COOKIE_MAX_BYTES,\n CookieTooLargeError,\n ACCESS_TOKEN_COOKIE_NAME,\n ID_TOKEN_COOKIE_NAME,\n defaultSessionCookieOptions,\n defaultOAuthCookieOptions,\n cookieByteSize,\n assertCookieWithinLimit,\n} from '@eventuras/fides-auth/cookies';\nexport type { CookieOptions } from '@eventuras/fides-auth/cookies';\n\nconst logger = Logger.create({ namespace: 'fides-auth-next:cookies' });\n\n/**\n * Sets a cookie with the given name and value.\n *\n * @param name - Cookie name\n * @param value - Cookie value\n * @param options - Cookie options (defaults to session cookie options)\n *\n * @example\n * ```ts\n * // In a server action or route handler\n * await setAuthCookie('session', encryptedJwt);\n *\n * // With custom options\n * await setAuthCookie('oauth_state', state, {\n * maxAge: 60 * 10, // 10 minutes\n * });\n * ```\n */\nexport async function setAuthCookie(\n name: string,\n value: string,\n options: CookieOptions = defaultSessionCookieOptions\n): Promise<void> {\n try {\n const cookieStore = await cookies();\n const cookieOptions = { ...defaultSessionCookieOptions, ...options };\n\n // Fail loudly above the browser per-cookie limit (the browser would otherwise\n // drop the cookie silently). The size check lives in the core package.\n const size = assertCookieWithinLimit(name, value);\n if (size >= COOKIE_INFO_BYTES) {\n logger.info({ cookieName: name, size }, 'Cookie approaching browser size limit');\n }\n\n cookieStore.set(name, value, cookieOptions);\n\n logger.debug({\n cookieName: name,\n maxAge: cookieOptions.maxAge,\n secure: cookieOptions.secure\n }, 'Cookie set successfully');\n } catch (error) {\n logger.error({ error, cookieName: name }, 'Failed to set cookie');\n throw error;\n }\n}\n\n/**\n * Gets a cookie value by name.\n *\n * @param name - Cookie name\n * @returns Cookie value or null if not found\n *\n * @example\n * ```ts\n * // In a server action or route handler\n * const sessionCookie = await getAuthCookie('session');\n * if (sessionCookie) {\n * // Validate and use session\n * }\n * ```\n */\nexport async function getAuthCookie(name: string): Promise<string | null> {\n try {\n const cookieStore = await cookies();\n const cookie = cookieStore.get(name);\n\n if (!cookie?.value) {\n logger.debug({ cookieName: name }, 'Cookie not found');\n return null;\n }\n\n logger.debug({ cookieName: name }, 'Cookie retrieved');\n return cookie.value;\n } catch (error) {\n logger.error({ error, cookieName: name }, 'Failed to get cookie');\n return null;\n }\n}\n\n/**\n * Deletes a cookie by name.\n *\n * @param name - Cookie name to delete\n *\n * @example\n * ```ts\n * // In a server action or route handler\n * await deleteAuthCookie('session');\n * ```\n */\nexport async function deleteAuthCookie(name: string): Promise<void> {\n try {\n const cookieStore = await cookies();\n cookieStore.delete(name);\n\n logger.debug({ cookieName: name }, 'Cookie deleted successfully');\n } catch (error) {\n logger.error({ error, cookieName: name }, 'Failed to delete cookie');\n throw error;\n }\n}\n\n/**\n * Deletes multiple cookies at once.\n * Useful for cleaning up OAuth flow cookies.\n *\n * @param names - Array of cookie names to delete\n *\n * @example\n * ```ts\n * // Clean up OAuth cookies after callback\n * await deleteAuthCookies(['oauth_state', 'oauth_code_verifier', 'returnTo']);\n * ```\n */\nexport async function deleteAuthCookies(names: string[]): Promise<void> {\n try {\n const cookieStore = await cookies();\n\n for (const name of names) {\n cookieStore.delete(name);\n }\n\n logger.debug({ cookieNames: names }, 'Multiple cookies deleted successfully');\n } catch (error) {\n logger.error({ error, cookieNames: names }, 'Failed to delete cookies');\n throw error;\n }\n}\n\n/**\n * Sets the session cookie with proper security settings.\n * This is a convenience wrapper around setAuthCookie.\n *\n * @param encryptedJwt - The encrypted JWT session token\n * @param options - Optional cookie options (merged with defaults)\n *\n * @example\n * ```ts\n * const jwt = await createSession({ ... });\n * await setSessionCookie(jwt);\n * ```\n */\nexport async function setSessionCookie(\n encryptedJwt: string,\n options: Partial<CookieOptions> = {}\n): Promise<void> {\n await setAuthCookie('session', encryptedJwt, {\n ...defaultSessionCookieOptions,\n ...options,\n });\n\n logger.info('Session cookie set');\n}\n\n/**\n * Sets the access-token cookie (\"session_at\") with session cookie settings.\n *\n * @param encryptedJwt - The encrypted JWT wrapping the access token\n * @param options - Optional cookie options (merged with session defaults)\n */\nexport async function setAccessTokenCookie(\n encryptedJwt: string,\n options: Partial<CookieOptions> = {}\n): Promise<void> {\n await setAuthCookie(ACCESS_TOKEN_COOKIE_NAME, encryptedJwt, {\n ...defaultSessionCookieOptions,\n ...options,\n });\n}\n\n/**\n * Gets the access-token cookie (\"session_at\") value, or null if absent.\n */\nexport async function getAccessTokenCookie(): Promise<string | null> {\n return await getAuthCookie(ACCESS_TOKEN_COOKIE_NAME);\n}\n\n/**\n * Deletes the access-token cookie (\"session_at\").\n */\nexport async function deleteAccessTokenCookie(): Promise<void> {\n await deleteAuthCookie(ACCESS_TOKEN_COOKIE_NAME);\n}\n\n/**\n * Gets the current session cookie value.\n * Returns null if no session cookie exists.\n *\n * @returns Encrypted JWT session token or null\n *\n * @example\n * ```ts\n * const sessionToken = await getSessionCookie();\n * if (sessionToken) {\n * const { status, session } = await validateSessionJwt(sessionToken);\n * // ...\n * }\n * ```\n */\nexport async function getSessionCookie(): Promise<string | null> {\n return await getAuthCookie('session');\n}\n\n/**\n * Deletes the session cookie.\n * Use this when logging out or when session validation fails.\n *\n * @example\n * ```ts\n * await deleteSessionCookie();\n * redirect('/');\n * ```\n */\nexport async function deleteSessionCookie(): Promise<void> {\n await deleteAuthCookie('session');\n logger.info('Session cookie deleted');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAyCA,IAAM,SAAS,OAAO,OAAO,EAAE,WAAW,0BAA0B,CAAC;;;;;;;;;;;;;;;;;;;AAoBrE,eAAsB,cACpB,MACA,OACA,UAAyB,6BACV;CACf,IAAI;EACF,MAAM,cAAc,MAAM,QAAQ;EAClC,MAAM,gBAAgB;GAAE,GAAG;GAA6B,GAAG;EAAQ;EAInE,MAAM,OAAO,wBAAwB,MAAM,KAAK;EAChD,IAAI,QAAQ,mBACV,OAAO,KAAK;GAAE,YAAY;GAAM;EAAK,GAAG,uCAAuC;EAGjF,YAAY,IAAI,MAAM,OAAO,aAAa;EAE1C,OAAO,MAAM;GACX,YAAY;GACZ,QAAQ,cAAc;GACtB,QAAQ,cAAc;EACxB,GAAG,yBAAyB;CAC9B,SAAS,OAAO;EACd,OAAO,MAAM;GAAE;GAAO,YAAY;EAAK,GAAG,sBAAsB;EAChE,MAAM;CACR;AACF;;;;;;;;;;;;;;;;AAiBA,eAAsB,cAAc,MAAsC;CACxE,IAAI;EAEF,MAAM,UAAS,MADW,QAAQ,EAAA,CACP,IAAI,IAAI;EAEnC,IAAI,CAAC,QAAQ,OAAO;GAClB,OAAO,MAAM,EAAE,YAAY,KAAK,GAAG,kBAAkB;GACrD,OAAO;EACT;EAEA,OAAO,MAAM,EAAE,YAAY,KAAK,GAAG,kBAAkB;EACrD,OAAO,OAAO;CAChB,SAAS,OAAO;EACd,OAAO,MAAM;GAAE;GAAO,YAAY;EAAK,GAAG,sBAAsB;EAChE,OAAO;CACT;AACF;;;;;;;;;;;;AAaA,eAAsB,iBAAiB,MAA6B;CAClE,IAAI;EAEF,CAAA,MAD0B,QAAQ,EAAA,CACtB,OAAO,IAAI;EAEvB,OAAO,MAAM,EAAE,YAAY,KAAK,GAAG,6BAA6B;CAClE,SAAS,OAAO;EACd,OAAO,MAAM;GAAE;GAAO,YAAY;EAAK,GAAG,yBAAyB;EACnE,MAAM;CACR;AACF;;;;;;;;;;;;;AAcA,eAAsB,kBAAkB,OAAgC;CACtE,IAAI;EACF,MAAM,cAAc,MAAM,QAAQ;EAElC,KAAK,MAAM,QAAQ,OACjB,YAAY,OAAO,IAAI;EAGzB,OAAO,MAAM,EAAE,aAAa,MAAM,GAAG,uCAAuC;CAC9E,SAAS,OAAO;EACd,OAAO,MAAM;GAAE;GAAO,aAAa;EAAM,GAAG,0BAA0B;EACtE,MAAM;CACR;AACF;;;;;;;;;;;;;;AAeA,eAAsB,iBACpB,cACA,UAAkC,CAAC,GACpB;CACf,MAAM,cAAc,WAAW,cAAc;EAC3C,GAAG;EACH,GAAG;CACL,CAAC;CAED,OAAO,KAAK,oBAAoB;AAClC;;;;;;;AAQA,eAAsB,qBACpB,cACA,UAAkC,CAAC,GACpB;CACf,MAAM,cAAc,0BAA0B,cAAc;EAC1D,GAAG;EACH,GAAG;CACL,CAAC;AACH;;;;AAKA,eAAsB,uBAA+C;CACnE,OAAO,MAAM,cAAc,wBAAwB;AACrD;;;;AAKA,eAAsB,0BAAyC;CAC7D,MAAM,iBAAiB,wBAAwB;AACjD;;;;;;;;;;;;;;;;AAiBA,eAAsB,mBAA2C;CAC/D,OAAO,MAAM,cAAc,SAAS;AACtC;;;;;;;;;;;AAYA,eAAsB,sBAAqC;CACzD,MAAM,iBAAiB,SAAS;CAChC,OAAO,KAAK,wBAAwB;AACtC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"heartbeat-handler.d.ts","sourceRoot":"","sources":["../src/heartbeat-handler.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"heartbeat-handler.d.ts","sourceRoot":"","sources":["../src/heartbeat-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAK/D,MAAM,WAAW,sBAAsB;IACrC,iEAAiE;IACjE,WAAW,EAAE,WAAW,CAAC;CAC1B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,sBAAsB,GAC7B,OAAO,CAAC,QAAQ,CAAC,CAMnB"}
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
import { globalPOSTRateLimit } from "./request.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { nextCookieStore } from "./cookie-store.js";
|
|
3
|
+
import { handleHeartbeat as handleHeartbeat$1 } from "@eventuras/fides-auth/server";
|
|
4
4
|
//#region src/heartbeat-handler.ts
|
|
5
5
|
/**
|
|
6
|
-
* Server-side heartbeat handler for Next.js route handlers
|
|
6
|
+
* Server-side heartbeat handler for Next.js route handlers — a thin wrapper over
|
|
7
|
+
* the framework-agnostic handler in `@eventuras/fides-auth/server`, wiring it to
|
|
8
|
+
* the Next cookie store and the global POST rate limiter.
|
|
7
9
|
*
|
|
8
10
|
* Pairs with the client-side `useHeartbeat` hook: when the user is active, the
|
|
9
|
-
* client POSTs here, and this handler exchanges the current refresh token for
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* Returns:
|
|
13
|
-
* - 200 with `{ accessTokenExpiresAt }` on success
|
|
14
|
-
* - 401 when there is no session or the refresh token is dead
|
|
15
|
-
* - 405 for non-POST methods
|
|
16
|
-
* - 429 when rate-limited
|
|
11
|
+
* client POSTs here, and this handler exchanges the current refresh token for a
|
|
12
|
+
* fresh access token (and rotated refresh token, if the IdP rotates).
|
|
17
13
|
*/
|
|
18
|
-
var logger = Logger.create({ namespace: "fides-auth-next:heartbeat" });
|
|
19
14
|
/**
|
|
20
|
-
* Handles a heartbeat request
|
|
15
|
+
* Handles a heartbeat request in a Next.js route handler.
|
|
21
16
|
*
|
|
22
17
|
* @example
|
|
23
18
|
* ```ts
|
|
@@ -31,30 +26,11 @@ var logger = Logger.create({ namespace: "fides-auth-next:heartbeat" });
|
|
|
31
26
|
* ```
|
|
32
27
|
*/
|
|
33
28
|
async function handleHeartbeat(request, config) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
return handleHeartbeat$1(request, {
|
|
30
|
+
oauthConfig: config.oauthConfig,
|
|
31
|
+
cookies: await nextCookieStore(),
|
|
32
|
+
rateLimit: globalPOSTRateLimit
|
|
37
33
|
});
|
|
38
|
-
if (!await globalPOSTRateLimit()) {
|
|
39
|
-
logger.warn("Heartbeat rate limit exceeded");
|
|
40
|
-
return new Response(null, { status: 429 });
|
|
41
|
-
}
|
|
42
|
-
const session = await getCurrentSession();
|
|
43
|
-
if (!session) {
|
|
44
|
-
logger.debug("Heartbeat with no session");
|
|
45
|
-
return new Response(null, { status: 401 });
|
|
46
|
-
}
|
|
47
|
-
if (!session.tokens?.refreshToken) {
|
|
48
|
-
logger.warn("Heartbeat with session but no refresh token");
|
|
49
|
-
return new Response(null, { status: 401 });
|
|
50
|
-
}
|
|
51
|
-
const updated = await refreshCurrentSession(config.oauthConfig);
|
|
52
|
-
if (!updated) {
|
|
53
|
-
logger.info("Heartbeat refresh failed");
|
|
54
|
-
return new Response(null, { status: 401 });
|
|
55
|
-
}
|
|
56
|
-
logger.debug("Heartbeat refresh succeeded");
|
|
57
|
-
return Response.json({ accessTokenExpiresAt: updated.tokens?.accessTokenExpiresAt ?? null }, { headers: { "Cache-Control": "private, no-store" } });
|
|
58
34
|
}
|
|
59
35
|
//#endregion
|
|
60
36
|
export { handleHeartbeat };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"heartbeat-handler.js","names":[],"sources":["../src/heartbeat-handler.ts"],"sourcesContent":["/**\n * Server-side heartbeat handler for Next.js route handlers.\n *\n * Pairs with the client-side `useHeartbeat` hook: when the user is active, the\n * client POSTs here, and this handler exchanges the current refresh token for\n *
|
|
1
|
+
{"version":3,"file":"heartbeat-handler.js","names":[],"sources":["../src/heartbeat-handler.ts"],"sourcesContent":["/**\n * Server-side heartbeat handler for Next.js route handlers — a thin wrapper over\n * the framework-agnostic handler in `@eventuras/fides-auth/server`, wiring it to\n * the Next cookie store and the global POST rate limiter.\n *\n * Pairs with the client-side `useHeartbeat` hook: when the user is active, the\n * client POSTs here, and this handler exchanges the current refresh token for a\n * fresh access token (and rotated refresh token, if the IdP rotates).\n */\n\nimport { handleHeartbeat as coreHandleHeartbeat } from '@eventuras/fides-auth/server';\nimport type { OAuthConfig } from '@eventuras/fides-auth/oauth';\n\nimport { nextCookieStore } from './cookie-store';\nimport { globalPOSTRateLimit } from './request';\n\nexport interface HeartbeatHandlerConfig {\n /** OAuth/OIDC configuration used to refresh the access token. */\n oauthConfig: OAuthConfig;\n}\n\n/**\n * Handles a heartbeat request in a Next.js route handler.\n *\n * @example\n * ```ts\n * // app/(auth)/api/auth/heartbeat/route.ts\n * import { handleHeartbeat } from '@eventuras/fides-auth-next/heartbeat-handler';\n * import { oauthConfig } from '@/utils/oauthConfig';\n *\n * export async function POST(request: Request) {\n * return handleHeartbeat(request, { oauthConfig });\n * }\n * ```\n */\nexport async function handleHeartbeat(\n request: Request,\n config: HeartbeatHandlerConfig,\n): Promise<Response> {\n return coreHandleHeartbeat(request, {\n oauthConfig: config.oauthConfig,\n cookies: await nextCookieStore(),\n rateLimit: globalPOSTRateLimit,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,eAAsB,gBACpB,SACA,QACmB;CACnB,OAAO,kBAAoB,SAAS;EAClC,aAAa,OAAO;EACpB,SAAS,MAAM,gBAAgB;EAC/B,WAAW;CACb,CAAC;AACH"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AAGpC,cAAc,SAAS,CAAC;AAGxB,cAAc,6BAA6B,CAAC;AAC5C,YAAY,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC/D,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,cAAc,oCAAoC,CAAC;AACnD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AAGpC,cAAc,SAAS,CAAC;AAGxB,cAAc,6BAA6B,CAAC;AAC5C,YAAY,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC/D,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,cAAc,oCAAoC,CAAC;AACnD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
import { globalGETRateLimit, globalPOSTRateLimit } from "./request.js";
|
|
2
|
-
import { defaultOAuthCookieOptions, defaultSessionCookieOptions, deleteAuthCookie, deleteAuthCookies, deleteSessionCookie, getAuthCookie, getSessionCookie, setAuthCookie, setSessionCookie } from "./cookies.js";
|
|
3
2
|
import { clearCurrentSession, createAndPersistSession, createSession, getCurrentSession, refreshCurrentSession } from "./session.js";
|
|
3
|
+
import { ACCESS_TOKEN_COOKIE_NAME, COOKIE_INFO_BYTES, COOKIE_MAX_BYTES, CookieTooLargeError, ID_TOKEN_COOKIE_NAME, assertCookieWithinLimit, cookieByteSize, defaultOAuthCookieOptions, defaultSessionCookieOptions, deleteAccessTokenCookie, deleteAuthCookie, deleteAuthCookies, deleteSessionCookie, getAccessTokenCookie, getAuthCookie, getSessionCookie, setAccessTokenCookie, setAuthCookie, setSessionCookie } from "./cookies.js";
|
|
4
4
|
import { handleOidcCallback } from "./oidc-callback.js";
|
|
5
5
|
import { handleOidcLogin } from "./oidc-login.js";
|
|
6
|
+
import { handleOidcLogout } from "./oidc-logout.js";
|
|
6
7
|
import { handleHeartbeat } from "./heartbeat-handler.js";
|
|
7
|
-
import { checkAuth, createAuthStore, initializeAuth } from "./store/
|
|
8
|
-
import { createAuthStoreHooks } from "./store/hooks.js";
|
|
9
|
-
import { startSessionMonitor } from "./store/session-monitor.js";
|
|
10
|
-
import { useSessionMonitor } from "./store/use-session-monitor.js";
|
|
11
|
-
import { useHeartbeat } from "./store/use-heartbeat.js";
|
|
12
|
-
import { configureAuthLogger } from "./store/configure-logger.js";
|
|
13
|
-
import "./store/index.js";
|
|
8
|
+
import { checkAuth, configureAuthLogger, createAuthStore, createAuthStoreHooks, initializeAuth, startSessionMonitor, useHeartbeat, useSessionMonitor } from "./store/index.js";
|
|
14
9
|
import { TokenBucket } from "@eventuras/fides-auth/rate-limit";
|
|
15
10
|
export * from "@eventuras/fides-auth/utils";
|
|
16
|
-
export * from "@eventuras/fides-auth/session-validation";
|
|
17
|
-
export * from "@eventuras/fides-auth/session-refresh";
|
|
18
11
|
export * from "@eventuras/fides-auth/oauth";
|
|
12
|
+
export * from "@eventuras/fides-auth/session-refresh";
|
|
13
|
+
export * from "@eventuras/fides-auth/session-validation";
|
|
19
14
|
export * from "@eventuras/fides-auth/silent-login";
|
|
20
15
|
export * from "@eventuras/fides-auth/types";
|
|
21
|
-
export { TokenBucket, checkAuth, clearCurrentSession, configureAuthLogger, createAndPersistSession, createAuthStore, createAuthStoreHooks, createSession, defaultOAuthCookieOptions, defaultSessionCookieOptions, deleteAuthCookie, deleteAuthCookies, deleteSessionCookie, getAuthCookie, getCurrentSession, getSessionCookie, globalGETRateLimit, globalPOSTRateLimit, handleHeartbeat, handleOidcCallback, handleOidcLogin, initializeAuth, refreshCurrentSession, setAuthCookie, setSessionCookie, startSessionMonitor, useHeartbeat, useSessionMonitor };
|
|
16
|
+
export { ACCESS_TOKEN_COOKIE_NAME, COOKIE_INFO_BYTES, COOKIE_MAX_BYTES, CookieTooLargeError, ID_TOKEN_COOKIE_NAME, TokenBucket, assertCookieWithinLimit, checkAuth, clearCurrentSession, configureAuthLogger, cookieByteSize, createAndPersistSession, createAuthStore, createAuthStoreHooks, createSession, defaultOAuthCookieOptions, defaultSessionCookieOptions, deleteAccessTokenCookie, deleteAuthCookie, deleteAuthCookies, deleteSessionCookie, getAccessTokenCookie, getAuthCookie, getCurrentSession, getSessionCookie, globalGETRateLimit, globalPOSTRateLimit, handleHeartbeat, handleOidcCallback, handleOidcLogin, handleOidcLogout, initializeAuth, refreshCurrentSession, setAccessTokenCookie, setAuthCookie, setSessionCookie, startSessionMonitor, useHeartbeat, useSessionMonitor };
|
package/dist/oidc-callback.d.ts
CHANGED
|
@@ -9,11 +9,6 @@ export interface OidcCallbackConfig {
|
|
|
9
9
|
* @default 'roles'
|
|
10
10
|
*/
|
|
11
11
|
rolesClaim?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Session duration in days.
|
|
14
|
-
* @default 7
|
|
15
|
-
*/
|
|
16
|
-
sessionDurationDays?: number;
|
|
17
12
|
/**
|
|
18
13
|
* Default path to redirect to after login if no returnTo cookie is set.
|
|
19
14
|
* @default '/'
|
|
@@ -24,6 +19,11 @@ export interface OidcCallbackConfig {
|
|
|
24
19
|
* If not provided, only same-origin paths are allowed.
|
|
25
20
|
*/
|
|
26
21
|
validateReturnTo?: (path: string) => boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Reserved for backwards compatibility. Currently not applied — the session
|
|
24
|
+
* cookie lifetime is fixed by the default cookie options.
|
|
25
|
+
*/
|
|
26
|
+
sessionDurationDays?: number;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Handles the OIDC callback in a Next.js route handler.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oidc-callback.d.ts","sourceRoot":"","sources":["../src/oidc-callback.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"oidc-callback.d.ts","sourceRoot":"","sources":["../src/oidc-callback.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAK/D,MAAM,WAAW,kBAAkB;IACjC,qFAAqF;IACrF,WAAW,EAAE,WAAW,CAAC;IAEzB,2FAA2F;IAC3F,cAAc,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;;OAGG;IACH,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;IAE7C;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAAC,QAAQ,CAAC,CAYnB"}
|
package/dist/oidc-callback.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { globalGETRateLimit } from "./request.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { Logger } from "@eventuras/logger";
|
|
5
|
-
import { buildSessionFromTokens, exchangeAuthorizationCode, validateReturnUrl } from "@eventuras/fides-auth/oauth";
|
|
2
|
+
import { nextCookieStore } from "./cookie-store.js";
|
|
3
|
+
import { handleOidcCallback as handleOidcCallback$1 } from "@eventuras/fides-auth/server";
|
|
6
4
|
//#region src/oidc-callback.ts
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* OIDC callback handler for Next.js route handlers — a thin wrapper over the
|
|
7
|
+
* framework-agnostic handler in `@eventuras/fides-auth/server`, wiring it to the
|
|
8
|
+
* Next cookie store and the global GET rate limiter.
|
|
9
|
+
*/
|
|
8
10
|
/**
|
|
9
11
|
* Handles the OIDC callback in a Next.js route handler.
|
|
10
12
|
*
|
|
@@ -23,39 +25,15 @@ var logger = Logger.create({ namespace: "fides-auth-next:oidc-callback" });
|
|
|
23
25
|
* ```
|
|
24
26
|
*/
|
|
25
27
|
async function handleOidcCallback(request, config) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const storedState = await getAuthCookie("oauth_state");
|
|
36
|
-
const storedCodeVerifier = await getAuthCookie("oauth_code_verifier");
|
|
37
|
-
if (!storedState || !storedCodeVerifier) {
|
|
38
|
-
logger.warn("Missing state or code verifier");
|
|
39
|
-
return new Response("Please restart the login process.", { status: 400 });
|
|
40
|
-
}
|
|
41
|
-
await setSessionCookie(await createSession(buildSessionFromTokens(await exchangeAuthorizationCode(oauthConfig, publicUrl, storedCodeVerifier, storedState), rolesClaim), { sessionDurationDays }));
|
|
42
|
-
const returnTo = await getAuthCookie("returnTo");
|
|
43
|
-
await deleteAuthCookies([
|
|
44
|
-
"oauth_state",
|
|
45
|
-
"oauth_code_verifier",
|
|
46
|
-
"returnTo"
|
|
47
|
-
]);
|
|
48
|
-
const redirectUrl = validateReturnUrl(returnTo, applicationUrl, defaultRedirectPath, validateReturnTo);
|
|
49
|
-
redirectUrl.searchParams.set("login", "success");
|
|
50
|
-
logger.info({ redirectUrl: redirectUrl.toString() }, "Login successful, redirecting");
|
|
51
|
-
return new Response(null, {
|
|
52
|
-
status: 302,
|
|
53
|
-
headers: { Location: redirectUrl.toString() }
|
|
54
|
-
});
|
|
55
|
-
} catch (error) {
|
|
56
|
-
logger.error({ error }, "OIDC callback error");
|
|
57
|
-
return new Response("An unexpected error occurred. Please restart the login process.", { status: 500 });
|
|
58
|
-
}
|
|
28
|
+
return handleOidcCallback$1(request, {
|
|
29
|
+
oauthConfig: config.oauthConfig,
|
|
30
|
+
applicationUrl: config.applicationUrl,
|
|
31
|
+
rolesClaim: config.rolesClaim,
|
|
32
|
+
defaultRedirectPath: config.defaultRedirectPath,
|
|
33
|
+
validateReturnTo: config.validateReturnTo,
|
|
34
|
+
cookies: await nextCookieStore(),
|
|
35
|
+
rateLimit: globalGETRateLimit
|
|
36
|
+
});
|
|
59
37
|
}
|
|
60
38
|
//#endregion
|
|
61
39
|
export { handleOidcCallback };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oidc-callback.js","names":[],"sources":["../src/oidc-callback.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"file":"oidc-callback.js","names":[],"sources":["../src/oidc-callback.ts"],"sourcesContent":["/**\n * OIDC callback handler for Next.js route handlers — a thin wrapper over the\n * framework-agnostic handler in `@eventuras/fides-auth/server`, wiring it to the\n * Next cookie store and the global GET rate limiter.\n */\n\nimport { handleOidcCallback as coreHandleOidcCallback } from '@eventuras/fides-auth/server';\nimport type { OAuthConfig } from '@eventuras/fides-auth/oauth';\n\nimport { nextCookieStore } from './cookie-store';\nimport { globalGETRateLimit } from './request';\n\nexport interface OidcCallbackConfig {\n /** OAuth/OIDC configuration (issuer, clientId, clientSecret, redirect_uri, scope) */\n oauthConfig: OAuthConfig;\n\n /** Public application URL (used to reconstruct the callback URL and validate redirects) */\n applicationUrl: string;\n\n /**\n * Name of the ID token claim that contains user roles.\n * @default 'roles'\n */\n rolesClaim?: string;\n\n /**\n * Default path to redirect to after login if no returnTo cookie is set.\n * @default '/'\n */\n defaultRedirectPath?: string;\n\n /**\n * Optional function to validate the returnTo path.\n * If not provided, only same-origin paths are allowed.\n */\n validateReturnTo?: (path: string) => boolean;\n\n /**\n * Reserved for backwards compatibility. Currently not applied — the session\n * cookie lifetime is fixed by the default cookie options.\n */\n sessionDurationDays?: number;\n}\n\n/**\n * Handles the OIDC callback in a Next.js route handler.\n *\n * @example\n * ```ts\n * // In app/api/auth/callback/oidc/route.ts\n * import { handleOidcCallback } from '@eventuras/fides-auth-next/oidc-callback';\n *\n * export async function GET(request: Request) {\n * return handleOidcCallback(request, {\n * oauthConfig,\n * applicationUrl: 'https://example.com',\n * rolesClaim: 'roles',\n * });\n * }\n * ```\n */\nexport async function handleOidcCallback(\n request: Request,\n config: OidcCallbackConfig,\n): Promise<Response> {\n // Map explicitly rather than spreading: sessionDurationDays is accepted for\n // backwards compatibility but the core handler doesn't take it.\n return coreHandleOidcCallback(request, {\n oauthConfig: config.oauthConfig,\n applicationUrl: config.applicationUrl,\n rolesClaim: config.rolesClaim,\n defaultRedirectPath: config.defaultRedirectPath,\n validateReturnTo: config.validateReturnTo,\n cookies: await nextCookieStore(),\n rateLimit: globalGETRateLimit,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,eAAsB,mBACpB,SACA,QACmB;CAGnB,OAAO,qBAAuB,SAAS;EACrC,aAAa,OAAO;EACpB,gBAAgB,OAAO;EACvB,YAAY,OAAO;EACnB,qBAAqB,OAAO;EAC5B,kBAAkB,OAAO;EACzB,SAAS,MAAM,gBAAgB;EAC/B,WAAW;CACb,CAAC;AACH"}
|
package/dist/oidc-login.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oidc-login.d.ts","sourceRoot":"","sources":["../src/oidc-login.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"oidc-login.d.ts","sourceRoot":"","sources":["../src/oidc-login.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAK/D,MAAM,WAAW,eAAe;IAC9B,+BAA+B;IAC/B,WAAW,EAAE,WAAW,CAAC;IAEzB;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;CAC3D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,QAAQ,CAAC,CAOnB"}
|
package/dist/oidc-login.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { globalGETRateLimit } from "./request.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { Logger } from "@eventuras/logger";
|
|
5
|
-
import { buildPKCEOptions, discoverAndBuildAuthorizationUrl } from "@eventuras/fides-auth/oauth";
|
|
2
|
+
import { nextCookieStore } from "./cookie-store.js";
|
|
3
|
+
import { handleOidcLogin as handleOidcLogin$1 } from "@eventuras/fides-auth/server";
|
|
6
4
|
//#region src/oidc-login.ts
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
/**
|
|
6
|
+
* OIDC login initiation for Next.js route handlers — a thin wrapper over the
|
|
7
|
+
* framework-agnostic handler in `@eventuras/fides-auth/server`, wiring it to the
|
|
8
|
+
* Next cookie store and the global GET rate limiter.
|
|
9
|
+
*/
|
|
9
10
|
/**
|
|
10
11
|
* Handles OIDC login initiation in a Next.js route handler.
|
|
11
12
|
*
|
|
@@ -20,24 +21,11 @@ var defaultValidateReturnTo = (raw) => /^\/(?!\/)/.test(raw) ? raw : null;
|
|
|
20
21
|
* ```
|
|
21
22
|
*/
|
|
22
23
|
async function handleOidcLogin(request, config) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const rawReturnTo = new URL(request.url).searchParams.get("returnTo");
|
|
29
|
-
const returnTo = rawReturnTo ? validateReturnTo(rawReturnTo) : null;
|
|
30
|
-
const pkce = await buildPKCEOptions(oauthConfig);
|
|
31
|
-
const authorizationUrl = await discoverAndBuildAuthorizationUrl(oauthConfig, pkce);
|
|
32
|
-
logger.info("Redirecting to OIDC provider for login");
|
|
33
|
-
const cookieStore = await cookies();
|
|
34
|
-
const cookieOpts = defaultOAuthCookieOptions;
|
|
35
|
-
cookieStore.set("oauth_state", pkce.state, cookieOpts);
|
|
36
|
-
cookieStore.set("oauth_code_verifier", pkce.code_verifier, cookieOpts);
|
|
37
|
-
if (returnTo) cookieStore.set("returnTo", returnTo, cookieOpts);
|
|
38
|
-
return new Response(null, {
|
|
39
|
-
status: 302,
|
|
40
|
-
headers: { Location: authorizationUrl.toString() }
|
|
24
|
+
return handleOidcLogin$1(request, {
|
|
25
|
+
oauthConfig: config.oauthConfig,
|
|
26
|
+
validateReturnTo: config.validateReturnTo,
|
|
27
|
+
cookies: await nextCookieStore(),
|
|
28
|
+
rateLimit: globalGETRateLimit
|
|
41
29
|
});
|
|
42
30
|
}
|
|
43
31
|
//#endregion
|