consent-request.js (1965B)
1 import { resolveClientForHost, resolveHostContext } from './host-context.js'; 2 import { HttpError } from './http/http-error.js'; 3 import { getClient, getSystemClient } from './oidc/clients.js'; 4 import { getAuthRequest, readAuthRequestId } from './oidc/requests.js'; 5 import { getSystemDatabase, openRealmDatabase } from './realm.js'; 6 import { authenticateRequest } from './session.js'; 7 8 export async function resolveConsentRequest({ req, res, publicUrl, clientId }) { 9 const hostContext = await resolveHostContext(req, { publicUrl }); 10 const target = await resolveClientForHost( 11 hostContext, 12 clientId || hostContext.clientId 13 ); 14 const realmDb = target.realm ? await openRealmDatabase(target.realm) : null; 15 const systemDb = getSystemDatabase(); 16 const client = target.systemClient 17 ? getSystemClient(target.clientId) 18 : await getClient(realmDb, target.clientId); 19 20 if (!client || !client.enabled) { 21 throw new HttpError(404, 'unknown_client', 'Unknown client'); 22 } 23 if (client.system_client) { 24 throw new HttpError(400, 'invalid_request', 'System clients do not use consent'); 25 } 26 27 const auth = await authenticateRequest({ 28 systemDb, 29 realmDb, 30 req, 31 res, 32 context: hostContext, 33 }); 34 if (!auth) { 35 throw new HttpError(401, 'not_authenticated', 'Not signed in'); 36 } 37 38 const store = realmDb; 39 const pending = await getAuthRequest(store, readAuthRequestId(req, target)); 40 if (!pending || pending.completed_at || pending.client_id !== client.client_id) { 41 throw new HttpError( 42 400, 43 'invalid_request', 44 'There is no pending authorization request' 45 ); 46 } 47 if (pending.user_id !== auth.user.id || pending.session_id !== auth.session.id) { 48 throw new HttpError( 49 409, 50 'consent_not_ready', 51 'The authorization request is not ready for consent' 52 ); 53 } 54 55 return { 56 hostContext, 57 target, 58 realmDb, 59 systemDb, 60 client, 61 pending, 62 store, 63 auth, 64 }; 65 }