auth-vir 5.0.1 → 5.0.2
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 +4 -4
- package/src/auth-client/backend-auth.client.ts +8 -0
- package/src/auth.ts +9 -1
- package/dist/auth-client/backend-auth.client.d.ts +0 -263
- package/dist/auth-client/backend-auth.client.js +0 -391
- package/dist/auth-client/frontend-auth.client.d.ts +0 -113
- package/dist/auth-client/frontend-auth.client.js +0 -131
- package/dist/auth-client/is-session-refresh-ready.d.ts +0 -23
- package/dist/auth-client/is-session-refresh-ready.js +0 -21
- package/dist/auth.d.ts +0 -74
- package/dist/auth.js +0 -128
- package/dist/cookie.d.ts +0 -111
- package/dist/cookie.js +0 -137
- package/dist/csrf-token.d.ts +0 -33
- package/dist/csrf-token.js +0 -42
- package/dist/generated/browser.d.ts +0 -9
- package/dist/generated/browser.js +0 -17
- package/dist/generated/client.d.ts +0 -26
- package/dist/generated/client.js +0 -32
- package/dist/generated/commonInputTypes.d.ts +0 -122
- package/dist/generated/commonInputTypes.js +0 -1
- package/dist/generated/enums.d.ts +0 -1
- package/dist/generated/enums.js +0 -10
- package/dist/generated/internal/class.d.ts +0 -126
- package/dist/generated/internal/class.js +0 -85
- package/dist/generated/internal/prismaNamespace.d.ts +0 -545
- package/dist/generated/internal/prismaNamespace.js +0 -102
- package/dist/generated/internal/prismaNamespaceBrowser.d.ts +0 -75
- package/dist/generated/internal/prismaNamespaceBrowser.js +0 -70
- package/dist/generated/models/User.d.ts +0 -980
- package/dist/generated/models/User.js +0 -1
- package/dist/generated/models.d.ts +0 -2
- package/dist/generated/models.js +0 -1
- package/dist/generated/shapes.gen.d.ts +0 -8
- package/dist/generated/shapes.gen.js +0 -11
- package/dist/hash.d.ts +0 -42
- package/dist/hash.js +0 -52
- package/dist/headers.d.ts +0 -19
- package/dist/headers.js +0 -32
- package/dist/index.d.ts +0 -11
- package/dist/index.js +0 -11
- package/dist/jwt/jwt-keys.d.ts +0 -44
- package/dist/jwt/jwt-keys.js +0 -57
- package/dist/jwt/jwt-keys.script.d.ts +0 -1
- package/dist/jwt/jwt-keys.script.js +0 -3
- package/dist/jwt/jwt.d.ts +0 -126
- package/dist/jwt/jwt.js +0 -109
- package/dist/jwt/user-jwt.d.ts +0 -44
- package/dist/jwt/user-jwt.js +0 -53
package/dist/auth.d.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { type SelectFrom } from '@augment-vir/common';
|
|
2
|
-
import { type FullDate, type UtcTimezone } from 'date-vir';
|
|
3
|
-
import { AuthCookie, type CookieParams } from './cookie.js';
|
|
4
|
-
import { type CsrfHeaderNameOption } from './csrf-token.js';
|
|
5
|
-
import { type ParseJwtParams } from './jwt/jwt.js';
|
|
6
|
-
import { type JwtUserData } from './jwt/user-jwt.js';
|
|
7
|
-
/**
|
|
8
|
-
* All possible headers container types supported by {@link extractUserIdFromRequestHeaders}.
|
|
9
|
-
*
|
|
10
|
-
* @category Internal
|
|
11
|
-
*/
|
|
12
|
-
export type HeaderContainer = Record<string, string[] | undefined | string | number> | Headers;
|
|
13
|
-
/**
|
|
14
|
-
* Output from {@link extractUserIdFromRequestHeaders}.
|
|
15
|
-
*
|
|
16
|
-
* @category Internal
|
|
17
|
-
*/
|
|
18
|
-
export type UserIdResult<UserId extends string | number> = {
|
|
19
|
-
userId: UserId;
|
|
20
|
-
jwtExpiration: FullDate<UtcTimezone>;
|
|
21
|
-
/** When the JWT was issued (`iat` claim). */
|
|
22
|
-
jwtIssuedAt: FullDate<UtcTimezone>;
|
|
23
|
-
cookieName: AuthCookie;
|
|
24
|
-
/** The CSRF token embedded in the JWT. */
|
|
25
|
-
csrfToken: string;
|
|
26
|
-
/**
|
|
27
|
-
* Unix timestamp (in milliseconds) when the session was originally started. Used to enforce max
|
|
28
|
-
* session duration.
|
|
29
|
-
*/
|
|
30
|
-
sessionStartedAt: JwtUserData['sessionStartedAt'];
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Extract the user id from a request by checking both the request cookie and CSRF token. This is
|
|
34
|
-
* used by host (backend) code to help verify a request. After extracting the user id using this,
|
|
35
|
-
* you should compare it to users stored in your database.
|
|
36
|
-
*
|
|
37
|
-
* @category Auth : Host
|
|
38
|
-
* @returns The extracted user id or `undefined` if no valid auth headers exist.
|
|
39
|
-
*/
|
|
40
|
-
export declare function extractUserIdFromRequestHeaders<UserId extends string | number>(headers: HeaderContainer, jwtParams: Readonly<ParseJwtParams>, csrfHeaderNameOption: Readonly<CsrfHeaderNameOption>, cookieName?: AuthCookie): Promise<Readonly<UserIdResult<UserId>> | undefined>;
|
|
41
|
-
/**
|
|
42
|
-
* Extract a user id from just the cookie, without CSRF token validation. This is _less secure_ than
|
|
43
|
-
* {@link extractUserIdFromRequestHeaders} as a result. This should only be used in rare
|
|
44
|
-
* circumstances where you cannot rely on client-side JavaScript to insert the CSRF token.
|
|
45
|
-
*
|
|
46
|
-
* @deprecated Prefer {@link extractUserIdFromRequestHeaders} instead: it is more secure.
|
|
47
|
-
* @category Auth : Host
|
|
48
|
-
*/
|
|
49
|
-
export declare function insecureExtractUserIdFromCookieAlone<UserId extends string | number>(headers: HeaderContainer, jwtParams: Readonly<ParseJwtParams>, cookieName: AuthCookie): Promise<Readonly<UserIdResult<UserId>> | undefined>;
|
|
50
|
-
/**
|
|
51
|
-
* Used by host (backend) code to set headers on a response object. Sets both the auth JWT cookie
|
|
52
|
-
* and the CSRF token cookie. The CSRF cookie is not `HttpOnly` so that frontend JavaScript can read
|
|
53
|
-
* it and inject the value as a request header.
|
|
54
|
-
*
|
|
55
|
-
* @category Auth : Host
|
|
56
|
-
*/
|
|
57
|
-
export declare function generateSuccessfulLoginHeaders(
|
|
58
|
-
/** The id from your database of the user you're authenticating. */
|
|
59
|
-
userId: string | number, cookieConfig: Readonly<CookieParams>,
|
|
60
|
-
/**
|
|
61
|
-
* The timestamp (in seconds) when the session originally started. If not provided, the current
|
|
62
|
-
* time will be used (for new sessions).
|
|
63
|
-
*/
|
|
64
|
-
sessionStartedAt?: number | undefined): Promise<Record<string, string[]>>;
|
|
65
|
-
/**
|
|
66
|
-
* Used by host (backend) code to set headers on a response object when the user has logged out or
|
|
67
|
-
* failed to authorize.
|
|
68
|
-
*
|
|
69
|
-
* @category Auth : Host
|
|
70
|
-
*/
|
|
71
|
-
export declare function generateLogoutHeaders(cookieConfig: Readonly<SelectFrom<CookieParams, {
|
|
72
|
-
hostOrigin: true;
|
|
73
|
-
isDev: true;
|
|
74
|
-
}>>): Record<string, string[]>;
|
package/dist/auth.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { AuthCookie, clearAuthCookie, clearCsrfCookie, extractCookieJwt, generateAuthCookie, generateCsrfCookie, } from './cookie.js';
|
|
2
|
-
import { generateCsrfToken, resolveCsrfHeaderName } from './csrf-token.js';
|
|
3
|
-
function readHeader(headers, headerName) {
|
|
4
|
-
if (headers instanceof Headers) {
|
|
5
|
-
return headers.get(headerName) || undefined;
|
|
6
|
-
}
|
|
7
|
-
else {
|
|
8
|
-
const value = headers[headerName];
|
|
9
|
-
if (value == undefined) {
|
|
10
|
-
return undefined;
|
|
11
|
-
}
|
|
12
|
-
else if (Array.isArray(value)) {
|
|
13
|
-
return value[0];
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return String(value);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function readCsrfTokenHeader(headers, csrfHeaderNameOption) {
|
|
21
|
-
return readHeader(headers, resolveCsrfHeaderName(csrfHeaderNameOption));
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Extract the user id from a request by checking both the request cookie and CSRF token. This is
|
|
25
|
-
* used by host (backend) code to help verify a request. After extracting the user id using this,
|
|
26
|
-
* you should compare it to users stored in your database.
|
|
27
|
-
*
|
|
28
|
-
* @category Auth : Host
|
|
29
|
-
* @returns The extracted user id or `undefined` if no valid auth headers exist.
|
|
30
|
-
*/
|
|
31
|
-
export async function extractUserIdFromRequestHeaders(headers, jwtParams, csrfHeaderNameOption, cookieName = AuthCookie.Auth) {
|
|
32
|
-
try {
|
|
33
|
-
const csrfToken = readCsrfTokenHeader(headers, csrfHeaderNameOption);
|
|
34
|
-
const cookie = readHeader(headers, 'cookie');
|
|
35
|
-
if (!cookie || !csrfToken) {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
const jwt = await extractCookieJwt(cookie, jwtParams, cookieName);
|
|
39
|
-
if (!jwt || jwt.data.csrfToken !== csrfToken) {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
userId: jwt.data.userId,
|
|
44
|
-
jwtExpiration: jwt.jwtExpiration,
|
|
45
|
-
jwtIssuedAt: jwt.jwtIssuedAt,
|
|
46
|
-
cookieName,
|
|
47
|
-
csrfToken: jwt.data.csrfToken,
|
|
48
|
-
sessionStartedAt: jwt.data.sessionStartedAt,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
catch {
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Extract a user id from just the cookie, without CSRF token validation. This is _less secure_ than
|
|
57
|
-
* {@link extractUserIdFromRequestHeaders} as a result. This should only be used in rare
|
|
58
|
-
* circumstances where you cannot rely on client-side JavaScript to insert the CSRF token.
|
|
59
|
-
*
|
|
60
|
-
* @deprecated Prefer {@link extractUserIdFromRequestHeaders} instead: it is more secure.
|
|
61
|
-
* @category Auth : Host
|
|
62
|
-
*/
|
|
63
|
-
export async function insecureExtractUserIdFromCookieAlone(headers, jwtParams, cookieName) {
|
|
64
|
-
try {
|
|
65
|
-
const cookie = readHeader(headers, 'cookie');
|
|
66
|
-
if (!cookie) {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
const jwt = await extractCookieJwt(cookie, jwtParams, cookieName);
|
|
70
|
-
if (!jwt) {
|
|
71
|
-
return undefined;
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
userId: jwt.data.userId,
|
|
75
|
-
jwtExpiration: jwt.jwtExpiration,
|
|
76
|
-
jwtIssuedAt: jwt.jwtIssuedAt,
|
|
77
|
-
cookieName,
|
|
78
|
-
csrfToken: jwt.data.csrfToken,
|
|
79
|
-
sessionStartedAt: jwt.data.sessionStartedAt,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
catch {
|
|
83
|
-
return undefined;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Used by host (backend) code to set headers on a response object. Sets both the auth JWT cookie
|
|
88
|
-
* and the CSRF token cookie. The CSRF cookie is not `HttpOnly` so that frontend JavaScript can read
|
|
89
|
-
* it and inject the value as a request header.
|
|
90
|
-
*
|
|
91
|
-
* @category Auth : Host
|
|
92
|
-
*/
|
|
93
|
-
export async function generateSuccessfulLoginHeaders(
|
|
94
|
-
/** The id from your database of the user you're authenticating. */
|
|
95
|
-
userId, cookieConfig,
|
|
96
|
-
/**
|
|
97
|
-
* The timestamp (in seconds) when the session originally started. If not provided, the current
|
|
98
|
-
* time will be used (for new sessions).
|
|
99
|
-
*/
|
|
100
|
-
sessionStartedAt) {
|
|
101
|
-
const csrfToken = generateCsrfToken();
|
|
102
|
-
const authCookie = await generateAuthCookie({
|
|
103
|
-
csrfToken,
|
|
104
|
-
userId,
|
|
105
|
-
sessionStartedAt: sessionStartedAt ?? Date.now(),
|
|
106
|
-
}, cookieConfig);
|
|
107
|
-
const csrfCookie = generateCsrfCookie(csrfToken, cookieConfig);
|
|
108
|
-
return {
|
|
109
|
-
'set-cookie': [
|
|
110
|
-
authCookie,
|
|
111
|
-
csrfCookie,
|
|
112
|
-
],
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Used by host (backend) code to set headers on a response object when the user has logged out or
|
|
117
|
-
* failed to authorize.
|
|
118
|
-
*
|
|
119
|
-
* @category Auth : Host
|
|
120
|
-
*/
|
|
121
|
-
export function generateLogoutHeaders(cookieConfig) {
|
|
122
|
-
return {
|
|
123
|
-
'set-cookie': [
|
|
124
|
-
clearAuthCookie(cookieConfig),
|
|
125
|
-
clearCsrfCookie(cookieConfig),
|
|
126
|
-
],
|
|
127
|
-
};
|
|
128
|
-
}
|
package/dist/cookie.d.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { type PartialWithUndefined, type SelectFrom } from '@augment-vir/common';
|
|
2
|
-
import { type AnyDuration } from 'date-vir';
|
|
3
|
-
import { type Primitive } from 'type-fest';
|
|
4
|
-
import { type CreateJwtParams, type ParseJwtParams, type ParsedJwt } from './jwt/jwt.js';
|
|
5
|
-
import { type JwtUserData } from './jwt/user-jwt.js';
|
|
6
|
-
/**
|
|
7
|
-
* Cookie header names supported by default.
|
|
8
|
-
*
|
|
9
|
-
* @category Internal
|
|
10
|
-
*/
|
|
11
|
-
export declare enum AuthCookie {
|
|
12
|
-
/** Used for a full user login auth. */
|
|
13
|
-
Auth = "auth",
|
|
14
|
-
/** Use for a temporary "just signed up" auth. */
|
|
15
|
-
SignUp = "sign-up",
|
|
16
|
-
/** Used for storing the CSRF token. Not `HttpOnly` so that frontend JS can read it. */
|
|
17
|
-
Csrf = "auth-vir-csrf"
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Parameters for {@link generateAuthCookie}.
|
|
21
|
-
*
|
|
22
|
-
* @category Internal
|
|
23
|
-
*/
|
|
24
|
-
export type CookieParams = {
|
|
25
|
-
/**
|
|
26
|
-
* The origin of the host (backend) service that cookies will be included in all requests to.
|
|
27
|
-
* This should be restricted to just your host (backend) origin for security purposes.
|
|
28
|
-
*
|
|
29
|
-
* @example 'https://www.example.com'
|
|
30
|
-
*/
|
|
31
|
-
hostOrigin: string;
|
|
32
|
-
/**
|
|
33
|
-
* The max duration of this cookie. Or, in other words, the max user session duration before
|
|
34
|
-
* they're logged out.
|
|
35
|
-
*/
|
|
36
|
-
cookieDuration: AnyDuration;
|
|
37
|
-
/**
|
|
38
|
-
* All JWT parameters required for generating the encrypted JWT that will be embedded in the
|
|
39
|
-
* Cookie. Note that all JWT keys contained herein should never shared with any frontend,
|
|
40
|
-
* client, etc.
|
|
41
|
-
*/
|
|
42
|
-
jwtParams: Readonly<CreateJwtParams>;
|
|
43
|
-
} & PartialWithUndefined<{
|
|
44
|
-
/**
|
|
45
|
-
* Which auth cookie name to use.
|
|
46
|
-
*
|
|
47
|
-
* @default AuthCookie.Auth
|
|
48
|
-
*/
|
|
49
|
-
authCookie: AuthCookie;
|
|
50
|
-
/**
|
|
51
|
-
* Is set to `true` (which should only be done in development environments), the cookie will be
|
|
52
|
-
* allowed in insecure requests (non HTTPS requests).
|
|
53
|
-
*
|
|
54
|
-
* @default false
|
|
55
|
-
*/
|
|
56
|
-
isDev: boolean;
|
|
57
|
-
}>;
|
|
58
|
-
/**
|
|
59
|
-
* Generate a secure cookie that stores the user JWT data. Used in host (backend) code.
|
|
60
|
-
*
|
|
61
|
-
* @category Internal
|
|
62
|
-
*/
|
|
63
|
-
export declare function generateAuthCookie(userJwtData: Readonly<JwtUserData>, cookieConfig: Readonly<CookieParams>): Promise<string>;
|
|
64
|
-
/**
|
|
65
|
-
* Generate a CSRF token cookie. This cookie is intentionally not `HttpOnly` so that frontend
|
|
66
|
-
* JavaScript can read it and inject the value as a request header for double-submit verification.
|
|
67
|
-
*
|
|
68
|
-
* The CSRF cookie uses a fixed 400-day MAX-AGE rather than matching the auth cookie duration. 400
|
|
69
|
-
* days is the cross-browser safe maximum (Chrome caps cookie lifetimes at 400 days; other browsers
|
|
70
|
-
* accept it as-is). The CSRF token is only meaningful when paired with a valid JWT, so it doesn't
|
|
71
|
-
* need its own expiration management. It gets regenerated on every fresh login.
|
|
72
|
-
*
|
|
73
|
-
* @category Internal
|
|
74
|
-
*/
|
|
75
|
-
export declare function generateCsrfCookie(csrfToken: string, cookieConfig: Readonly<SelectFrom<CookieParams, {
|
|
76
|
-
hostOrigin: true;
|
|
77
|
-
isDev: true;
|
|
78
|
-
}>>): string;
|
|
79
|
-
/**
|
|
80
|
-
* Generate a cookie value that will clear the previous auth cookie. Use this when signing out.
|
|
81
|
-
*
|
|
82
|
-
* @category Internal
|
|
83
|
-
*/
|
|
84
|
-
export declare function clearAuthCookie(cookieConfig: Readonly<SelectFrom<CookieParams, {
|
|
85
|
-
hostOrigin: true;
|
|
86
|
-
isDev: true;
|
|
87
|
-
}>> & PartialWithUndefined<{
|
|
88
|
-
authCookie: AuthCookie;
|
|
89
|
-
}>): string;
|
|
90
|
-
/**
|
|
91
|
-
* Generate a cookie value that will clear the CSRF token cookie. Use this when signing out.
|
|
92
|
-
*
|
|
93
|
-
* @category Internal
|
|
94
|
-
*/
|
|
95
|
-
export declare function clearCsrfCookie(cookieConfig: Readonly<SelectFrom<CookieParams, {
|
|
96
|
-
hostOrigin: true;
|
|
97
|
-
isDev: true;
|
|
98
|
-
}>>): string;
|
|
99
|
-
/**
|
|
100
|
-
* Generate a cookie string from a raw set of parameters.
|
|
101
|
-
*
|
|
102
|
-
* @category Internal
|
|
103
|
-
*/
|
|
104
|
-
export declare function generateCookie(params: Readonly<Record<string, Exclude<Primitive, symbol>>>): string;
|
|
105
|
-
/**
|
|
106
|
-
* Extract an auth cookie from a cookie string. Used in host (backend) code.
|
|
107
|
-
*
|
|
108
|
-
* @category Internal
|
|
109
|
-
* @returns The extracted auth Cookie JWT data or `undefined` if no valid auth JWT data was found.
|
|
110
|
-
*/
|
|
111
|
-
export declare function extractCookieJwt(rawCookie: string, jwtParams: Readonly<ParseJwtParams>, cookieName: AuthCookie): Promise<undefined | ParsedJwt<JwtUserData>>;
|
package/dist/cookie.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { check } from '@augment-vir/assert';
|
|
2
|
-
import { escapeStringForRegExp, safeMatch, } from '@augment-vir/common';
|
|
3
|
-
import { convertDuration } from 'date-vir';
|
|
4
|
-
import { parseUrl } from 'url-vir';
|
|
5
|
-
import { createUserJwt, parseUserJwt } from './jwt/user-jwt.js';
|
|
6
|
-
/**
|
|
7
|
-
* Cookie header names supported by default.
|
|
8
|
-
*
|
|
9
|
-
* @category Internal
|
|
10
|
-
*/
|
|
11
|
-
export var AuthCookie;
|
|
12
|
-
(function (AuthCookie) {
|
|
13
|
-
/** Used for a full user login auth. */
|
|
14
|
-
AuthCookie["Auth"] = "auth";
|
|
15
|
-
/** Use for a temporary "just signed up" auth. */
|
|
16
|
-
AuthCookie["SignUp"] = "sign-up";
|
|
17
|
-
/** Used for storing the CSRF token. Not `HttpOnly` so that frontend JS can read it. */
|
|
18
|
-
AuthCookie["Csrf"] = "auth-vir-csrf";
|
|
19
|
-
})(AuthCookie || (AuthCookie = {}));
|
|
20
|
-
function generateSetCookie({ name, value, httpOnly, cookieConfig, }) {
|
|
21
|
-
return generateCookie({
|
|
22
|
-
[name]: value,
|
|
23
|
-
Domain: parseUrl(cookieConfig.hostOrigin).hostname,
|
|
24
|
-
HttpOnly: httpOnly,
|
|
25
|
-
Path: '/',
|
|
26
|
-
SameSite: 'Strict',
|
|
27
|
-
'MAX-AGE': cookieConfig.cookieDuration
|
|
28
|
-
? convertDuration(cookieConfig.cookieDuration, {
|
|
29
|
-
seconds: true,
|
|
30
|
-
}).seconds
|
|
31
|
-
: 0,
|
|
32
|
-
Secure: !cookieConfig.isDev,
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Generate a secure cookie that stores the user JWT data. Used in host (backend) code.
|
|
37
|
-
*
|
|
38
|
-
* @category Internal
|
|
39
|
-
*/
|
|
40
|
-
export async function generateAuthCookie(userJwtData, cookieConfig) {
|
|
41
|
-
return generateSetCookie({
|
|
42
|
-
name: cookieConfig.authCookie || AuthCookie.Auth,
|
|
43
|
-
value: await createUserJwt(userJwtData, cookieConfig.jwtParams),
|
|
44
|
-
httpOnly: true,
|
|
45
|
-
cookieConfig,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Generate a CSRF token cookie. This cookie is intentionally not `HttpOnly` so that frontend
|
|
50
|
-
* JavaScript can read it and inject the value as a request header for double-submit verification.
|
|
51
|
-
*
|
|
52
|
-
* The CSRF cookie uses a fixed 400-day MAX-AGE rather than matching the auth cookie duration. 400
|
|
53
|
-
* days is the cross-browser safe maximum (Chrome caps cookie lifetimes at 400 days; other browsers
|
|
54
|
-
* accept it as-is). The CSRF token is only meaningful when paired with a valid JWT, so it doesn't
|
|
55
|
-
* need its own expiration management. It gets regenerated on every fresh login.
|
|
56
|
-
*
|
|
57
|
-
* @category Internal
|
|
58
|
-
*/
|
|
59
|
-
export function generateCsrfCookie(csrfToken, cookieConfig) {
|
|
60
|
-
return generateSetCookie({
|
|
61
|
-
name: AuthCookie.Csrf,
|
|
62
|
-
value: csrfToken,
|
|
63
|
-
httpOnly: false,
|
|
64
|
-
cookieConfig: {
|
|
65
|
-
...cookieConfig,
|
|
66
|
-
cookieDuration: {
|
|
67
|
-
days: 400,
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Generate a cookie value that will clear the previous auth cookie. Use this when signing out.
|
|
74
|
-
*
|
|
75
|
-
* @category Internal
|
|
76
|
-
*/
|
|
77
|
-
export function clearAuthCookie(cookieConfig) {
|
|
78
|
-
return generateSetCookie({
|
|
79
|
-
name: cookieConfig.authCookie || AuthCookie.Auth,
|
|
80
|
-
value: 'redacted',
|
|
81
|
-
httpOnly: true,
|
|
82
|
-
cookieConfig,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Generate a cookie value that will clear the CSRF token cookie. Use this when signing out.
|
|
87
|
-
*
|
|
88
|
-
* @category Internal
|
|
89
|
-
*/
|
|
90
|
-
export function clearCsrfCookie(cookieConfig) {
|
|
91
|
-
return generateSetCookie({
|
|
92
|
-
name: AuthCookie.Csrf,
|
|
93
|
-
value: 'redacted',
|
|
94
|
-
httpOnly: false,
|
|
95
|
-
cookieConfig,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Generate a cookie string from a raw set of parameters.
|
|
100
|
-
*
|
|
101
|
-
* @category Internal
|
|
102
|
-
*/
|
|
103
|
-
export function generateCookie(params) {
|
|
104
|
-
return Object.entries(params)
|
|
105
|
-
.map(([key, value,]) => {
|
|
106
|
-
if (value == undefined || value === false) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
else if (value === '' || value === true) {
|
|
110
|
-
return key;
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
return [
|
|
114
|
-
key,
|
|
115
|
-
value,
|
|
116
|
-
].join('=');
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
.filter(check.isTruthy)
|
|
120
|
-
.join('; ');
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Extract an auth cookie from a cookie string. Used in host (backend) code.
|
|
124
|
-
*
|
|
125
|
-
* @category Internal
|
|
126
|
-
* @returns The extracted auth Cookie JWT data or `undefined` if no valid auth JWT data was found.
|
|
127
|
-
*/
|
|
128
|
-
export async function extractCookieJwt(rawCookie, jwtParams, cookieName) {
|
|
129
|
-
const cookieRegExp = new RegExp(`${escapeStringForRegExp(cookieName)}=[^;]+(?:;|$)`);
|
|
130
|
-
const [cookieValue] = safeMatch(rawCookie, cookieRegExp);
|
|
131
|
-
if (!cookieValue) {
|
|
132
|
-
return undefined;
|
|
133
|
-
}
|
|
134
|
-
const rawJwt = cookieValue.replace(`${cookieName}=`, '').replace(';', '');
|
|
135
|
-
const jwt = await parseUserJwt(rawJwt, jwtParams);
|
|
136
|
-
return jwt;
|
|
137
|
-
}
|
package/dist/csrf-token.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type RequireExactlyOne } from 'type-fest';
|
|
2
|
-
/**
|
|
3
|
-
* Generates a random, cryptographically secure CSRF token string.
|
|
4
|
-
*
|
|
5
|
-
* @category Internal
|
|
6
|
-
*/
|
|
7
|
-
export declare function generateCsrfToken(): string;
|
|
8
|
-
/**
|
|
9
|
-
* Options for specifying the CSRF token header name.
|
|
10
|
-
*
|
|
11
|
-
* @category Auth : Client
|
|
12
|
-
* @category Auth : Host
|
|
13
|
-
*/
|
|
14
|
-
export type CsrfHeaderNameOption = RequireExactlyOne<{
|
|
15
|
-
/** Prefix used to generate the header name: `${prefix}-auth-vir-csrf-token`. */
|
|
16
|
-
csrfHeaderPrefix: string;
|
|
17
|
-
/** Overrides the entire CSRF header name. */
|
|
18
|
-
csrfHeaderName: string;
|
|
19
|
-
}>;
|
|
20
|
-
/**
|
|
21
|
-
* Resolves a {@link CsrfHeaderNameOption} to the actual header name string.
|
|
22
|
-
*
|
|
23
|
-
* @category Auth : Client
|
|
24
|
-
* @category Auth : Host
|
|
25
|
-
*/
|
|
26
|
-
export declare function resolveCsrfHeaderName(options: Readonly<CsrfHeaderNameOption>): string;
|
|
27
|
-
/**
|
|
28
|
-
* Used in client (frontend) code to retrieve the current CSRF token from the browser cookie in
|
|
29
|
-
* order to send it with requests to the host (backend).
|
|
30
|
-
*
|
|
31
|
-
* @category Auth : Client
|
|
32
|
-
*/
|
|
33
|
-
export declare function getCurrentCsrfToken(): string | undefined;
|
package/dist/csrf-token.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { check } from '@augment-vir/assert';
|
|
2
|
-
import { escapeStringForRegExp, randomString, safeMatch } from '@augment-vir/common';
|
|
3
|
-
import { AuthCookie } from './cookie.js';
|
|
4
|
-
/**
|
|
5
|
-
* Generates a random, cryptographically secure CSRF token string.
|
|
6
|
-
*
|
|
7
|
-
* @category Internal
|
|
8
|
-
*/
|
|
9
|
-
export function generateCsrfToken() {
|
|
10
|
-
return randomString(256);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Resolves a {@link CsrfHeaderNameOption} to the actual header name string.
|
|
14
|
-
*
|
|
15
|
-
* @category Auth : Client
|
|
16
|
-
* @category Auth : Host
|
|
17
|
-
*/
|
|
18
|
-
export function resolveCsrfHeaderName(options) {
|
|
19
|
-
if ('csrfHeaderName' in options && options.csrfHeaderName) {
|
|
20
|
-
return options.csrfHeaderName;
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
return [
|
|
24
|
-
options.csrfHeaderPrefix,
|
|
25
|
-
'auth-vir',
|
|
26
|
-
'csrf-token',
|
|
27
|
-
]
|
|
28
|
-
.filter(check.isTruthy)
|
|
29
|
-
.join('-');
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Used in client (frontend) code to retrieve the current CSRF token from the browser cookie in
|
|
34
|
-
* order to send it with requests to the host (backend).
|
|
35
|
-
*
|
|
36
|
-
* @category Auth : Client
|
|
37
|
-
*/
|
|
38
|
-
export function getCurrentCsrfToken() {
|
|
39
|
-
const cookieRegExp = new RegExp(`${escapeStringForRegExp(AuthCookie.Csrf)}=([^;]+)`);
|
|
40
|
-
const [, value,] = safeMatch(globalThis.document.cookie, cookieRegExp);
|
|
41
|
-
return value || undefined;
|
|
42
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* !!! This is code generated by Prisma. Do not edit directly. !!! */
|
|
2
|
-
/* eslint-disable */
|
|
3
|
-
// biome-ignore-all lint: generated file
|
|
4
|
-
// @ts-nocheck
|
|
5
|
-
/*
|
|
6
|
-
* This file should be your main import to use Prisma-related types and utilities in a browser.
|
|
7
|
-
* Use it to get access to models, enums, and input types.
|
|
8
|
-
*
|
|
9
|
-
* This file does not contain a `PrismaClient` class, nor several other helpers that are intended as server-side only.
|
|
10
|
-
* See `client.ts` for the standard, server-side entry point.
|
|
11
|
-
*
|
|
12
|
-
* 🟢 You can import this file directly.
|
|
13
|
-
*/
|
|
14
|
-
import * as Prisma from './internal/prismaNamespaceBrowser.js';
|
|
15
|
-
export { Prisma };
|
|
16
|
-
export * as $Enums from './enums.js';
|
|
17
|
-
export * from './enums.js';
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import * as runtime from "@prisma/client/runtime/client";
|
|
2
|
-
import * as $Class from "./internal/class.js";
|
|
3
|
-
import * as Prisma from "./internal/prismaNamespace.js";
|
|
4
|
-
export * as $Enums from './enums.js';
|
|
5
|
-
export * from "./enums.js";
|
|
6
|
-
/**
|
|
7
|
-
* ## Prisma Client
|
|
8
|
-
*
|
|
9
|
-
* Type-safe database client for TypeScript
|
|
10
|
-
* @example
|
|
11
|
-
* ```
|
|
12
|
-
* const prisma = new PrismaClient()
|
|
13
|
-
* // Fetch zero or more Users
|
|
14
|
-
* const users = await prisma.user.findMany()
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* Read more in our [docs](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client).
|
|
18
|
-
*/
|
|
19
|
-
export declare const PrismaClient: $Class.PrismaClientConstructor;
|
|
20
|
-
export type PrismaClient<LogOpts extends Prisma.LogLevel = never, OmitOpts extends Prisma.PrismaClientOptions["omit"] = Prisma.PrismaClientOptions["omit"], ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = $Class.PrismaClient<LogOpts, OmitOpts, ExtArgs>;
|
|
21
|
-
export { Prisma };
|
|
22
|
-
/**
|
|
23
|
-
* Model User
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
export type User = Prisma.UserModel;
|
package/dist/generated/client.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/* !!! This is code generated by Prisma. Do not edit directly. !!! */
|
|
2
|
-
/* eslint-disable */
|
|
3
|
-
// biome-ignore-all lint: generated file
|
|
4
|
-
// @ts-nocheck
|
|
5
|
-
/*
|
|
6
|
-
* This file should be your main import to use Prisma. Through it you get access to all the models, enums, and input types.
|
|
7
|
-
* If you're looking for something you can import in the client-side of your application, please refer to the `browser.ts` file instead.
|
|
8
|
-
*
|
|
9
|
-
* 🟢 You can import this file directly.
|
|
10
|
-
*/
|
|
11
|
-
import * as path from 'node:path';
|
|
12
|
-
import { fileURLToPath } from 'node:url';
|
|
13
|
-
globalThis['__dirname'] = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
-
import * as $Class from "./internal/class.js";
|
|
15
|
-
import * as Prisma from "./internal/prismaNamespace.js";
|
|
16
|
-
export * as $Enums from './enums.js';
|
|
17
|
-
export * from "./enums.js";
|
|
18
|
-
/**
|
|
19
|
-
* ## Prisma Client
|
|
20
|
-
*
|
|
21
|
-
* Type-safe database client for TypeScript
|
|
22
|
-
* @example
|
|
23
|
-
* ```
|
|
24
|
-
* const prisma = new PrismaClient()
|
|
25
|
-
* // Fetch zero or more Users
|
|
26
|
-
* const users = await prisma.user.findMany()
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* Read more in our [docs](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client).
|
|
30
|
-
*/
|
|
31
|
-
export const PrismaClient = $Class.getPrismaClientClass(__dirname);
|
|
32
|
-
export { Prisma };
|