dauth-md-node 3.0.2 → 4.1.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/src/router.ts ADDED
@@ -0,0 +1,428 @@
1
+ import { Router, Request, Response } from 'express';
2
+ import jwt from 'jsonwebtoken';
3
+ import { getServerBasePath } from './api/utils/config';
4
+ import {
5
+ deriveEncryptionKey,
6
+ encryptSession,
7
+ decryptSessionWithKeys,
8
+ SessionPayload,
9
+ } from './session';
10
+ import { generateCsrfToken, verifyCsrf } from './csrf';
11
+
12
+ export interface DauthRouterOptions {
13
+ domainName: string;
14
+ tsk: string;
15
+ dauthUrl?: string;
16
+ cookieName?: string;
17
+ csrfCookieName?: string;
18
+ maxAge?: number;
19
+ secure?: boolean;
20
+ previousTsk?: string;
21
+ sessionSalt?: string;
22
+ }
23
+
24
+ interface ResolvedConfig {
25
+ domainName: string;
26
+ dauthBasePath: string;
27
+ cookieName: string;
28
+ csrfCookieName: string;
29
+ maxAgeMs: number;
30
+ secure: boolean;
31
+ encKeys: Buffer[];
32
+ }
33
+
34
+ // Refresh lock to prevent race conditions on concurrent token rotation
35
+ const refreshLocks = new Map<string, Promise<SessionPayload | null>>();
36
+
37
+ function lockKey(refreshToken: string): string {
38
+ return refreshToken.substring(0, 16);
39
+ }
40
+
41
+ function clearStaleLocks(): void {
42
+ if (refreshLocks.size > 100) refreshLocks.clear();
43
+ }
44
+
45
+ async function resolveConfig(
46
+ opts: DauthRouterOptions
47
+ ): Promise<ResolvedConfig> {
48
+ const secure = opts.secure ?? process.env.NODE_ENV !== 'development';
49
+ const cookieName =
50
+ opts.cookieName ?? (secure ? '__Host-dauth-session' : 'dauth-session');
51
+ const csrfCookieName =
52
+ opts.csrfCookieName ?? (secure ? '__Host-csrf' : 'csrf-token');
53
+ const maxAgeMs = (opts.maxAge ?? 30 * 24 * 3600) * 1000;
54
+
55
+ const keys: Buffer[] = [];
56
+ keys.push(await deriveEncryptionKey(opts.tsk, opts.sessionSalt));
57
+ if (opts.previousTsk) {
58
+ keys.push(await deriveEncryptionKey(opts.previousTsk, opts.sessionSalt));
59
+ }
60
+
61
+ let dauthBasePath: string;
62
+ if (opts.dauthUrl) {
63
+ dauthBasePath = `${opts.dauthUrl.replace(/\/+$/, '')}/api/v1`;
64
+ } else {
65
+ dauthBasePath = getServerBasePath();
66
+ }
67
+
68
+ return {
69
+ domainName: opts.domainName,
70
+ dauthBasePath,
71
+ cookieName,
72
+ csrfCookieName,
73
+ maxAgeMs,
74
+ secure,
75
+ encKeys: keys,
76
+ };
77
+ }
78
+
79
+ function setSessionCookie(
80
+ res: Response,
81
+ payload: SessionPayload,
82
+ config: ResolvedConfig
83
+ ): void {
84
+ const encrypted = encryptSession(payload, config.encKeys[0]);
85
+ const cookieOpts: Record<string, unknown> = {
86
+ httpOnly: true,
87
+ secure: config.secure,
88
+ sameSite: 'lax',
89
+ maxAge: config.maxAgeMs,
90
+ path: '/',
91
+ };
92
+ // __Host- prefix requires no domain attribute
93
+ if (!config.secure) {
94
+ // Dev mode: no __Host- prefix, no domain restriction needed
95
+ }
96
+ res.cookie(config.cookieName, encrypted, cookieOpts);
97
+ }
98
+
99
+ function setCsrfCookie(res: Response, config: ResolvedConfig): void {
100
+ const csrfToken = generateCsrfToken();
101
+ res.cookie(config.csrfCookieName, csrfToken, {
102
+ httpOnly: false,
103
+ secure: config.secure,
104
+ sameSite: 'lax',
105
+ maxAge: config.maxAgeMs,
106
+ path: '/',
107
+ });
108
+ }
109
+
110
+ function clearCookies(res: Response, config: ResolvedConfig): void {
111
+ const baseOpts = { path: '/', secure: config.secure };
112
+ res.clearCookie(config.cookieName, baseOpts);
113
+ res.clearCookie(config.csrfCookieName, baseOpts);
114
+ }
115
+
116
+ function readSession(
117
+ req: Request,
118
+ config: ResolvedConfig
119
+ ): SessionPayload | null {
120
+ const cookie = req.cookies?.[config.cookieName];
121
+ if (!cookie) return null;
122
+ return decryptSessionWithKeys(cookie, config.encKeys);
123
+ }
124
+
125
+ function isTokenExpiringSoon(token: string, thresholdMs = 300_000): boolean {
126
+ try {
127
+ const decoded = jwt.decode(token) as { exp?: number } | null;
128
+ if (!decoded?.exp) return true;
129
+ return decoded.exp * 1000 - Date.now() < thresholdMs;
130
+ } catch {
131
+ return true;
132
+ }
133
+ }
134
+
135
+ async function maybeRefreshTokens(
136
+ session: SessionPayload,
137
+ config: ResolvedConfig,
138
+ res: Response
139
+ ): Promise<SessionPayload> {
140
+ if (!isTokenExpiringSoon(session.accessToken)) return session;
141
+
142
+ const key = lockKey(session.refreshToken);
143
+ clearStaleLocks();
144
+
145
+ const existingLock = refreshLocks.get(key);
146
+ if (existingLock) {
147
+ const result = await existingLock;
148
+ return result ?? session;
149
+ }
150
+
151
+ const refreshPromise = (async (): Promise<SessionPayload | null> => {
152
+ try {
153
+ const response = await fetch(
154
+ `${config.dauthBasePath}/app/${config.domainName}/refresh-token`,
155
+ {
156
+ method: 'POST',
157
+ headers: { 'Content-Type': 'application/json' },
158
+ body: JSON.stringify({
159
+ refreshToken: session.refreshToken,
160
+ }),
161
+ }
162
+ );
163
+ if (!response.ok) return null;
164
+ const data = (await response.json()) as {
165
+ accessToken?: string;
166
+ refreshToken?: string;
167
+ };
168
+ if (!data.accessToken || !data.refreshToken) return null;
169
+ const newSession: SessionPayload = {
170
+ accessToken: data.accessToken,
171
+ refreshToken: data.refreshToken,
172
+ };
173
+ setSessionCookie(res, newSession, config);
174
+ return newSession;
175
+ } catch {
176
+ return null;
177
+ }
178
+ })();
179
+
180
+ refreshLocks.set(key, refreshPromise);
181
+
182
+ // Timeout safety net: clean lock after 10s
183
+ const timeout = setTimeout(() => refreshLocks.delete(key), 10_000);
184
+ refreshPromise.finally(() => {
185
+ clearTimeout(timeout);
186
+ refreshLocks.delete(key);
187
+ });
188
+
189
+ const result = await refreshPromise;
190
+ return result ?? session;
191
+ }
192
+
193
+ export function dauthRouter(opts: DauthRouterOptions): Router {
194
+ const router = Router();
195
+ let configPromise: Promise<ResolvedConfig> | null = null;
196
+
197
+ async function getConfig(): Promise<ResolvedConfig> {
198
+ if (!configPromise) configPromise = resolveConfig(opts);
199
+ return configPromise;
200
+ }
201
+
202
+ // POST /exchange-code — no CSRF (no prior session)
203
+ router.post('/exchange-code', async (req: Request, res: Response) => {
204
+ const config = await getConfig();
205
+ const { code } = req.body;
206
+ if (!code) {
207
+ return res
208
+ .status(400)
209
+ .send({ status: 'code-required', message: 'Code required' });
210
+ }
211
+
212
+ const response = await fetch(
213
+ `${config.dauthBasePath}/app/${config.domainName}/exchange-code`,
214
+ {
215
+ method: 'POST',
216
+ headers: { 'Content-Type': 'application/json' },
217
+ body: JSON.stringify({ code }),
218
+ }
219
+ );
220
+ if (!response.ok) {
221
+ return res
222
+ .status(response.status)
223
+ .send({ status: 'code-invalid', message: 'Code invalid' });
224
+ }
225
+ const data = (await response.json()) as {
226
+ accessToken: string;
227
+ refreshToken: string;
228
+ isNewUser: boolean;
229
+ };
230
+
231
+ setSessionCookie(
232
+ res,
233
+ {
234
+ accessToken: data.accessToken,
235
+ refreshToken: data.refreshToken,
236
+ },
237
+ config
238
+ );
239
+ setCsrfCookie(res, config);
240
+
241
+ // Fetch user data to return
242
+ const userResponse = await fetch(
243
+ `${config.dauthBasePath}/app/${config.domainName}/user`,
244
+ {
245
+ method: 'GET',
246
+ headers: { Authorization: data.accessToken },
247
+ }
248
+ );
249
+ const userData = (await userResponse.json()) as {
250
+ user?: unknown;
251
+ domain?: unknown;
252
+ };
253
+
254
+ return res.status(200).send({
255
+ user: userData.user,
256
+ domain: userData.domain,
257
+ isNewUser: data.isNewUser,
258
+ });
259
+ });
260
+
261
+ // GET /session — no CSRF (read-only)
262
+ router.get('/session', async (req: Request, res: Response) => {
263
+ const config = await getConfig();
264
+ const session = readSession(req, config);
265
+ if (!session) {
266
+ return res
267
+ .status(401)
268
+ .send({ status: 'no-session', message: 'Not authenticated' });
269
+ }
270
+
271
+ const refreshed = await maybeRefreshTokens(session, config, res);
272
+
273
+ const userResponse = await fetch(
274
+ `${config.dauthBasePath}/app/${config.domainName}/user`,
275
+ {
276
+ method: 'GET',
277
+ headers: { Authorization: refreshed.accessToken },
278
+ }
279
+ );
280
+ if (!userResponse.ok) {
281
+ clearCookies(res, config);
282
+ return res
283
+ .status(401)
284
+ .send({ status: 'session-invalid', message: 'Session expired' });
285
+ }
286
+ const userData = (await userResponse.json()) as {
287
+ user?: unknown;
288
+ domain?: unknown;
289
+ };
290
+ return res.status(200).send({
291
+ user: userData.user,
292
+ domain: userData.domain,
293
+ });
294
+ });
295
+
296
+ // POST /logout — CSRF required
297
+ router.post('/logout', async (req: Request, res: Response) => {
298
+ const config = await getConfig();
299
+ if (!verifyCsrf(req, config.csrfCookieName)) {
300
+ return res
301
+ .status(403)
302
+ .send({ status: 'csrf-invalid', message: 'CSRF token invalid' });
303
+ }
304
+ const session = readSession(req, config);
305
+ if (session) {
306
+ // Revoke refresh token server-to-server (fire-and-forget)
307
+ fetch(`${config.dauthBasePath}/app/${config.domainName}/logout`, {
308
+ method: 'POST',
309
+ headers: { 'Content-Type': 'application/json' },
310
+ body: JSON.stringify({
311
+ refreshToken: session.refreshToken,
312
+ }),
313
+ }).catch(() => {});
314
+ }
315
+ clearCookies(res, config);
316
+ return res.status(200).send({ status: 'success', message: 'Logged out' });
317
+ });
318
+
319
+ // PATCH /user — CSRF required
320
+ router.patch('/user', async (req: Request, res: Response) => {
321
+ const config = await getConfig();
322
+ if (!verifyCsrf(req, config.csrfCookieName)) {
323
+ return res
324
+ .status(403)
325
+ .send({ status: 'csrf-invalid', message: 'CSRF token invalid' });
326
+ }
327
+ const session = readSession(req, config);
328
+ if (!session) {
329
+ return res
330
+ .status(401)
331
+ .send({ status: 'no-session', message: 'Not authenticated' });
332
+ }
333
+ const refreshed = await maybeRefreshTokens(session, config, res);
334
+
335
+ const response = await fetch(
336
+ `${config.dauthBasePath}/app/${config.domainName}/user`,
337
+ {
338
+ method: 'PATCH',
339
+ headers: {
340
+ 'Content-Type': 'application/json',
341
+ Authorization: refreshed.accessToken,
342
+ },
343
+ body: JSON.stringify(req.body),
344
+ }
345
+ );
346
+ const data = await response.json();
347
+ return res.status(response.status).send(data);
348
+ });
349
+
350
+ // DELETE /user — CSRF required
351
+ router.delete('/user', async (req: Request, res: Response) => {
352
+ const config = await getConfig();
353
+ if (!verifyCsrf(req, config.csrfCookieName)) {
354
+ return res
355
+ .status(403)
356
+ .send({ status: 'csrf-invalid', message: 'CSRF token invalid' });
357
+ }
358
+ const session = readSession(req, config);
359
+ if (!session) {
360
+ return res
361
+ .status(401)
362
+ .send({ status: 'no-session', message: 'Not authenticated' });
363
+ }
364
+
365
+ const response = await fetch(
366
+ `${config.dauthBasePath}/app/${config.domainName}/user`,
367
+ {
368
+ method: 'DELETE',
369
+ headers: { Authorization: session.accessToken },
370
+ }
371
+ );
372
+ const data = await response.json();
373
+ clearCookies(res, config);
374
+ return res.status(response.status).send(data);
375
+ });
376
+
377
+ // GET /profile-redirect — CSRF required (generates profile code)
378
+ router.get('/profile-redirect', async (req: Request, res: Response) => {
379
+ const config = await getConfig();
380
+ if (!verifyCsrf(req, config.csrfCookieName)) {
381
+ return res.status(403).send({
382
+ status: 'csrf-invalid',
383
+ message: 'CSRF token invalid',
384
+ });
385
+ }
386
+ const session = readSession(req, config);
387
+ if (!session) {
388
+ return res.status(401).send({
389
+ status: 'no-session',
390
+ message: 'Not authenticated',
391
+ });
392
+ }
393
+ const refreshed = await maybeRefreshTokens(session, config, res);
394
+
395
+ const response = await fetch(
396
+ `${config.dauthBasePath}/app/${config.domainName}/profile-code`,
397
+ {
398
+ method: 'POST',
399
+ headers: {
400
+ 'Content-Type': 'application/json',
401
+ Authorization: refreshed.accessToken,
402
+ },
403
+ }
404
+ );
405
+ if (!response.ok) {
406
+ return res.status(response.status).send({
407
+ status: 'profile-code-error',
408
+ message: 'Could not generate profile code',
409
+ });
410
+ }
411
+ const data = (await response.json()) as { code: string };
412
+
413
+ // Build redirect URL to dauth frontend
414
+ const dauthFrontendUrl = opts.dauthUrl
415
+ ? opts.dauthUrl.replace(/\/+$/, '')
416
+ : process.env.DAUTH_URL
417
+ ? process.env.DAUTH_URL.replace(/\/+$/, '')
418
+ : process.env.NODE_ENV === 'development'
419
+ ? 'http://localhost:5185'
420
+ : 'https://dauth.ovh';
421
+
422
+ return res.status(200).send({
423
+ redirectUrl: `${dauthFrontendUrl}/${config.domainName}/update-user?code=${data.code}`,
424
+ });
425
+ });
426
+
427
+ return router;
428
+ }
package/src/session.ts ADDED
@@ -0,0 +1,78 @@
1
+ import crypto from 'crypto';
2
+
3
+ export interface SessionPayload {
4
+ accessToken: string;
5
+ refreshToken: string;
6
+ }
7
+
8
+ const INFO = 'dauth-cookie-enc-v1';
9
+ const DEFAULT_SALT = Buffer.from(
10
+ 'a3f8c1d7e9b24f6081c5d3a7e2f49b0653d81f7a2e94c0b6d8f3a5e1c7b09d42',
11
+ 'hex'
12
+ );
13
+
14
+ export async function deriveEncryptionKey(
15
+ tsk: string,
16
+ salt?: string
17
+ ): Promise<Buffer> {
18
+ const saltBuf = salt ? Buffer.from(salt, 'hex') : DEFAULT_SALT;
19
+ return new Promise((resolve, reject) => {
20
+ crypto.hkdf(
21
+ 'sha256',
22
+ Buffer.from(tsk),
23
+ saltBuf,
24
+ INFO,
25
+ 32,
26
+ (err, derivedKey) => {
27
+ if (err) return reject(err);
28
+ resolve(Buffer.from(derivedKey));
29
+ }
30
+ );
31
+ });
32
+ }
33
+
34
+ export function encryptSession(payload: SessionPayload, key: Buffer): string {
35
+ const nonce = crypto.randomBytes(12);
36
+ const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce);
37
+ const plaintext = JSON.stringify(payload);
38
+ const encrypted = Buffer.concat([
39
+ cipher.update(plaintext, 'utf8'),
40
+ cipher.final(),
41
+ ]);
42
+ const authTag = cipher.getAuthTag();
43
+ // Format: base64(nonce + ciphertext + authTag)
44
+ return Buffer.concat([nonce, encrypted, authTag]).toString('base64');
45
+ }
46
+
47
+ export function decryptSession(
48
+ ciphertext: string,
49
+ key: Buffer
50
+ ): SessionPayload | null {
51
+ try {
52
+ const buf = Buffer.from(ciphertext, 'base64');
53
+ if (buf.length < 12 + 16) return null; // nonce(12) + authTag(16) minimum
54
+ const nonce = buf.subarray(0, 12);
55
+ const authTag = buf.subarray(buf.length - 16);
56
+ const encrypted = buf.subarray(12, buf.length - 16);
57
+ const decipher = crypto.createDecipheriv('aes-256-gcm', key, nonce);
58
+ decipher.setAuthTag(authTag);
59
+ const decrypted = Buffer.concat([
60
+ decipher.update(encrypted),
61
+ decipher.final(),
62
+ ]);
63
+ return JSON.parse(decrypted.toString('utf8')) as SessionPayload;
64
+ } catch {
65
+ return null;
66
+ }
67
+ }
68
+
69
+ export function decryptSessionWithKeys(
70
+ ciphertext: string,
71
+ keys: Buffer[]
72
+ ): SessionPayload | null {
73
+ for (const key of keys) {
74
+ const result = decryptSession(ciphertext, key);
75
+ if (result) return result;
76
+ }
77
+ return null;
78
+ }