auth

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

bans.js (3381B)


      1 export async function getBan(db, { userId, clientId }) {
      2   return db.get(
      3     `SELECT user_id, client_id, created_by, created_at
      4        FROM app_bans
      5       WHERE user_id = {userId} AND client_id = {clientId}`,
      6     { userId, clientId }
      7   );
      8 }
      9 
     10 export async function getActiveBan(db, { userId, clientId }) {
     11   return db.get(
     12     `SELECT user_id, client_id, created_by, created_at
     13        FROM app_bans
     14       WHERE user_id = {userId}
     15         AND (client_id = {clientId} OR client_id = '*')
     16       ORDER BY CASE WHEN client_id = '*' THEN 1 ELSE 0 END
     17       LIMIT 1`,
     18     { userId, clientId }
     19   );
     20 }
     21 
     22 export async function isUserBanned(db, { userId, clientId }) {
     23   return Boolean(await getActiveBan(db, { userId, clientId }));
     24 }
     25 
     26 export async function createBan(db, { userId, clientId, createdBy = null, now = Date.now() }) {
     27   await db.run(
     28     `INSERT INTO app_bans (user_id, client_id, created_by, created_at)
     29      VALUES ({userId}, {clientId}, {createdBy}, {now})
     30      ON CONFLICT(user_id, client_id) DO NOTHING`,
     31     { userId, clientId, createdBy, now }
     32   );
     33   return getBan(db, { userId, clientId });
     34 }
     35 
     36 export async function deleteBan(db, { userId, clientId }) {
     37   const result = await db.run(
     38     `DELETE FROM app_bans
     39       WHERE user_id = {userId} AND client_id = {clientId}`,
     40     { userId, clientId }
     41   );
     42   return result.changes > 0;
     43 }
     44 
     45 export function serializeBan(ban) {
     46   if (!ban) return null;
     47   return {
     48     user_id: ban.user_id,
     49     client_id: ban.client_id,
     50     created_by: ban.created_by,
     51     created_at: ban.created_at,
     52   };
     53 }
     54 
     55 export async function revokeBannedClientAccess(
     56   systemDb,
     57   { userId, realmId, clientId, now = Date.now() }
     58 ) {
     59   return systemDb.transaction(async (tx) => {
     60     const consents = await tx.run(
     61       `UPDATE client_consents
     62           SET revoked_at = {now}, updated_at = {now}
     63         WHERE user_id = {userId} AND realm_id = {realmId}
     64           AND client_id = {clientId} AND revoked_at IS NULL`,
     65       { userId, realmId, clientId, now }
     66     );
     67     const refreshTokens = await tx.run(
     68       `UPDATE refresh_tokens
     69           SET revoked_at = {now}
     70         WHERE user_id = {userId} AND realm_id = {realmId}
     71           AND client_id = {clientId} AND revoked_at IS NULL`,
     72       { userId, realmId, clientId, now }
     73     );
     74     return {
     75       consents: consents.changes,
     76       refresh_tokens: refreshTokens.changes,
     77     };
     78   });
     79 }
     80 
     81 export async function revokeUserRealmAccess(
     82   systemDb,
     83   { userId, realmId, now = Date.now() }
     84 ) {
     85   return systemDb.transaction(async (tx) => {
     86     const consents = await tx.run(
     87       `UPDATE client_consents
     88           SET revoked_at = {now}, updated_at = {now}
     89         WHERE user_id = {userId} AND realm_id = {realmId}
     90           AND client_id NOT IN ('admin', 'account')
     91           AND revoked_at IS NULL`,
     92       { userId, realmId, now }
     93     );
     94     const refreshTokens = await tx.run(
     95       `UPDATE refresh_tokens
     96           SET revoked_at = {now}
     97         WHERE user_id = {userId} AND realm_id = {realmId}
     98           AND client_id NOT IN ('admin', 'account')
     99           AND revoked_at IS NULL`,
    100       { userId, realmId, now }
    101     );
    102     return {
    103       consents: consents.changes,
    104       refresh_tokens: refreshTokens.changes,
    105     };
    106   });
    107 }
    108 
    109 export async function revokeBannedRealmAccess(
    110   systemDb,
    111   { userId, realmId, now = Date.now() }
    112 ) {
    113   return revokeUserRealmAccess(systemDb, { userId, realmId, now });
    114 }