hooks.utils.ts51 lines · main
| 1 | import { ident, safeSql, type SafeSqlFragment } from '@supabase/pg-meta/src/pg-format' |
| 2 | |
| 3 | import { Hook } from './hooks.constants' |
| 4 | |
| 5 | export const extractMethod = ( |
| 6 | uri: string, |
| 7 | secret?: string |
| 8 | ): |
| 9 | | { type: 'postgres'; schema: string; functionName: string } |
| 10 | | { type: 'https'; url: string; secret: string } => { |
| 11 | if (uri.startsWith('https')) { |
| 12 | return { type: 'https', url: uri, secret: secret || '' } |
| 13 | } else { |
| 14 | const [_proto, _x, _db, schema, functionName] = (uri || '').split('/') |
| 15 | |
| 16 | return { |
| 17 | type: 'postgres', |
| 18 | schema: schema || '', |
| 19 | functionName: functionName || '', |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | export const isValidHook = (h: Hook) => { |
| 25 | return ( |
| 26 | (h.method.type === 'postgres' && |
| 27 | h.method.schema.length > 0 && |
| 28 | h.method.functionName.length > 0) || |
| 29 | (h.method.type === 'https' && h.method.url.startsWith('https') && h.method.secret.length > 0) |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @param schema the schema that the function belongs to |
| 36 | * @param functionName the function name associated with the hook |
| 37 | * @returns an array of SQL statements to restore the original permissions to the function |
| 38 | */ |
| 39 | export const getRevokePermissionStatements = ( |
| 40 | schema: string, |
| 41 | functionName: string |
| 42 | ): Array<SafeSqlFragment> => { |
| 43 | return [ |
| 44 | safeSql`-- Revoke access to function from briven_auth_admin |
| 45 | revoke execute on function ${ident(schema)}.${ident(functionName)} from briven_auth_admin;`, |
| 46 | safeSql`-- Revoke access to schema from briven_auth_admin |
| 47 | revoke usage on schema ${ident(schema)} from briven_auth_admin;`, |
| 48 | safeSql`-- Restore function permissions to authenticated, anon and public |
| 49 | grant execute on function ${ident(schema)}.${ident(functionName)} to authenticated, anon, public;`, |
| 50 | ] |
| 51 | } |