|
| 1 | +import type { GrantIdentifier, OAuthClient } from '@jmondi/oauth2-server' |
| 2 | +import { supabaseAdmin } from 'app/utils/supabase/admin' |
| 3 | +import debug from 'debug' |
| 4 | +import type { OAuthScope } from 'src/oauth2/types' |
| 5 | + |
| 6 | +const logger = debug.log |
| 7 | + |
| 8 | +export const getClient = async (clientId: string): Promise<OAuthClient> => { |
| 9 | + try { |
| 10 | + const { data, error } = await supabaseAdmin |
| 11 | + .from('oauth2_clients') |
| 12 | + .select('*') |
| 13 | + .eq('client_id', clientId) |
| 14 | + .single() |
| 15 | + if (error) { |
| 16 | + logger(error) |
| 17 | + throw error |
| 18 | + } |
| 19 | + return { |
| 20 | + id: data.client_id, |
| 21 | + name: data.client_name, |
| 22 | + redirectUris: [data.redirect_uri], |
| 23 | + allowedGrants: await getClientGrants(clientId), |
| 24 | + scopes: await getClientScopes(clientId), |
| 25 | + } |
| 26 | + } catch (error) { |
| 27 | + logger(`Error retrieving client from client id: [${clientId}]. ${error})`) |
| 28 | + throw error |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export const clientExists = async (clientId: string): Promise<boolean> => { |
| 33 | + const { data, error } = await supabaseAdmin |
| 34 | + .from('oauth2_clients') |
| 35 | + .select('*') |
| 36 | + .eq('client_id', clientId) |
| 37 | + .single() |
| 38 | + if (error) { |
| 39 | + logger(`Unable to determine whether client exists. clientId: [${clientId}]. ${error}`) |
| 40 | + return false |
| 41 | + } |
| 42 | + return !!data |
| 43 | +} |
| 44 | + |
| 45 | +export const clientHasGrantType = async ( |
| 46 | + clientId: string, |
| 47 | + grantType: GrantIdentifier |
| 48 | +): Promise<boolean> => { |
| 49 | + const clientGrants: GrantIdentifier[] = await getClientGrants(clientId) |
| 50 | + return clientGrants.includes(grantType) |
| 51 | +} |
| 52 | + |
| 53 | +export const getClientGrants = async (clientId: string): Promise<GrantIdentifier[]> => { |
| 54 | + const { data, error } = await supabaseAdmin |
| 55 | + .from('oauth2_client_authorization_grant_types') |
| 56 | + .select('grant_type') |
| 57 | + .eq('client_id', clientId) |
| 58 | + if (error) { |
| 59 | + logger(error) |
| 60 | + throw error |
| 61 | + } |
| 62 | + return data.map((grant) => grant.grant_type) as GrantIdentifier[] |
| 63 | +} |
| 64 | + |
| 65 | +export const getClientScopes = async (clientId: string): Promise<OAuthScope[]> => { |
| 66 | + const { data, error } = await supabaseAdmin |
| 67 | + .from('oauth2_client_scopes') |
| 68 | + .select('name') |
| 69 | + .eq('client_id', clientId) |
| 70 | + if (error) { |
| 71 | + logger(error) |
| 72 | + return [] |
| 73 | + } |
| 74 | + return data |
| 75 | +} |
0 commit comments