http-consent.test.js (15251B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 4 import { 5 clearConsent, 6 locationQuery, 7 mergeCookies, 8 pkce, 9 setConsentMode, 10 withSystemDatabase, 11 createHttpHarness, 12 } from './helpers/http-harness.js'; 13 14 test('HTTP client consent prompts', async (t) => { 15 const harness = await createHttpHarness(); 16 const { 17 acme, 18 app, 19 origin, 20 publicUrl, 21 env, 22 completeAuthorization, 23 } = harness; 24 25 try { 26 await t.test('remembered consent prompts once and can be denied', async () => { 27 clearConsent(env, { username: 'alice', realm: 'acme', clientId: 'internal-app' }); 28 29 const login = await acme({ 30 method: 'POST', 31 path: '/api/v1/login/password', 32 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 33 }); 34 let jar = mergeCookies('', login); 35 36 const { verifier, challenge } = pkce(); 37 const redirectUri = `${origin('app.acme.localhost')}/callback`; 38 const authorizePath = 39 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 40 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 41 `&state=consent-once&code_challenge=${challenge}&code_challenge_method=S256`; 42 43 const prompt = await acme({ path: authorizePath, cookie: jar }); 44 assert.equal(prompt.status, 302); 45 assert.equal(locationQuery(prompt).pathname, '/oidc/internal-app/consent'); 46 jar = mergeCookies(jar, prompt); 47 48 const request = await acme({ 49 path: '/api/v1/consent?client_id=internal-app', 50 cookie: jar, 51 }); 52 assert.equal(request.status, 200); 53 assert.equal(request.json.client.id, 'internal-app'); 54 assert.equal(request.json.user.username, 'alice'); 55 assert.ok(request.json.scopes.includes('email')); 56 57 const allow = await acme({ 58 method: 'POST', 59 path: '/api/v1/consent', 60 body: { client_id: 'internal-app', decision: 'allow' }, 61 cookie: jar, 62 }); 63 assert.equal(allow.status, 200); 64 assert.equal(new URL(allow.json.redirect).pathname, '/oidc/internal-app/authorize'); 65 66 const approved = await acme({ path: authorizePath.split('?')[0], cookie: jar }); 67 assert.equal(approved.status, 302); 68 assert.ok(locationQuery(approved).searchParams.get('code')); 69 70 const stored = withSystemDatabase(env, (database) => 71 database 72 .prepare( 73 `SELECT scopes, granted_via, revoked_at 74 FROM client_consents 75 WHERE user_id = (SELECT id FROM users WHERE username = 'alice') 76 AND client_id = 'internal-app' 77 AND realm_id = (SELECT id FROM realms WHERE slug = 'acme')` 78 ) 79 .get() 80 ); 81 assert.equal(stored.granted_via, 'user'); 82 assert.equal(stored.revoked_at, null); 83 assert.ok(stored.scopes.includes('email')); 84 85 const repeat = pkce(); 86 const repeatPath = 87 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 88 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 89 `&state=consent-repeat&code_challenge=${repeat.challenge}&code_challenge_method=S256`; 90 const direct = await acme({ path: repeatPath, cookie: jar }); 91 assert.equal(direct.status, 302); 92 assert.ok(locationQuery(direct).searchParams.get('code')); 93 94 clearConsent(env, { username: 'alice', realm: 'acme', clientId: 'internal-app' }); 95 const denied = pkce(); 96 const denyPath = 97 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 98 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 99 `&state=consent-deny&code_challenge=${denied.challenge}&code_challenge_method=S256`; 100 const denyPrompt = await acme({ path: denyPath, cookie: jar }); 101 assert.equal(locationQuery(denyPrompt).pathname, '/oidc/internal-app/consent'); 102 jar = mergeCookies(jar, denyPrompt); 103 104 const deny = await acme({ 105 method: 'POST', 106 path: '/api/v1/consent', 107 body: { client_id: 'internal-app', decision: 'deny' }, 108 cookie: jar, 109 }); 110 assert.equal(deny.status, 200); 111 const deniedRedirect = new URL(deny.json.redirect); 112 assert.equal(deniedRedirect.searchParams.get('error'), 'access_denied'); 113 assert.equal(deniedRedirect.searchParams.get('state'), 'consent-deny'); 114 115 const missing = await acme({ 116 path: '/api/v1/consent?client_id=internal-app', 117 cookie: jar, 118 }); 119 assert.equal(missing.status, 400); 120 }); 121 122 await t.test('consent scope upgrades require a new prompt', async () => { 123 clearConsent(env, { username: 'alice', realm: 'acme', clientId: 'internal-app' }); 124 125 const login = await acme({ 126 method: 'POST', 127 path: '/api/v1/login/password', 128 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 129 }); 130 let jar = mergeCookies('', login); 131 const redirectUri = `${origin('app.acme.localhost')}/callback`; 132 133 const base = pkce(); 134 const basePath = 135 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 136 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20profile` + 137 `&state=scope-base&code_challenge=${base.challenge}&code_challenge_method=S256`; 138 const approved = await completeAuthorization(acme, { 139 path: basePath, 140 cookie: jar, 141 clientId: 'internal-app', 142 }); 143 jar = approved.cookie; 144 assert.ok(locationQuery(approved.response).searchParams.get('code')); 145 146 const upgrade = pkce(); 147 const upgradePath = 148 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 149 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20profile%20email` + 150 `&state=scope-upgrade&code_challenge=${upgrade.challenge}&code_challenge_method=S256`; 151 const prompt = await acme({ path: upgradePath, cookie: jar }); 152 assert.equal(prompt.status, 302); 153 assert.equal(locationQuery(prompt).pathname, '/oidc/internal-app/consent'); 154 }); 155 156 await t.test('trusted clients skip consent for home users but guests still prompt', async () => { 157 setConsentMode(env, { realm: 'acme', clientId: 'acme-members-app', mode: 'trusted' }); 158 clearConsent(env, { username: 'alice', realm: 'acme', clientId: 'acme-members-app' }); 159 try { 160 const login = await acme({ 161 method: 'POST', 162 path: '/api/v1/login/password', 163 body: { 164 client_id: 'acme-members-app', 165 identifier: 'alice@acme.test', 166 password: 'demo', 167 }, 168 }); 169 const jar = mergeCookies('', login); 170 const home = pkce(); 171 const homePath = 172 `/oidc/acme-members-app/authorize?response_type=code&client_id=acme-members-app` + 173 `&redirect_uri=${encodeURIComponent(`${origin('members.localhost')}/callback`)}` + 174 `&scope=openid%20email&state=trusted-home` + 175 `&code_challenge=${home.challenge}&code_challenge_method=S256`; 176 const direct = await acme({ path: homePath, cookie: jar }); 177 assert.equal(direct.status, 302); 178 assert.ok(locationQuery(direct).searchParams.get('code')); 179 180 const stored = withSystemDatabase(env, (database) => 181 database 182 .prepare( 183 `SELECT granted_via 184 FROM client_consents 185 WHERE user_id = (SELECT id FROM users WHERE username = 'alice') 186 AND client_id = 'acme-members-app' 187 AND realm_id = (SELECT id FROM realms WHERE slug = 'acme')` 188 ) 189 .get() 190 ); 191 assert.equal(stored.granted_via, 'trusted'); 192 193 setConsentMode(env, { 194 realm: 'acme', 195 clientId: 'acme-members-app', 196 mode: 'remembered', 197 }); 198 const changed = pkce(); 199 const changedPath = 200 `/oidc/acme-members-app/authorize?response_type=code&client_id=acme-members-app` + 201 `&redirect_uri=${encodeURIComponent(`${origin('members.localhost')}/callback`)}` + 202 `&scope=openid%20email&state=trusted-changed` + 203 `&code_challenge=${changed.challenge}&code_challenge_method=S256`; 204 const prompt = await acme({ path: changedPath, cookie: jar }); 205 assert.equal(prompt.status, 302); 206 assert.equal(locationQuery(prompt).pathname, '/oidc/acme-members-app/consent'); 207 } finally { 208 setConsentMode(env, { 209 realm: 'acme', 210 clientId: 'acme-members-app', 211 mode: 'remembered', 212 }); 213 clearConsent(env, { 214 username: 'alice', 215 realm: 'acme', 216 clientId: 'acme-members-app', 217 }); 218 } 219 220 setConsentMode(env, { realm: 'acme', clientId: 'acme-public-app', mode: 'trusted' }); 221 clearConsent(env, { 222 username: 'contractor', 223 realm: 'acme', 224 clientId: 'acme-public-app', 225 }); 226 try { 227 const guest = pkce(); 228 const guestPath = 229 `/oidc/acme-public-app/authorize?response_type=code&client_id=acme-public-app` + 230 `&redirect_uri=${encodeURIComponent(`${origin('public.localhost')}/callback`)}` + 231 `&scope=openid%20email&state=trusted-guest` + 232 `&code_challenge=${guest.challenge}&code_challenge_method=S256`; 233 const first = await acme({ path: guestPath }); 234 let jar = mergeCookies('', first); 235 const login = await acme({ 236 method: 'POST', 237 path: '/api/v1/login/password', 238 body: { 239 client_id: 'acme-public-app', 240 identifier: 'contractor@personal.test', 241 password: 'demo', 242 }, 243 cookie: jar, 244 }); 245 assert.equal(login.status, 200); 246 assert.equal(login.json.pending_auth_request, true); 247 jar = mergeCookies(jar, login); 248 249 const prompt = await acme({ path: guestPath, cookie: jar }); 250 assert.equal(prompt.status, 302); 251 assert.equal(locationQuery(prompt).pathname, '/oidc/acme-public-app/consent'); 252 } finally { 253 setConsentMode(env, { 254 realm: 'acme', 255 clientId: 'acme-public-app', 256 mode: 'remembered', 257 }); 258 clearConsent(env, { 259 username: 'contractor', 260 realm: 'acme', 261 clientId: 'acme-public-app', 262 }); 263 } 264 }); 265 266 await t.test('client-host account shows and revokes current app consent', async () => { 267 clearConsent(env, { username: 'alice', realm: 'acme', clientId: 'internal-app' }); 268 269 const login = await app({ 270 method: 'POST', 271 path: '/api/v1/login/password', 272 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 273 }); 274 let jar = mergeCookies('', login); 275 const redirectUri = `${origin('app.acme.localhost')}/callback`; 276 const { challenge } = pkce(); 277 const authorizePath = 278 `/oidc/authorize?response_type=code&client_id=internal-app` + 279 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 280 `&state=client-consent&code_challenge=${challenge}&code_challenge_method=S256`; 281 282 const prompt = await app({ path: authorizePath, cookie: jar }); 283 assert.equal(prompt.status, 302); 284 assert.equal(locationQuery(prompt).pathname, '/oidc/consent'); 285 jar = mergeCookies(jar, prompt); 286 287 const allow = await app({ 288 method: 'POST', 289 path: '/api/v1/consent', 290 body: { client_id: 'internal-app', decision: 'allow' }, 291 cookie: jar, 292 }); 293 assert.equal(allow.status, 200); 294 const approved = await app({ path: authorizePath.split('?')[0], cookie: jar }); 295 assert.ok(locationQuery(approved).searchParams.get('code')); 296 297 const current = await app({ 298 path: '/api/v1/account/clients/internal-app/consent', 299 cookie: jar, 300 }); 301 assert.equal(current.status, 200); 302 assert.equal(current.json.active, true); 303 assert.equal(current.json.client.id, 'internal-app'); 304 assert.equal(current.json.account_url, `${publicUrl}/app/account`); 305 306 const wrongClient = await app({ 307 path: '/api/v1/account/clients/acme-members-app/consent', 308 cookie: jar, 309 }); 310 assert.equal(wrongClient.status, 404); 311 312 const revoked = await app({ 313 method: 'DELETE', 314 path: '/api/v1/account/clients/internal-app/consent', 315 cookie: jar, 316 }); 317 assert.equal(revoked.status, 200); 318 assert.equal(revoked.json.ok, true); 319 assert.equal(revoked.json.active, false); 320 321 const session = await app({ path: '/api/v1/session', cookie: jar }); 322 assert.equal(session.status, 200); 323 324 const next = pkce(); 325 const nextPath = 326 `/oidc/authorize?response_type=code&client_id=internal-app` + 327 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 328 `&state=client-revoked&code_challenge=${next.challenge}&code_challenge_method=S256`; 329 const reprompt = await app({ path: nextPath, cookie: jar }); 330 assert.equal(reprompt.status, 302); 331 assert.equal(locationQuery(reprompt).pathname, '/oidc/consent'); 332 }); 333 334 await t.test('revoking consent after authorization blocks the code exchange', async () => { 335 clearConsent(env, { username: 'alice', realm: 'acme', clientId: 'internal-app' }); 336 337 const login = await acme({ 338 method: 'POST', 339 path: '/api/v1/login/password', 340 body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' }, 341 }); 342 const jar = mergeCookies('', login); 343 const { verifier, challenge } = pkce(); 344 const redirectUri = `${origin('app.acme.localhost')}/callback`; 345 const authorizePath = 346 `/oidc/internal-app/authorize?response_type=code&client_id=internal-app` + 347 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 348 `&state=revoked-code&code_challenge=${challenge}&code_challenge_method=S256`; 349 350 const approved = await completeAuthorization(acme, { 351 path: authorizePath, 352 cookie: jar, 353 clientId: 'internal-app', 354 }); 355 const code = locationQuery(approved.response).searchParams.get('code'); 356 assert.ok(code); 357 358 withSystemDatabase(env, (database) => { 359 database 360 .prepare( 361 `UPDATE client_consents 362 SET revoked_at = ? 363 WHERE user_id = (SELECT id FROM users WHERE username = 'alice') 364 AND client_id = 'internal-app' 365 AND realm_id = (SELECT id FROM realms WHERE slug = 'acme')` 366 ) 367 .run(Date.now()); 368 }); 369 370 const tokenResponse = await acme({ 371 method: 'POST', 372 path: '/oidc/internal-app/token', 373 form: { 374 grant_type: 'authorization_code', 375 code, 376 redirect_uri: redirectUri, 377 client_id: 'internal-app', 378 code_verifier: verifier, 379 }, 380 }); 381 assert.equal(tokenResponse.status, 400); 382 assert.equal(tokenResponse.json.error, 'invalid_grant'); 383 }); 384 385 } finally { 386 await harness.close(); 387 } 388 });