resolve.js (1392B)
1 import { app } from '../../app.js'; 2 import { resolveClientForHost, resolveHostContext } from '../../lib/host-context.js'; 3 import { resolveLogin } from '../../lib/login.js'; 4 import { getClient } from '../../lib/oidc/clients.js'; 5 import { HttpError } from '../../lib/http/http-error.js'; 6 import { openRealmDatabase, realmMetadata } from '../../lib/realm.js'; 7 8 export default { 9 method: 'post', 10 path: '/api/v1/login/resolve', 11 name: 'login.resolve', 12 async handler(req) { 13 const body = req.body || {}; 14 if (typeof body.identifier !== 'string') { 15 throw new HttpError(400, 'invalid_request', 'identifier is required'); 16 } 17 if (typeof body.client_id !== 'string') { 18 throw new HttpError(400, 'invalid_request', 'client_id is required'); 19 } 20 21 const hostContext = await resolveHostContext(req, { 22 publicUrl: app.config.server.public_url, 23 }); 24 const { realm, clientId } = await resolveClientForHost(hostContext, body.client_id); 25 const realmDb = await openRealmDatabase(realm); 26 const client = await getClient(realmDb, clientId); 27 if (!client || !client.enabled) { 28 throw new HttpError(404, 'unknown_client', 'Unknown client'); 29 } 30 31 const metadata = await realmMetadata(realm); 32 const result = await resolveLogin({ identifier: body.identifier }); 33 34 return new app.HttpOk({ 35 realm: metadata, 36 ...result, 37 }); 38 }, 39 };