@bhooai/nexus-auth 2.0.11 → 2.0.12
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/package.json +1 -1
- package/src/csrf.ts +23 -0
package/package.json
CHANGED
package/src/csrf.ts
CHANGED
|
@@ -68,6 +68,29 @@ export function issueCsrfToken(ctx: RequestContext, options: CsrfOptions = {}):
|
|
|
68
68
|
return issueToken(ctx, options.cookieName ?? 'nexus_csrf', options.cookie ?? { path: '/', sameSite: 'lax', secure: false });
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Read the existing double-submit cookie token WITHOUT rotating it. Returns the
|
|
73
|
+
* current `nexus_csrf` value, or undefined when no cookie is present. Use this
|
|
74
|
+
* (not `issueCsrfToken`) when you only need to expose the token, so the cookie
|
|
75
|
+
* stays stable and previously-issued tokens keep matching.
|
|
76
|
+
*/
|
|
77
|
+
export function getCsrfToken(ctx: RequestContext, options: CsrfOptions = {}): string | undefined {
|
|
78
|
+
return readCookie(ctx, options.cookieName ?? 'nexus_csrf');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Verify a mutation's CSRF token against the double-submit cookie. The token may
|
|
83
|
+
* be carried either as an argument (`csrf`) or in the `X-CSRF-Token` header; only
|
|
84
|
+
* one needs to match the cookie (never both). Both channels stay cookie-bound:
|
|
85
|
+
* the provided token must equal the `nexus_csrf` cookie value.
|
|
86
|
+
*/
|
|
87
|
+
export function verifyMutationCsrf(ctx: RequestContext, argToken: string | undefined, headerToken?: string | undefined, options: CsrfOptions = {}): boolean {
|
|
88
|
+
const cookieToken = readCookie(ctx, options.cookieName ?? 'nexus_csrf');
|
|
89
|
+
if (!cookieToken) return false;
|
|
90
|
+
const provided = argToken ?? headerToken;
|
|
91
|
+
return !!provided && timingSafeEqual(cookieToken, provided);
|
|
92
|
+
}
|
|
93
|
+
|
|
71
94
|
/** CSRF + origin check for the WebSocket upgrade request (no CORS preflight). */
|
|
72
95
|
export function checkWsUpgrade(headers: Record<string, string | string[] | undefined>, options: CsrfOptions = {}): void {
|
|
73
96
|
const trusted = options.trustedOrigins ?? [];
|