http-client-secrets.test.js (6528B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 4 import { 5 locationQuery, 6 mergeCookies, 7 pkce, 8 createHttpHarness, 9 } from './helpers/http-harness.js'; 10 11 test('HTTP confidential client secrets', async (t) => { 12 const harness = await createHttpHarness(); 13 const { 14 acme, 15 origin, 16 completeAuthorization, 17 adminToken, 18 adminRequest, 19 createAdminClient, 20 updateAdminClient, 21 deleteAdminClient, 22 authorizeCodeForClient, 23 } = harness; 24 25 try { 26 await t.test('admin clients rotate confidential client secrets', async () => { 27 const admin = await adminToken('it@acme.test'); 28 const clientId = 'admin-created-confidential'; 29 const redirectUri = `${origin('created-conf.localhost')}/callback`; 30 try { 31 const created = await createAdminClient(admin, 'acme', { 32 client_id: clientId, 33 name: 'Admin Created Confidential', 34 access_mode: 'members', 35 token_endpoint_auth_method: 'client_secret_basic', 36 redirect_uris: [redirectUri], 37 }); 38 assert.equal(created.status, 200); 39 assert.match(created.json.client_secret, /^[A-Za-z0-9_-]{43}$/); 40 assert.equal(created.json.client.has_secret, true); 41 42 const initial = await authorizeCodeForClient(acme, { 43 clientId, 44 identifier: 'alice@acme.test', 45 redirectUri, 46 state: 'conf-initial', 47 }); 48 const initialToken = await acme({ 49 method: 'POST', 50 path: `/oidc/${clientId}/token`, 51 form: { 52 grant_type: 'authorization_code', 53 code: initial.code, 54 redirect_uri: redirectUri, 55 code_verifier: initial.verifier, 56 }, 57 headers: { 58 authorization: `Basic ${Buffer.from(`${clientId}:${created.json.client_secret}`).toString('base64')}`, 59 }, 60 }); 61 assert.equal(initialToken.status, 200); 62 63 const rotated = await adminRequest(admin, { 64 method: 'POST', 65 path: `/api/v1/admin/realms/acme/clients/${clientId}/secret`, 66 }); 67 assert.equal(rotated.status, 200); 68 assert.match(rotated.json.client_secret, /^[A-Za-z0-9_-]{43}$/); 69 assert.notEqual(rotated.json.client_secret, created.json.client_secret); 70 assert.equal(rotated.json.client.token_endpoint_auth_method, 'client_secret_basic'); 71 72 const second = await authorizeCodeForClient(acme, { 73 clientId, 74 identifier: 'alice@acme.test', 75 redirectUri, 76 state: 'conf-old-secret', 77 }); 78 const oldDenied = await acme({ 79 method: 'POST', 80 path: `/oidc/${clientId}/token`, 81 form: { 82 grant_type: 'authorization_code', 83 code: second.code, 84 redirect_uri: redirectUri, 85 code_verifier: second.verifier, 86 }, 87 headers: { 88 authorization: `Basic ${Buffer.from(`${clientId}:${created.json.client_secret}`).toString('base64')}`, 89 }, 90 }); 91 assert.equal(oldDenied.status, 401); 92 assert.equal(oldDenied.json.error, 'invalid_client'); 93 94 const newAllowed = await acme({ 95 method: 'POST', 96 path: `/oidc/${clientId}/token`, 97 form: { 98 grant_type: 'authorization_code', 99 code: second.code, 100 redirect_uri: redirectUri, 101 code_verifier: second.verifier, 102 }, 103 headers: { 104 authorization: `Basic ${Buffer.from(`${clientId}:${rotated.json.client_secret}`).toString('base64')}`, 105 }, 106 }); 107 assert.equal(newAllowed.status, 200); 108 109 const cleared = await updateAdminClient(admin, 'acme', clientId, { 110 token_endpoint_auth_method: 'none', 111 }); 112 assert.equal(cleared.status, 200); 113 assert.equal(cleared.json.client.has_secret, false); 114 assert.equal(cleared.json.client.require_pkce, true); 115 116 const discovery = await acme({ path: `/oidc/${clientId}/openid-configuration` }); 117 assert.deepEqual(discovery.json.token_endpoint_auth_methods_supported, ['none']); 118 119 const publicAccess = await authorizeCodeForClient(acme, { 120 clientId, 121 identifier: 'alice@acme.test', 122 redirectUri, 123 state: 'conf-to-public', 124 }); 125 const publicToken = await acme({ 126 method: 'POST', 127 path: `/oidc/${clientId}/token`, 128 form: { 129 grant_type: 'authorization_code', 130 code: publicAccess.code, 131 redirect_uri: redirectUri, 132 client_id: clientId, 133 code_verifier: publicAccess.verifier, 134 }, 135 }); 136 assert.equal(publicToken.status, 200); 137 } finally { 138 const cleanup = await deleteAdminClient(admin, 'acme', clientId); 139 assert.ok(cleanup.status === 200 || cleanup.status === 404); 140 } 141 }); 142 143 await t.test('confidential clients authenticate with client_secret_basic', async () => { 144 const { verifier, challenge } = pkce(); 145 const redirectUri = `${origin('confidential.localhost')}/callback`; 146 const authorizePath = 147 `/oidc/confidential-app/authorize?response_type=code&client_id=confidential-app` + 148 `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` + 149 `&state=state-4&code_challenge=${challenge}&code_challenge_method=S256`; 150 151 const first = await acme({ path: authorizePath }); 152 let jar = mergeCookies('', first); 153 const login = await acme({ 154 method: 'POST', 155 path: '/api/v1/login/password', 156 body: { client_id: 'confidential-app', identifier: 'alice@acme.test', password: 'demo' }, 157 cookie: jar, 158 }); 159 jar = mergeCookies(jar, login); 160 const second = await completeAuthorization(acme, { 161 path: authorizePath, 162 cookie: jar, 163 clientId: 'confidential-app', 164 }); 165 const code = locationQuery(second.response).searchParams.get('code'); 166 167 const tokenResponse = await acme({ 168 method: 'POST', 169 path: '/oidc/confidential-app/token', 170 form: { 171 grant_type: 'authorization_code', 172 code, 173 redirect_uri: redirectUri, 174 code_verifier: verifier, 175 }, 176 headers: { 177 authorization: `Basic ${Buffer.from('confidential-app:demo-secret').toString('base64')}`, 178 }, 179 }); 180 assert.equal(tokenResponse.status, 200); 181 }); 182 183 } finally { 184 await harness.close(); 185 } 186 });