1
0
Fork 0
mirror of https://github.com/xHyroM/waki.git synced 2024-09-20 04:33:18 +02:00
waki/src/utils.ts
2024-05-20 12:45:11 +02:00

30 lines
732 B
TypeScript

export function verify(request: Request, secret: string): boolean {
const authorization = request.headers.get("authorization");
if (!authorization) return false;
const token = authorization?.split(" ")?.[1];
if (!token) return false;
return token === btoa(secret);
}
interface Provider {
name: string;
url: string;
token: string;
}
export function parseProviders(providers: string): Provider[] {
try {
const json = JSON.parse(providers);
if (!Array.isArray(json)) return [];
return json satisfies Provider[];
} catch {
return [];
}
}
export function getMainProvider(providers: Provider[], main: string): Provider | undefined {
return providers.find((provider) => provider.name === main) ?? providers[0];
}