auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

resolve.js (1526B)


      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, getSystemClient } 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, systemClient } = await resolveClientForHost(
     25       hostContext,
     26       body.client_id
     27     );
     28     const realmDb = realm ? await openRealmDatabase(realm) : null;
     29     const client = systemClient
     30       ? getSystemClient(clientId)
     31       : await getClient(realmDb, clientId);
     32     if (!client || !client.enabled) {
     33       throw new HttpError(404, 'unknown_client', 'Unknown client');
     34     }
     35 
     36     const metadata = realm ? await realmMetadata(realm) : null;
     37     const result = await resolveLogin({ identifier: body.identifier });
     38 
     39     return new app.HttpOk({
     40       realm: metadata,
     41       ...result,
     42     });
     43   },
     44 };