http-session-reauth.test.js (7169B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 4 import { 5 activeSessionId, 6 locationQuery, 7 mergeCookies, 8 pkce, 9 withSystemDatabase, 10 createHttpHarness, 11 } from './helpers/http-harness.js'; 12 13 test('HTTP re-verification after a revoked realm binding', async (t) => { 14 const harness = await createHttpHarness(); 15 const { 16 acme, 17 origin, 18 env, 19 completeAuthorization, 20 } = harness; 21 22 try { 23 await t.test('a revoked realm binding requires password re-verification', async () => { 24 const login = await acme({ 25 method: 'POST', 26 path: '/api/v1/login/password', 27 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 28 }); 29 assert.equal(login.status, 200); 30 let jar = mergeCookies('', login); 31 32 const { verifier, challenge } = pkce(); 33 const redirectUri = `${origin('app.acme.localhost')}/callback`; 34 const authorizePath = 35 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 36 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 37 `&state=state-revoked&code_challenge=${challenge}&code_challenge_method=S256`; 38 39 const completed = await completeAuthorization(acme, { 40 path: authorizePath, 41 cookie: jar, 42 clientId: 'internal-app', 43 }); 44 assert.equal(completed.response.status, 302); 45 assert.ok(locationQuery(completed.response).searchParams.get('code')); 46 47 const sessionId = activeSessionId(env, jar); 48 const revokedAt = Date.now(); 49 withSystemDatabase(env, (database) => { 50 database 51 .prepare( 52 `UPDATE session_realms 53 SET revoked_at = ?, revoked_by = ? 54 WHERE session_id = ? 55 AND realm_id = (SELECT id FROM realms WHERE slug = 'acme')` 56 ) 57 .run(revokedAt, 'test-admin', sessionId); 58 }); 59 60 const reauth = await acme({ path: authorizePath, cookie: jar }); 61 assert.equal(reauth.status, 302); 62 const reauthLocation = locationQuery(reauth); 63 assert.equal(reauthLocation.pathname, '/oidc/internal-app/login'); 64 assert.equal(reauthLocation.searchParams.get('reauth'), '1'); 65 jar = mergeCookies(jar, reauth); 66 67 const relogin = await acme({ 68 method: 'POST', 69 path: '/api/v1/login/password', 70 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 71 cookie: jar, 72 }); 73 assert.equal(relogin.status, 200); 74 assert.equal(relogin.json.pending_auth_request, true); 75 jar = mergeCookies(jar, relogin); 76 77 const second = await completeAuthorization(acme, { 78 path: authorizePath, 79 cookie: jar, 80 clientId: 'internal-app', 81 }); 82 assert.equal(second.response.status, 302); 83 const code = locationQuery(second.response).searchParams.get('code'); 84 assert.ok(code); 85 86 const binding = withSystemDatabase(env, (database) => 87 database 88 .prepare( 89 `SELECT revoked_at 90 FROM session_realms 91 WHERE session_id = ? 92 AND realm_id = (SELECT id FROM realms WHERE slug = 'acme')` 93 ) 94 .get(sessionId) 95 ); 96 assert.equal(binding.revoked_at, null); 97 98 const tokenResponse = await acme({ 99 method: 'POST', 100 path: '/oidc/internal-app/token', 101 form: { 102 grant_type: 'authorization_code', 103 code, 104 redirect_uri: redirectUri, 105 client_id: 'internal-app', 106 code_verifier: verifier, 107 }, 108 }); 109 assert.equal(tokenResponse.status, 200); 110 }); 111 112 await t.test('the account chooser cannot bypass a revoked realm binding', async () => { 113 const aliceLogin = await acme({ 114 method: 'POST', 115 path: '/api/v1/login/password', 116 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 117 }); 118 let jar = mergeCookies('', aliceLogin); 119 const itLogin = await acme({ 120 method: 'POST', 121 path: '/api/v1/login/password', 122 body: { client_id: 'internal-app', identifier: 'it@acme.test', password: 'demo' }, 123 cookie: jar, 124 }); 125 jar = mergeCookies(jar, itLogin); 126 127 const { challenge } = pkce(); 128 const redirectUri = `${origin('app.acme.localhost')}/callback`; 129 const authorizePath = 130 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 131 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 132 `&state=state-revoked-chooser&code_challenge=${challenge}&code_challenge_method=S256`; 133 134 const chooser = await acme({ path: authorizePath, cookie: jar }); 135 assert.equal(chooser.status, 302); 136 assert.equal(locationQuery(chooser).pathname, '/oidc/internal-app/login'); 137 jar = mergeCookies(jar, chooser); 138 139 const session = await acme({ path: '/api/v1/session', cookie: jar }); 140 const itAccount = session.json.accounts.find( 141 (account) => account.user.username === 'acme-it' 142 ); 143 const selected = await acme({ 144 method: 'POST', 145 path: '/api/v1/session/select', 146 body: { public_id: itAccount.public_id, client_id: 'internal-app' }, 147 cookie: jar, 148 }); 149 assert.equal(selected.status, 200); 150 assert.equal(selected.json.pending_auth_request, true); 151 152 const authorized = await completeAuthorization(acme, { 153 path: authorizePath, 154 cookie: jar, 155 clientId: 'internal-app', 156 }); 157 assert.equal(authorized.response.status, 302); 158 assert.ok(locationQuery(authorized.response).searchParams.get('code')); 159 160 const sessionId = activeSessionId(env, jar); 161 withSystemDatabase(env, (database) => { 162 database 163 .prepare( 164 `UPDATE session_realms 165 SET revoked_at = ?, revoked_by = ? 166 WHERE session_id = ? 167 AND realm_id = (SELECT id FROM realms WHERE slug = 'acme')` 168 ) 169 .run(Date.now(), 'test-admin', sessionId); 170 }); 171 172 const nextChooser = await acme({ path: authorizePath, cookie: jar }); 173 assert.equal(nextChooser.status, 302); 174 const chooserLocation = locationQuery(nextChooser); 175 assert.equal(chooserLocation.pathname, '/oidc/internal-app/login'); 176 assert.equal(chooserLocation.searchParams.get('reauth'), null); 177 jar = mergeCookies(jar, nextChooser); 178 179 const reselected = await acme({ 180 method: 'POST', 181 path: '/api/v1/session/select', 182 body: { public_id: itAccount.public_id, client_id: 'internal-app' }, 183 cookie: jar, 184 }); 185 assert.equal(reselected.status, 200); 186 assert.equal(reselected.json.pending_auth_request, true); 187 188 const blocked = await acme({ path: authorizePath, cookie: jar }); 189 assert.equal(blocked.status, 302); 190 const blockedLocation = locationQuery(blocked); 191 assert.equal(blockedLocation.pathname, '/oidc/internal-app/login'); 192 assert.equal(blockedLocation.searchParams.get('reauth'), '1'); 193 }); 194 195 } finally { 196 await harness.close(); 197 } 198 });