auth

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

select.js (2503B)


      1 import { app } from '../../app.js';
      2 import { resolveClientForHost, resolveHostContext } from '../../lib/host-context.js';
      3 import { HttpError } from '../../lib/http/http-error.js';
      4 import {
      5   getAuthRequest,
      6   readAuthRequestId,
      7   selectAuthRequestAccount,
      8 } from '../../lib/oidc/requests.js';
      9 import { getSystemDatabase, openRealmDatabase } from '../../lib/realm.js';
     10 import { requestToken, selectSessionAccount } from '../../lib/session.js';
     11 
     12 export default {
     13   method: 'post',
     14   path: '/api/v1/session/select',
     15   name: 'session.select',
     16   async handler(req) {
     17     const body = req.body || {};
     18     if (typeof body.public_id !== 'string') {
     19       throw new HttpError(400, 'invalid_request', 'public_id is required');
     20     }
     21     if (body.client_id != null && typeof body.client_id !== 'string') {
     22       throw new HttpError(400, 'invalid_request', 'client_id must be a string');
     23     }
     24 
     25     const hostContext = await resolveHostContext(req, {
     26       publicUrl: app.config.server.public_url,
     27     });
     28     const systemDb = getSystemDatabase();
     29     let target = {
     30       realm: hostContext.realm || null,
     31       clientId: hostContext.clientId || null,
     32       systemClient: false,
     33     };
     34     if (body.client_id) {
     35       target = await resolveClientForHost(hostContext, body.client_id);
     36     }
     37 
     38     const selected = await selectSessionAccount(systemDb, {
     39       token: requestToken(req),
     40       context: hostContext,
     41       publicId: body.public_id,
     42     });
     43     if (!selected) {
     44       throw new HttpError(401, 'not_authenticated', 'Not signed in');
     45     }
     46 
     47     let pendingAuthRequest = false;
     48     const store = target.systemClient
     49       ? systemDb
     50       : target.realm
     51         ? await openRealmDatabase(target.realm)
     52         : null;
     53     if (store) {
     54       const pending = await getAuthRequest(store, readAuthRequestId(req, target));
     55       if (
     56         pending &&
     57         !pending.completed_at &&
     58         (!target.clientId || pending.client_id === target.clientId)
     59       ) {
     60         await selectAuthRequestAccount(store, pending.id, {
     61           userId: selected.user.id,
     62           sessionId: selected.session.id,
     63           authTime: selected.session.auth_time,
     64         });
     65         pendingAuthRequest = true;
     66       }
     67     }
     68 
     69     return new app.HttpOk({
     70       ok: true,
     71       user: {
     72         id: selected.user.id,
     73         username: selected.user.username,
     74         display_name: selected.user.display_name,
     75       },
     76       expires_at: selected.session.expires_at,
     77       pending_auth_request: pendingAuthRequest,
     78     });
     79   },
     80 };