Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207/* eslint-disable @typescript-eslint/no-explicit-any */import {getGatekeeperUrl} from '#/lib/blacksky-pds'
class GatekeeperError extends Error { constructor( public status: number, public errorType: string, message: string, ) { super(message) this.name = 'GatekeeperError' }}
async function gatekeeperPost( serviceUrl: string, path: string, body: Record<string, unknown>,): Promise<any> { const base = getGatekeeperUrl(serviceUrl) const res = await fetch(`${base}${path}`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(body), })
if (!res.ok) { let errorType = 'UnknownError' let message = `Request failed with status ${res.status}` try { const json = (await res.json()) as {error?: string; message?: string} errorType = json.error || errorType message = json.message || message } catch {} throw new GatekeeperError(res.status, errorType, message) }
const text = await res.text() if (!text) return {} try { return JSON.parse(text) } catch { return {} }}
export async function gateUpdateEmail(params: { serviceUrl: string did: string password: string email: string token?: string emailAuthFactor?: boolean}): Promise<{status: 'success' | 'tokenRequired'}> { const body: Record<string, unknown> = { did: params.did, password: params.password, email: params.email, } if (params.token) { body.token = params.token } if (params.emailAuthFactor !== undefined) { body.emailAuthFactor = params.emailAuthFactor }
try { await gatekeeperPost(params.serviceUrl, '/gate/update-email', body) return {status: 'success'} } catch (e) { if (e instanceof GatekeeperError && e.errorType === 'TokenRequired') { return {status: 'tokenRequired'} } throw e }}
export async function gateRequestAccountDelete(params: { serviceUrl: string did: string password: string authFactorToken?: string}): Promise<{status: 'success' | 'authFactorTokenRequired'}> { const body: Record<string, unknown> = { did: params.did, password: params.password, } if (params.authFactorToken) { body.authFactorToken = params.authFactorToken }
try { await gatekeeperPost( params.serviceUrl, '/gate/request-account-delete', body, ) return {status: 'success'} } catch (e) { if ( e instanceof GatekeeperError && e.errorType === 'AuthFactorTokenRequired' ) { return {status: 'authFactorTokenRequired'} } throw e }}
export async function gateDeactivateAccount(params: { serviceUrl: string did: string password: string}): Promise<void> { await gatekeeperPost(params.serviceUrl, '/gate/deactivate-account', { did: params.did, password: params.password, })}
export async function gateListAppPasswords(params: { serviceUrl: string did: string password: string}): Promise<{ passwords: {name: string; createdAt: string; privileged?: boolean}[]}> { return await gatekeeperPost(params.serviceUrl, '/gate/list-app-passwords', { did: params.did, password: params.password, })}
export async function gateCreateAppPassword(params: { serviceUrl: string did: string password: string name: string privileged?: boolean}): Promise<{ name: string password: string createdAt: string privileged?: boolean}> { return await gatekeeperPost(params.serviceUrl, '/gate/create-app-password', { did: params.did, password: params.password, name: params.name, privileged: params.privileged, })}
export async function gateRevokeAppPassword(params: { serviceUrl: string did: string password: string name: string}): Promise<void> { await gatekeeperPost(params.serviceUrl, '/gate/revoke-app-password', { did: params.did, password: params.password, name: params.name, })}
export async function gateGetPreferences(params: { serviceUrl: string did: string password: string}): Promise<{preferences: any[]}> { return await gatekeeperPost(params.serviceUrl, '/gate/get-preferences', { did: params.did, password: params.password, })}
export async function gatePutPreferences(params: { serviceUrl: string did: string password: string preferences: unknown authFactorToken?: string}): Promise<{status: 'success' | 'authFactorTokenRequired'}> { const body: Record<string, unknown> = { did: params.did, password: params.password, preferences: params.preferences, } if (params.authFactorToken) { body.authFactorToken = params.authFactorToken }
try { await gatekeeperPost(params.serviceUrl, '/gate/put-preferences', body) return {status: 'success'} } catch (e) { if ( e instanceof GatekeeperError && e.errorType === 'AuthFactorTokenRequired' ) { return {status: 'authFactorTokenRequired'} } throw e }}