auth

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

create.js (1199B)


      1 import { app } from '../../../app.js';
      2 import { authenticateAdminRequest } from '../../../lib/admin-auth.js';
      3 import { addAdminRealmMember } from '../../../lib/admin-users.js';
      4 import { HttpError } from '../../../lib/http/http-error.js';
      5 
      6 function assertBody(body) {
      7   if (body === null || typeof body !== 'object' || Array.isArray(body)) {
      8     throw new HttpError(400, 'invalid_request', 'Request body must be a JSON object');
      9   }
     10   for (const field of Object.keys(body)) {
     11     if (field !== 'user_id') {
     12       throw new HttpError(400, 'invalid_request', `Unknown field: ${field}`);
     13     }
     14   }
     15 }
     16 
     17 export default {
     18   method: 'post',
     19   path: '/api/v1/admin/realms/:realm/users',
     20   name: 'admin.users.create',
     21   async handler(req) {
     22     const auth = await authenticateAdminRequest(req, {
     23       realmSlug: req.params.realm,
     24     });
     25     const body = req.body || {};
     26     assertBody(body);
     27     const detail = await addAdminRealmMember({
     28       systemDb: auth.systemDb,
     29       realmDb: auth.realmDb,
     30       realm: auth.realm,
     31       userId: body.user_id,
     32       actor: auth.user,
     33       actorRoles: auth.roles,
     34     });
     35 
     36     return new app.HttpOk({ ok: true, ...detail }, { 'cache-control': 'no-store' });
     37   },
     38 };