-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensureCookies.ts
More file actions
194 lines (177 loc) · 4.37 KB
/
ensureCookies.ts
File metadata and controls
194 lines (177 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { verifyCookieJwt } from "./verifyCookieJwt.js";
import { refreshAccessToken } from "./refreshAccessToken.js";
export interface EnsureCookiesInput {
path: string;
cookies: Record<string, string | undefined>;
}
export interface CookiePayload {
sub: string;
token?: string;
refreshToken?: string;
roles?: string[];
email?: string;
phone?: string;
}
export interface CookieInstruction {
name: string;
value: CookiePayload;
ttl: number;
domain?: string;
}
export interface EnsureCookiesResult {
type: "ok" | "error";
status?: number;
error?: string;
user?: {
sub: string;
roles?: string[];
};
setCookies?: CookieInstruction[];
clearCookies?: string[];
}
export interface EnsureCookiesOptions {
authServerUrl: string;
cookieDomain?: string;
accessCookieName: string;
registrationCookieName: string;
refreshCookieName: string;
preAuthCookieName: string;
cookieSecret: string;
serviceSecret: string;
issuer: string;
audience: string;
keyId: string;
}
const COOKIE_REQUIREMENTS: Record<
string,
{ name: keyof EnsureCookiesOptions; required: boolean }
> = {
"/webAuthn/login/finish": { name: "preAuthCookieName", required: true },
"/webAuthn/login/start": { name: "preAuthCookieName", required: true },
"/webAuthn/register/start": {
name: "registrationCookieName",
required: true,
},
"/webAuthn/register/finish": {
name: "registrationCookieName",
required: true,
},
"/otp/verify-email-otp": {
name: "registrationCookieName",
required: true,
},
"/otp/verify-phone-otp": {
name: "registrationCookieName",
required: true,
},
"/otp/generate-email-otp": {
name: "registrationCookieName",
required: true,
},
"/otp/generate-phone-otp": {
name: "registrationCookieName",
required: true,
},
"/logout": { name: "accessCookieName", required: true },
"/users/me": { name: "accessCookieName", required: true },
};
export async function ensureCookies(
input: EnsureCookiesInput,
opts: EnsureCookiesOptions,
): Promise<EnsureCookiesResult> {
const match = Object.entries(COOKIE_REQUIREMENTS).find(([path]) =>
input.path.startsWith(path),
);
if (!match) {
console.debug(
"[SEAMLESS-AUTH-CORE] - (ensureCookies) - No cookie requirements for this path. Path: ",
input.path,
);
return { type: "ok" };
}
const [, { name, required }] = match;
const cookieName = opts[name];
if (!cookieName) {
return {
type: "error",
status: 400,
error: "Missing required cookie",
};
}
const cookieValue = input.cookies[cookieName];
const refreshCookie = input.cookies[opts.refreshCookieName];
if (required && !cookieValue) {
if (!refreshCookie) {
return {
type: "error",
status: 400,
error: `Missing required cookie "${cookieName}"`,
};
}
const refreshed = await refreshAccessToken(refreshCookie, {
authServerUrl: opts.authServerUrl,
cookieSecret: opts.cookieSecret,
serviceSecret: opts.serviceSecret,
issuer: opts.issuer,
audience: opts.audience,
keyId: opts.keyId,
});
if (!refreshed?.token) {
return {
type: "error",
status: 401,
error: "Refresh failed",
clearCookies: [
cookieName,
opts.registrationCookieName,
opts.refreshCookieName,
],
};
}
return {
type: "ok",
user: {
sub: refreshed.sub,
roles: refreshed.roles,
},
setCookies: [
{
name: cookieName,
value: {
sub: refreshed.sub,
roles: refreshed.roles,
},
ttl: refreshed.ttl,
domain: opts.cookieDomain,
},
{
name: opts.refreshCookieName,
value: {
sub: refreshed.sub,
refreshToken: refreshed.refreshToken,
},
ttl: refreshed.refreshTtl,
domain: opts.cookieDomain,
},
],
};
}
if (cookieValue) {
const payload = verifyCookieJwt(cookieValue, opts.cookieSecret);
if (!payload) {
return {
type: "error",
status: 401,
error: `Invalid or expired ${cookieName} cookie`,
};
}
return {
type: "ok",
user: {
sub: payload.sub as string,
roles: payload.roles as string[] | undefined,
},
};
}
return { type: "ok" };
}