consents.js (4993B)
1 export function splitScopes(value) { 2 return [...new Set(String(value || '').split(/\s+/).filter(Boolean))]; 3 } 4 5 function scopeSet(value) { 6 return new Set(splitScopes(value)); 7 } 8 9 export function scopesCovered(consented, requested) { 10 const granted = scopeSet(consented); 11 return splitScopes(requested).every((scope) => granted.has(scope)); 12 } 13 14 function mergeScopes(left, right) { 15 return splitScopes([...splitScopes(left), ...splitScopes(right)].join(' ')).join(' '); 16 } 17 18 export async function getConsent(db, { userId, realmId, clientId }) { 19 return db.get( 20 `SELECT user_id, realm_id, client_id, scopes, granted_via, created_at, 21 updated_at, revoked_at 22 FROM client_consents 23 WHERE user_id = {userId} AND realm_id = {realmId} AND client_id = {clientId}`, 24 { userId, realmId, clientId } 25 ); 26 } 27 28 export async function recordConsent( 29 db, 30 { userId, realmId, clientId, scopes: requestedScopes, grantedVia = 'user', now = Date.now() } 31 ) { 32 return db.transaction(async (tx) => { 33 const existing = await getConsent(tx, { userId, realmId, clientId }); 34 const scopes = 35 existing && !existing.revoked_at 36 ? mergeScopes(existing.scopes, requestedScopes) 37 : splitScopes(requestedScopes).join(' '); 38 const granted_via = 39 grantedVia === 'user' || existing?.granted_via === 'user' ? 'user' : 'trusted'; 40 41 if (existing) { 42 await tx.run( 43 `UPDATE client_consents 44 SET scopes = {scopes}, granted_via = {grantedVia}, updated_at = {now}, 45 revoked_at = NULL 46 WHERE user_id = {userId} AND realm_id = {realmId} AND client_id = {clientId}`, 47 { scopes, grantedVia: granted_via, now, userId, realmId, clientId } 48 ); 49 return { 50 ...existing, 51 scopes, 52 granted_via, 53 updated_at: now, 54 revoked_at: null, 55 }; 56 } 57 58 await tx.run( 59 `INSERT INTO client_consents 60 (user_id, realm_id, client_id, scopes, granted_via, created_at, 61 updated_at, revoked_at) 62 VALUES 63 ({userId}, {realmId}, {clientId}, {scopes}, {grantedVia}, {now}, {now}, NULL)`, 64 { userId, realmId, clientId, scopes, grantedVia: granted_via, now } 65 ); 66 return { 67 user_id: userId, 68 realm_id: realmId, 69 client_id: clientId, 70 scopes, 71 granted_via, 72 created_at: now, 73 updated_at: now, 74 revoked_at: null, 75 }; 76 }); 77 } 78 79 export async function revokeConsent( 80 db, 81 { userId, realmId, clientId, now = Date.now() } 82 ) { 83 return db.transaction(async (tx) => { 84 const existing = await getConsent(tx, { userId, realmId, clientId }); 85 if (existing) { 86 await tx.run( 87 `UPDATE client_consents 88 SET revoked_at = {now}, updated_at = {now} 89 WHERE user_id = {userId} AND realm_id = {realmId} AND client_id = {clientId}`, 90 { now, userId, realmId, clientId } 91 ); 92 return { ...existing, revoked_at: now, updated_at: now }; 93 } 94 95 await tx.run( 96 `INSERT INTO client_consents 97 (user_id, realm_id, client_id, scopes, granted_via, created_at, 98 updated_at, revoked_at) 99 VALUES 100 ({userId}, {realmId}, {clientId}, '', 'user', {now}, {now}, {now})`, 101 { userId, realmId, clientId, now } 102 ); 103 return { 104 user_id: userId, 105 realm_id: realmId, 106 client_id: clientId, 107 scopes: '', 108 granted_via: 'user', 109 created_at: now, 110 updated_at: now, 111 revoked_at: now, 112 }; 113 }); 114 } 115 116 export async function revokeConsentRefreshTokens( 117 db, 118 { userId, realmId, clientId, now = Date.now() } 119 ) { 120 const result = await db.run( 121 `UPDATE refresh_tokens 122 SET revoked_at = {now} 123 WHERE user_id = {userId} AND realm_id = {realmId} 124 AND client_id = {clientId} AND revoked_at IS NULL`, 125 { userId, realmId, clientId, now } 126 ); 127 return result.changes; 128 } 129 130 export function consentStatus({ client, user, realm, consent, scope }) { 131 if (client?.system_client) { 132 return { required: false, autoGrant: false, reason: 'system', consent: null }; 133 } 134 if (!client || !realm) { 135 return { required: true, autoGrant: false, reason: 'context', consent: null }; 136 } 137 138 const homeRealm = user?.home_realm_id === realm.id; 139 if (client.consent_mode === 'trusted' && homeRealm) { 140 if (consent?.revoked_at) { 141 return { required: true, autoGrant: false, reason: 'revoked', consent }; 142 } 143 return { required: false, autoGrant: true, reason: 'trusted', consent }; 144 } 145 146 if (!consent || consent.revoked_at) { 147 return { 148 required: true, 149 autoGrant: false, 150 reason: consent?.revoked_at ? 'revoked' : 'missing', 151 consent, 152 }; 153 } 154 if (consent.granted_via !== 'user') { 155 return { required: true, autoGrant: false, reason: 'trusted_grant', consent }; 156 } 157 if (!scopesCovered(consent.scopes, scope)) { 158 return { required: true, autoGrant: false, reason: 'scopes', consent }; 159 } 160 161 return { required: false, autoGrant: false, reason: 'granted', consent }; 162 }