admin-client-hosts.js (4261B)
1 import { HttpError } from './http/http-error.js'; 2 import { pagedResult, searchClause } from './pagination.js'; 3 import { 4 addOidcClientHost, 5 findClientRealm, 6 findOidcClientHost, 7 findRealmHost, 8 listOidcClientHosts, 9 normalizeHost, 10 removeOidcClientHost, 11 validateRealmHostname, 12 } from './realm.js'; 13 import { isSystemClient } from './oidc/clients.js'; 14 import { normalizeReservedHosts } from './admin-realm-hosts.js'; 15 import { destroyClientHostSessions } from './session.js'; 16 17 function invalidRequest(message) { 18 return new HttpError(400, 'invalid_request', message); 19 } 20 21 async function assertClientUsable(clientId, realm) { 22 if (isSystemClient(clientId)) { 23 throw new HttpError(400, 'reserved_client_id', 'This client_id is reserved'); 24 } 25 const mapping = await findClientRealm(clientId); 26 if (!mapping || mapping.realm_id !== realm.id) { 27 throw new HttpError(404, 'client_not_found', 'Client is not available in this realm.'); 28 } 29 } 30 31 export async function listAdminClientHosts({ systemDb, realm, clientId }) { 32 await assertClientUsable(clientId, realm); 33 const rows = await listOidcClientHosts(systemDb, clientId); 34 return rows.map((row) => ({ host: row.host, created_at: row.created_at })); 35 } 36 37 export async function listAdminClientHostsPage({ systemDb, realm, clientId, pagination }) { 38 await assertClientUsable(clientId, realm); 39 40 const params = { clientId, limit: pagination.limit, offset: pagination.offset }; 41 let filter = 'client_id = {clientId}'; 42 if (pagination.q) { 43 const search = searchClause(pagination.q, ['host']); 44 filter += ` AND ${search.clause}`; 45 Object.assign(params, search.params); 46 } 47 48 const rows = await systemDb.all( 49 `SELECT host, created_at FROM oidc_client_hosts 50 WHERE ${filter} 51 ORDER BY host ASC 52 LIMIT {limit} OFFSET {offset}`, 53 params 54 ); 55 const count = await systemDb.get( 56 `SELECT COUNT(*) AS total FROM oidc_client_hosts WHERE ${filter}`, 57 params 58 ); 59 60 return pagedResult( 61 'hosts', 62 rows.map((row) => ({ host: row.host, created_at: row.created_at })), 63 { limit: pagination.limit, offset: pagination.offset, total: count.total } 64 ); 65 } 66 67 export async function addAdminClientHost({ 68 systemDb, 69 realm, 70 clientId, 71 host, 72 isSystemAdmin = false, 73 reservedHosts, 74 }) { 75 await assertClientUsable(clientId, realm); 76 const normalized = validateRealmHostname(host); 77 78 if (reservedHosts && reservedHosts.has(normalized)) { 79 throw new HttpError( 80 409, 81 'host_reserved', 82 'This hostname is reserved for the authentication service.' 83 ); 84 } 85 86 const clientHost = await findOidcClientHost(normalized); 87 if (clientHost) { 88 if (clientHost.client_id === clientId) { 89 throw new HttpError( 90 409, 91 'host_in_use', 92 'This hostname is already assigned to this client.' 93 ); 94 } 95 throw new HttpError( 96 409, 97 'host_in_use', 98 'This hostname is already assigned to a client application.' 99 ); 100 } 101 102 const existing = await findRealmHost(normalized); 103 if (existing) { 104 if (existing.id === realm.id) { 105 throw new HttpError( 106 409, 107 'host_in_use', 108 'This hostname is already assigned to this realm.' 109 ); 110 } 111 throw new HttpError( 112 409, 113 'host_in_use', 114 isSystemAdmin 115 ? `This hostname is already assigned to ${existing.slug}.` 116 : 'This hostname is already assigned to another realm.' 117 ); 118 } 119 120 const createdAt = Date.now(); 121 try { 122 await addOidcClientHost(clientId, normalized); 123 } catch { 124 throw new HttpError(409, 'host_in_use', 'This hostname is already assigned.'); 125 } 126 127 return { host: normalized, created_at: createdAt }; 128 } 129 130 export async function deleteAdminClientHost({ systemDb, realm, clientId, host }) { 131 await assertClientUsable(clientId, realm); 132 const normalized = normalizeHost(host); 133 if (!normalized) throw invalidRequest('host is required'); 134 135 const removed = await removeOidcClientHost(systemDb, clientId, normalized); 136 if (!removed) { 137 throw new HttpError(404, 'host_not_found', 'Host is not assigned to this client.'); 138 } 139 140 await destroyClientHostSessions(systemDb, { 141 realmId: realm.id, 142 clientId, 143 host: normalized, 144 }); 145 146 return { removed: true, host: normalized }; 147 }