session-realm-bindings.test.js (7363B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 import fs from 'node:fs/promises'; 4 import { closeRealms, createRealm } from '../src/lib/realm.js'; 5 import { 6 SHORT_SESSION_TTL, 7 bindSessionRealm, 8 createHostSession, 9 getSessionRealmBinding, 10 reauthenticateSession, 11 revokeRealmSessions, 12 revokeSessionRealm, 13 } from '../src/lib/session.js'; 14 import { canonicalContext, setup } from './helpers/session-harness.js'; 15 16 test('realm bindings are inserted once and updated on repeat', async () => { 17 const { dir, realm, systemDb, userId } = await setup(); 18 try { 19 const { session } = await createHostSession(systemDb, { 20 context: canonicalContext(), 21 userId, 22 ttl: SHORT_SESSION_TTL, 23 }); 24 25 await bindSessionRealm(systemDb, session.id, realm.id, 1000); 26 await bindSessionRealm(systemDb, session.id, realm.id, 2000); 27 28 const rows = await systemDb.all( 29 `SELECT session_id, realm_id, last_seen_at 30 FROM session_realms 31 WHERE session_id = {sessionId}`, 32 { sessionId: session.id } 33 ); 34 assert.equal(rows.length, 1); 35 assert.equal(rows[0].last_seen_at, 2000); 36 } finally { 37 await closeRealms(); 38 await fs.rm(dir, { recursive: true, force: true }); 39 } 40 }); 41 42 test('revoking one session realm leaves other realm bindings intact', async () => { 43 const { dir, realm, systemDb, userId } = await setup(); 44 try { 45 const other = await createRealm({ slug: 'globex', displayName: 'Globex' }); 46 const { session } = await createHostSession(systemDb, { 47 context: canonicalContext(), 48 userId, 49 ttl: SHORT_SESSION_TTL, 50 }); 51 52 await bindSessionRealm(systemDb, session.id, realm.id, 1000); 53 await bindSessionRealm(systemDb, session.id, other.id, 1000); 54 55 const revoked = await revokeSessionRealm(systemDb, { 56 sessionId: session.id, 57 realmId: realm.id, 58 revokedBy: 'admin', 59 now: 2000, 60 }); 61 assert.equal(revoked.revoked_at, 2000); 62 assert.equal(revoked.revoked_by, 'admin'); 63 64 const acme = await getSessionRealmBinding(systemDb, session.id, realm.id); 65 assert.equal(acme.revoked_at, 2000); 66 const globex = await getSessionRealmBinding(systemDb, session.id, other.id); 67 assert.equal(globex.revoked_at, null); 68 69 const again = await revokeSessionRealm(systemDb, { 70 sessionId: session.id, 71 realmId: realm.id, 72 revokedBy: 'second-admin', 73 now: 3000, 74 }); 75 assert.equal(again.revoked_at, 2000); 76 assert.equal(again.revoked_by, 'admin'); 77 } finally { 78 await closeRealms(); 79 await fs.rm(dir, { recursive: true, force: true }); 80 } 81 }); 82 83 test('revoking an unbound session realm creates a revoked binding', async () => { 84 const { dir, realm, systemDb, userId } = await setup(); 85 try { 86 const { session } = await createHostSession(systemDb, { 87 context: canonicalContext(), 88 userId, 89 ttl: SHORT_SESSION_TTL, 90 }); 91 92 await revokeSessionRealm(systemDb, { 93 sessionId: session.id, 94 realmId: realm.id, 95 revokedBy: 'admin', 96 now: 4000, 97 }); 98 99 const binding = await getSessionRealmBinding(systemDb, session.id, realm.id); 100 assert.equal(binding.created_at, 4000); 101 assert.equal(binding.last_seen_at, 4000); 102 assert.equal(binding.revoked_at, 4000); 103 assert.equal(binding.revoked_by, 'admin'); 104 } finally { 105 await closeRealms(); 106 await fs.rm(dir, { recursive: true, force: true }); 107 } 108 }); 109 110 test('revoking a realm marks every unrevoked binding for that realm', async () => { 111 const { dir, realm, systemDb, userId } = await setup(); 112 try { 113 const other = await createRealm({ slug: 'globex', displayName: 'Globex' }); 114 const first = await createHostSession(systemDb, { 115 context: canonicalContext('identity.test'), 116 userId, 117 ttl: SHORT_SESSION_TTL, 118 }); 119 const second = await createHostSession(systemDb, { 120 context: canonicalContext('other-identity.test'), 121 userId, 122 ttl: SHORT_SESSION_TTL, 123 }); 124 125 await bindSessionRealm(systemDb, first.session.id, realm.id, 1000); 126 await bindSessionRealm(systemDb, first.session.id, other.id, 1000); 127 await bindSessionRealm(systemDb, second.session.id, realm.id, 1000); 128 129 const changed = await revokeRealmSessions(systemDb, { 130 realmId: realm.id, 131 revokedBy: 'admin', 132 now: 5000, 133 }); 134 assert.equal(changed, 2); 135 136 const firstAcme = await getSessionRealmBinding(systemDb, first.session.id, realm.id); 137 const secondAcme = await getSessionRealmBinding(systemDb, second.session.id, realm.id); 138 const firstGlobex = await getSessionRealmBinding(systemDb, first.session.id, other.id); 139 assert.equal(firstAcme.revoked_at, 5000); 140 assert.equal(secondAcme.revoked_at, 5000); 141 assert.equal(firstGlobex.revoked_at, null); 142 } finally { 143 await closeRealms(); 144 await fs.rm(dir, { recursive: true, force: true }); 145 } 146 }); 147 148 test('reauthentication clears only the target realm and refreshes auth_time', async () => { 149 const { dir, realm, systemDb, userId } = await setup(); 150 try { 151 const other = await createRealm({ slug: 'globex', displayName: 'Globex' }); 152 const { session } = await createHostSession(systemDb, { 153 context: canonicalContext(), 154 userId, 155 ttl: SHORT_SESSION_TTL, 156 }); 157 158 await bindSessionRealm(systemDb, session.id, realm.id, 1000); 159 await bindSessionRealm(systemDb, session.id, other.id, 1000); 160 await revokeSessionRealm(systemDb, { 161 sessionId: session.id, 162 realmId: realm.id, 163 revokedBy: 'admin', 164 now: 2000, 165 }); 166 await revokeSessionRealm(systemDb, { 167 sessionId: session.id, 168 realmId: other.id, 169 revokedBy: 'admin', 170 now: 2000, 171 }); 172 173 const authTime = await reauthenticateSession(systemDb, { 174 sessionId: session.id, 175 realmId: realm.id, 176 now: 9000, 177 }); 178 assert.equal(authTime, 9); 179 180 const acme = await getSessionRealmBinding(systemDb, session.id, realm.id); 181 const globex = await getSessionRealmBinding(systemDb, session.id, other.id); 182 assert.equal(acme.revoked_at, null); 183 assert.equal(acme.revoked_by, null); 184 assert.equal(acme.last_seen_at, 9000); 185 assert.equal(globex.revoked_at, 2000); 186 187 const updated = await systemDb.get( 188 'SELECT auth_time, last_seen_at FROM sessions WHERE id = {id}', 189 { id: session.id } 190 ); 191 assert.equal(updated.auth_time, 9); 192 assert.equal(updated.last_seen_at, 9000); 193 } finally { 194 await closeRealms(); 195 await fs.rm(dir, { recursive: true, force: true }); 196 } 197 }); 198 199 test('binding a revoked realm does not clear the revocation', async () => { 200 const { dir, realm, systemDb, userId } = await setup(); 201 try { 202 const { session } = await createHostSession(systemDb, { 203 context: canonicalContext(), 204 userId, 205 ttl: SHORT_SESSION_TTL, 206 }); 207 208 await bindSessionRealm(systemDb, session.id, realm.id, 1000); 209 await revokeSessionRealm(systemDb, { 210 sessionId: session.id, 211 realmId: realm.id, 212 revokedBy: 'admin', 213 now: 2000, 214 }); 215 await bindSessionRealm(systemDb, session.id, realm.id, 3000); 216 217 const binding = await getSessionRealmBinding(systemDb, session.id, realm.id); 218 assert.equal(binding.revoked_at, 2000); 219 assert.equal(binding.last_seen_at, 1000); 220 } finally { 221 await closeRealms(); 222 await fs.rm(dir, { recursive: true, force: true }); 223 } 224 });