auth-vir 1.1.0 → 1.2.1
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/auth.d.ts +1 -1
- package/dist/auth.js +2 -2
- package/dist/cookie.d.ts +1 -1
- package/dist/cookie.js +4 -3
- package/package.json +1 -1
- package/src/auth.ts +2 -1
- package/src/cookie.ts +5 -2
package/dist/auth.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type HeaderContainer = Record<string, string[] | undefined | string | num
|
|
|
14
14
|
* @category Auth : Host
|
|
15
15
|
* @returns The extracted user id or `undefined` if no valid auth headers exist.
|
|
16
16
|
*/
|
|
17
|
-
export declare function extractUserIdFromRequestHeaders(headers: HeaderContainer, jwtParams: Readonly<ParseJwtParams
|
|
17
|
+
export declare function extractUserIdFromRequestHeaders(headers: HeaderContainer, jwtParams: Readonly<ParseJwtParams>, cookieName?: string | undefined): Promise<string | undefined>;
|
|
18
18
|
/**
|
|
19
19
|
* Used by host (backend) code to set headers on a response object.
|
|
20
20
|
*
|
package/dist/auth.js
CHANGED
|
@@ -25,14 +25,14 @@ function readHeader(headers, headerName) {
|
|
|
25
25
|
* @category Auth : Host
|
|
26
26
|
* @returns The extracted user id or `undefined` if no valid auth headers exist.
|
|
27
27
|
*/
|
|
28
|
-
export async function extractUserIdFromRequestHeaders(headers, jwtParams) {
|
|
28
|
+
export async function extractUserIdFromRequestHeaders(headers, jwtParams, cookieName) {
|
|
29
29
|
try {
|
|
30
30
|
const csrfToken = readHeader(headers, csrfTokenHeaderName);
|
|
31
31
|
const cookie = readHeader(headers, 'cookie');
|
|
32
32
|
if (!cookie || !csrfToken) {
|
|
33
33
|
return undefined;
|
|
34
34
|
}
|
|
35
|
-
const jwt = await extractCookieJwt(cookie, jwtParams);
|
|
35
|
+
const jwt = await extractCookieJwt(cookie, jwtParams, cookieName);
|
|
36
36
|
if (!jwt || jwt.csrfToken !== csrfToken) {
|
|
37
37
|
return undefined;
|
|
38
38
|
}
|
package/dist/cookie.d.ts
CHANGED
|
@@ -61,4 +61,4 @@ export declare function generateCookie(params: Readonly<Record<string, Exclude<P
|
|
|
61
61
|
* @category Internal
|
|
62
62
|
* @returns The extracted auth Cookie JWT data or `undefined` if no valid auth JWT data was found.
|
|
63
63
|
*/
|
|
64
|
-
export declare function extractCookieJwt(rawCookie: string, jwtParams: Readonly<ParseJwtParams
|
|
64
|
+
export declare function extractCookieJwt(rawCookie: string, jwtParams: Readonly<ParseJwtParams>, cookieName?: string): Promise<undefined | UserJwtData>;
|
package/dist/cookie.js
CHANGED
|
@@ -65,12 +65,13 @@ export function generateCookie(params) {
|
|
|
65
65
|
* @category Internal
|
|
66
66
|
* @returns The extracted auth Cookie JWT data or `undefined` if no valid auth JWT data was found.
|
|
67
67
|
*/
|
|
68
|
-
export async function extractCookieJwt(rawCookie, jwtParams) {
|
|
69
|
-
const
|
|
68
|
+
export async function extractCookieJwt(rawCookie, jwtParams, cookieName = 'auth') {
|
|
69
|
+
const cookieRegExp = new RegExp(`${cookieName}=[^;]+(?:;|$)`);
|
|
70
|
+
const [auth] = safeMatch(rawCookie, cookieRegExp);
|
|
70
71
|
if (!auth) {
|
|
71
72
|
return undefined;
|
|
72
73
|
}
|
|
73
|
-
const rawJwt = auth.replace(
|
|
74
|
+
const rawJwt = auth.replace(`${cookieName}=`, '').replace(';', '');
|
|
74
75
|
const jwt = await parseUserJwt(rawJwt, jwtParams);
|
|
75
76
|
return jwt;
|
|
76
77
|
}
|
package/package.json
CHANGED
package/src/auth.ts
CHANGED
|
@@ -41,6 +41,7 @@ function readHeader(headers: HeaderContainer, headerName: string): string | unde
|
|
|
41
41
|
export async function extractUserIdFromRequestHeaders(
|
|
42
42
|
headers: HeaderContainer,
|
|
43
43
|
jwtParams: Readonly<ParseJwtParams>,
|
|
44
|
+
cookieName?: string | undefined,
|
|
44
45
|
): Promise<string | undefined> {
|
|
45
46
|
try {
|
|
46
47
|
const csrfToken = readHeader(headers, csrfTokenHeaderName);
|
|
@@ -50,7 +51,7 @@ export async function extractUserIdFromRequestHeaders(
|
|
|
50
51
|
return undefined;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
const jwt = await extractCookieJwt(cookie, jwtParams);
|
|
54
|
+
const jwt = await extractCookieJwt(cookie, jwtParams, cookieName);
|
|
54
55
|
|
|
55
56
|
if (!jwt || jwt.csrfToken !== csrfToken) {
|
|
56
57
|
return undefined;
|
package/src/cookie.ts
CHANGED
|
@@ -122,14 +122,17 @@ export function generateCookie(
|
|
|
122
122
|
export async function extractCookieJwt(
|
|
123
123
|
rawCookie: string,
|
|
124
124
|
jwtParams: Readonly<ParseJwtParams>,
|
|
125
|
+
cookieName: string = 'auth',
|
|
125
126
|
): Promise<undefined | UserJwtData> {
|
|
126
|
-
const
|
|
127
|
+
const cookieRegExp = new RegExp(`${cookieName}=[^;]+(?:;|$)`);
|
|
128
|
+
|
|
129
|
+
const [auth] = safeMatch(rawCookie, cookieRegExp);
|
|
127
130
|
|
|
128
131
|
if (!auth) {
|
|
129
132
|
return undefined;
|
|
130
133
|
}
|
|
131
134
|
|
|
132
|
-
const rawJwt = auth.replace(
|
|
135
|
+
const rawJwt = auth.replace(`${cookieName}=`, '').replace(';', '');
|
|
133
136
|
|
|
134
137
|
const jwt = await parseUserJwt(rawJwt, jwtParams);
|
|
135
138
|
|