revoke.js (1860B)
1 import { app } from '../../../app.js'; 2 import { authenticateAdminRequest } from '../../../lib/admin-auth.js'; 3 import { assertAdminRealmUser } from '../../../lib/admin-users.js'; 4 import { 5 revokeConsent, 6 revokeConsentRefreshTokens, 7 splitScopes, 8 } from '../../../lib/consents.js'; 9 import { HttpError } from '../../../lib/http/http-error.js'; 10 import { getClient } from '../../../lib/oidc/clients.js'; 11 12 function serializeConsent(consent) { 13 return { 14 user_id: consent.user_id, 15 realm_id: consent.realm_id, 16 client_id: consent.client_id, 17 scopes: splitScopes(consent.scopes), 18 granted_via: consent.granted_via, 19 created_at: consent.created_at, 20 updated_at: consent.updated_at, 21 revoked_at: consent.revoked_at, 22 }; 23 } 24 25 export default { 26 method: 'delete', 27 path: '/api/v1/admin/realms/:realm/users/:userId/consents/:clientId', 28 name: 'admin.consents.revoke', 29 async handler(req) { 30 const auth = await authenticateAdminRequest(req, { 31 realmSlug: req.params.realm, 32 }); 33 const user = await assertAdminRealmUser({ 34 systemDb: auth.systemDb, 35 realmDb: auth.realmDb, 36 realm: auth.realm, 37 userId: req.params.userId, 38 }); 39 40 const client = await getClient(auth.realmDb, req.params.clientId); 41 if (!client || client.system_client) { 42 throw new HttpError(404, 'client_not_found', 'Client not found'); 43 } 44 45 const now = Date.now(); 46 const consent = await revokeConsent(auth.systemDb, { 47 userId: user.id, 48 realmId: auth.realm.id, 49 clientId: client.client_id, 50 now, 51 }); 52 await revokeConsentRefreshTokens(auth.systemDb, { 53 userId: user.id, 54 realmId: auth.realm.id, 55 clientId: client.client_id, 56 now, 57 }); 58 59 return new app.HttpOk( 60 { ok: true, consent: serializeConsent(consent) }, 61 { 'cache-control': 'no-store' } 62 ); 63 }, 64 };