auth

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

clients.js (3415B)


      1 import { app } from '../../app.js';
      2 import { authenticateAdminRequest } from '../../lib/admin-auth.js';
      3 import {
      4   createAdminRealmClient,
      5   deleteAdminRealmClient,
      6   listAdminRealmClientsPage,
      7   normalizeCreateInput,
      8   rotateAdminClientSecret,
      9   updateAdminRealmClient,
     10 } from '../../lib/admin-clients.js';
     11 import { parsePagination } from '../../lib/pagination.js';
     12 
     13 export default [
     14   {
     15     method: 'get',
     16     path: '/api/v1/admin/realms/:realm/clients',
     17     name: 'admin.clients.list',
     18     async handler(req) {
     19       const auth = await authenticateAdminRequest(req, {
     20         realmSlug: req.params.realm,
     21       });
     22       const pagination = parsePagination(
     23         Object.fromEntries(new URL(req.url, 'http://localhost').searchParams)
     24       );
     25 
     26       const result = await listAdminRealmClientsPage(
     27         auth.systemDb,
     28         auth.realmDb,
     29         auth.realm.id,
     30         { pagination }
     31       );
     32 
     33       return new app.HttpOk(result, { 'cache-control': 'no-store' });
     34     },
     35   },
     36   {
     37     method: 'post',
     38     path: '/api/v1/admin/realms/:realm/clients',
     39     name: 'admin.clients.create',
     40     async handler(req) {
     41       const auth = await authenticateAdminRequest(req, {
     42         realmSlug: req.params.realm,
     43       });
     44       const input = normalizeCreateInput(req.body || {});
     45       const { client, client_secret: clientSecret } = await createAdminRealmClient({
     46         systemDb: auth.systemDb,
     47         realmDb: auth.realmDb,
     48         realmId: auth.realm.id,
     49         input,
     50       });
     51 
     52       const response = { ok: true, client };
     53       if (clientSecret) response.client_secret = clientSecret;
     54       return new app.HttpOk(response, { 'cache-control': 'no-store' });
     55     },
     56   },
     57   {
     58     method: 'patch',
     59     path: '/api/v1/admin/realms/:realm/clients/:clientId',
     60     name: 'admin.clients.update',
     61     async handler(req) {
     62       const auth = await authenticateAdminRequest(req, {
     63         realmSlug: req.params.realm,
     64       });
     65       const client = await updateAdminRealmClient({
     66         realmDb: auth.realmDb,
     67         clientId: req.params.clientId,
     68         body: req.body || {},
     69       });
     70 
     71       return new app.HttpOk({ ok: true, client }, { 'cache-control': 'no-store' });
     72     },
     73   },
     74   {
     75     method: 'delete',
     76     path: '/api/v1/admin/realms/:realm/clients/:clientId',
     77     name: 'admin.clients.delete',
     78     async handler(req) {
     79       const auth = await authenticateAdminRequest(req, {
     80         realmSlug: req.params.realm,
     81       });
     82       const result = await deleteAdminRealmClient({
     83         systemDb: auth.systemDb,
     84         realmDb: auth.realmDb,
     85         realmId: auth.realm.id,
     86         clientId: req.params.clientId,
     87       });
     88 
     89       return new app.HttpOk(
     90         { ok: true, removed: result.removed, client_id: req.params.clientId },
     91         { 'cache-control': 'no-store' }
     92       );
     93     },
     94   },
     95   {
     96     method: 'post',
     97     path: '/api/v1/admin/realms/:realm/clients/:clientId/secret',
     98     name: 'admin.clients.rotate_secret',
     99     async handler(req) {
    100       const auth = await authenticateAdminRequest(req, {
    101         realmSlug: req.params.realm,
    102       });
    103       const { client, client_secret: clientSecret } = await rotateAdminClientSecret({
    104         realmDb: auth.realmDb,
    105         clientId: req.params.clientId,
    106       });
    107 
    108       return new app.HttpOk(
    109         { ok: true, client_id: client.client_id, client: client, client_secret: clientSecret },
    110         { 'cache-control': 'no-store' }
    111       );
    112     },
    113   },
    114 ];