refresh-tokens.js (3112B)
1 import crypto from 'node:crypto'; 2 3 export const REFRESH_TOKEN_TTL = 7 * 24 * 60 * 60; 4 5 function hashToken(token) { 6 return crypto.createHash('sha256').update(String(token || '')).digest('hex'); 7 } 8 9 function nextToken() { 10 return crypto.randomBytes(32).toString('base64url'); 11 } 12 13 async function insertRefreshToken(tx, { 14 token, 15 familyId, 16 sessionId, 17 userId, 18 realmId, 19 clientId, 20 scopes, 21 now, 22 }) { 23 const id = hashToken(token); 24 const expiresAt = now + REFRESH_TOKEN_TTL * 1000; 25 await tx.run( 26 `INSERT INTO refresh_tokens 27 (id, family_id, session_id, user_id, realm_id, client_id, scopes, 28 created_at, expires_at, consumed_at, replaced_by, revoked_at) 29 VALUES 30 ({id}, {familyId}, {sessionId}, {userId}, {realmId}, {clientId}, {scopes}, 31 {now}, {expiresAt}, NULL, NULL, NULL)`, 32 { 33 id, 34 familyId, 35 sessionId, 36 userId, 37 realmId, 38 clientId, 39 scopes, 40 now, 41 expiresAt, 42 } 43 ); 44 return { id, token, expiresAt }; 45 } 46 47 export async function issueRefreshToken( 48 systemDb, 49 { sessionId, userId, realmId = null, clientId, scopes, now = Date.now() } 50 ) { 51 const familyId = crypto.randomUUID(); 52 const token = nextToken(); 53 return systemDb.transaction((tx) => 54 insertRefreshToken(tx, { 55 token, 56 familyId, 57 sessionId, 58 userId, 59 realmId, 60 clientId, 61 scopes, 62 now, 63 }) 64 ); 65 } 66 67 async function revokeFamily(tx, familyId, now) { 68 await tx.run( 69 `UPDATE refresh_tokens 70 SET revoked_at = {now} 71 WHERE family_id = {familyId} AND revoked_at IS NULL`, 72 { familyId, now } 73 ); 74 } 75 76 export async function rotateRefreshToken( 77 systemDb, 78 token, 79 { validate, now = Date.now() } = {} 80 ) { 81 if (!token) return { status: 'invalid' }; 82 83 return systemDb.transaction(async (tx) => { 84 const row = await tx.get( 85 `SELECT id, family_id, session_id, user_id, realm_id, client_id, scopes, 86 created_at, expires_at, consumed_at, replaced_by, revoked_at 87 FROM refresh_tokens 88 WHERE id = {id}`, 89 { id: hashToken(token) } 90 ); 91 if (!row || row.revoked_at) return { status: 'invalid' }; 92 if (row.consumed_at) { 93 await revokeFamily(tx, row.family_id, now); 94 return { status: 'reuse' }; 95 } 96 if (row.expires_at <= now) return { status: 'expired' }; 97 98 if (validate && !(await validate(row, tx))) { 99 await revokeFamily(tx, row.family_id, now); 100 return { status: 'denied' }; 101 } 102 103 const next = nextToken(); 104 const nextId = hashToken(next); 105 const expiresAt = now + REFRESH_TOKEN_TTL * 1000; 106 await tx.run( 107 `UPDATE refresh_tokens 108 SET consumed_at = {now}, replaced_by = {nextId} 109 WHERE id = {id}`, 110 { now, nextId, id: row.id } 111 ); 112 await insertRefreshToken(tx, { 113 token: next, 114 familyId: row.family_id, 115 sessionId: row.session_id, 116 userId: row.user_id, 117 realmId: row.realm_id, 118 clientId: row.client_id, 119 scopes: row.scopes, 120 now, 121 }); 122 123 return { 124 status: 'ok', 125 token: next, 126 expiresAt, 127 row, 128 }; 129 }); 130 }