client-consent.js (3575B)
1 import { app } from '../../app.js'; 2 import { 3 getConsent, 4 revokeConsent, 5 revokeConsentRefreshTokens, 6 splitScopes, 7 } from '../../lib/consents.js'; 8 import { resolveHostContext } from '../../lib/host-context.js'; 9 import { HttpError } from '../../lib/http/http-error.js'; 10 import { getClient } from '../../lib/oidc/clients.js'; 11 import { canonicalBaseUrl } from '../../lib/oidc/context.js'; 12 import { getSystemDatabase, openRealmDatabase, realmMetadata } from '../../lib/realm.js'; 13 import { authenticateRequest } from '../../lib/session.js'; 14 15 async function resolveClientAccount(req, res, clientId) { 16 const hostContext = await resolveHostContext(req, { 17 publicUrl: app.config.server.public_url, 18 }); 19 if (hostContext.mode !== 'client' || hostContext.clientId !== clientId) { 20 throw new HttpError(404, 'not_found', 'Not found'); 21 } 22 23 const realmDb = await openRealmDatabase(hostContext.realm); 24 const systemDb = getSystemDatabase(); 25 const client = await getClient(realmDb, hostContext.clientId); 26 if (!client || !client.enabled) { 27 throw new HttpError(404, 'unknown_client', 'Unknown client'); 28 } 29 30 const auth = await authenticateRequest({ 31 systemDb, 32 realmDb, 33 req, 34 res, 35 context: hostContext, 36 }); 37 if (!auth) { 38 throw new HttpError(401, 'not_authenticated', 'Not signed in'); 39 } 40 41 return { hostContext, realmDb, systemDb, client, auth }; 42 } 43 44 async function consentResponse({ hostContext, systemDb, client, auth }) { 45 const consent = await getConsent(systemDb, { 46 userId: auth.user.id, 47 realmId: hostContext.realm.id, 48 clientId: client.client_id, 49 }); 50 const realm = await realmMetadata(hostContext.realm); 51 52 return { 53 client: { 54 id: client.client_id, 55 name: client.name, 56 }, 57 realm, 58 user: { 59 id: auth.user.id, 60 username: auth.user.username, 61 display_name: auth.user.display_name, 62 }, 63 scopes: splitScopes(consent?.scopes), 64 account_url: `${canonicalBaseUrl(app.config.server.public_url)}/app/account`, 65 active: Boolean(consent && !consent.revoked_at), 66 consent: consent 67 ? { 68 granted_via: consent.granted_via, 69 created_at: consent.created_at, 70 updated_at: consent.updated_at, 71 revoked_at: consent.revoked_at, 72 } 73 : null, 74 }; 75 } 76 77 export default [ 78 { 79 method: 'get', 80 path: '/api/v1/account/clients/:clientId/consent', 81 name: 'account.client_consent.show', 82 async handler(req, res) { 83 const context = await resolveClientAccount(req, res, req.params.clientId); 84 return new app.HttpOk(await consentResponse(context), { 85 'cache-control': 'no-store', 86 }); 87 }, 88 }, 89 { 90 method: 'delete', 91 path: '/api/v1/account/clients/:clientId/consent', 92 name: 'account.client_consent.revoke', 93 async handler(req, res) { 94 const context = await resolveClientAccount(req, res, req.params.clientId); 95 const now = Date.now(); 96 const consent = await revokeConsent(context.systemDb, { 97 userId: context.auth.user.id, 98 realmId: context.hostContext.realm.id, 99 clientId: context.client.client_id, 100 now, 101 }); 102 await revokeConsentRefreshTokens(context.systemDb, { 103 userId: context.auth.user.id, 104 realmId: context.hostContext.realm.id, 105 clientId: context.client.client_id, 106 now, 107 }); 108 109 return new app.HttpOk( 110 { 111 ...(await consentResponse(context)), 112 ok: true, 113 revoked_at: consent.revoked_at, 114 }, 115 { 'cache-control': 'no-store' } 116 ); 117 }, 118 }, 119 ];