mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 17:49:09 +00:00
port full auth flow to cf workers
This commit is contained in:
parent
9eeb946389
commit
aad0987dce
50 changed files with 551 additions and 1167 deletions
55
src/backend-worker/utils/api-tools.ts
Normal file
55
src/backend-worker/utils/api-tools.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import { UserGuildPermissions } from '../../common/types';
|
||||
import {
|
||||
evaluatePermission,
|
||||
permissions as Permissions,
|
||||
} from '../../common/utils/hasPermission';
|
||||
|
||||
export const formData = (obj: Record<string, any>): string => {
|
||||
return Object.keys(obj)
|
||||
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`)
|
||||
.join('&');
|
||||
};
|
||||
|
||||
export const respond = (obj: Record<string, any>, init?: ResponseInit) =>
|
||||
new Response(JSON.stringify(obj), init);
|
||||
|
||||
export const resolveFailures = (
|
||||
handleWith: Response,
|
||||
handler: (request: Request) => Promise<Response> | Response
|
||||
) => async (request: Request): Promise<Response> | Response => {
|
||||
try {
|
||||
return handler(request);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return handleWith;
|
||||
}
|
||||
};
|
||||
|
||||
export const parsePermissions = (
|
||||
permissions: number,
|
||||
owner: boolean = false
|
||||
): UserGuildPermissions => {
|
||||
if (owner || evaluatePermission(permissions, Permissions.ADMINISTRATOR)) {
|
||||
return UserGuildPermissions.Admin;
|
||||
}
|
||||
|
||||
if (evaluatePermission(permissions, Permissions.MANAGE_ROLES)) {
|
||||
return UserGuildPermissions.Manager;
|
||||
}
|
||||
|
||||
return UserGuildPermissions.User;
|
||||
};
|
||||
|
||||
export const getSessionID = (request: Request): { type: string; id: string } | null => {
|
||||
const sessionID = request.headers.get('authorization');
|
||||
if (!sessionID) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [type, id] = sessionID.split(' ');
|
||||
if (type !== 'Bearer') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { type, id };
|
||||
};
|
11
src/backend-worker/utils/config.ts
Normal file
11
src/backend-worker/utils/config.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
const self = (global as any) as Record<string, string>;
|
||||
|
||||
const safeURI = (x: string) => x.replace(/\/$/, '');
|
||||
const list = (x: string) => x.split(',');
|
||||
|
||||
export const botClientID = self.BOT_CLIENT_ID;
|
||||
export const botClientSecret = self.BOT_CLIENT_SECRET;
|
||||
export const uiPublicURI = safeURI(self.UI_PUBLIC_URI);
|
||||
export const apiPublicURI = safeURI(self.API_PUBLIC_URI);
|
||||
export const rootUsers = list(self.ROOT_USERS);
|
||||
export const kvPrefix = self.KV_PREFIX;
|
26
src/backend-worker/utils/kv.ts
Normal file
26
src/backend-worker/utils/kv.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
class WrappedKVNamespace {
|
||||
constructor(private kvNamespace: KVNamespace) {}
|
||||
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
const data = await this.kvNamespace.get(key, 'text');
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(data) as T;
|
||||
}
|
||||
|
||||
async put<T>(key: string, value: T, ttlSeconds?: number) {
|
||||
await this.kvNamespace.put(key, JSON.stringify(value), {
|
||||
expirationTtl: ttlSeconds,
|
||||
});
|
||||
}
|
||||
|
||||
list = this.kvNamespace.list;
|
||||
getWithMetadata = this.kvNamespace.getWithMetadata;
|
||||
delete = this.kvNamespace.delete;
|
||||
}
|
||||
|
||||
export const Sessions = new WrappedKVNamespace(KV_SESSIONS);
|
||||
export const GuildData = new WrappedKVNamespace(KV_GUILD_DATA);
|
||||
export const Guilds = new WrappedKVNamespace(KV_GUILDS);
|
Loading…
Add table
Add a link
Reference in a new issue