From a85c4d5dddfe4adf0c78e2c507e6ed08cf2d2f43 Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Mon, 14 Dec 2020 14:43:22 -0500 Subject: [PATCH] add withSession, cacheLayer, and userAgent to discordFetch --- src/backend-worker/handlers/get-session.ts | 40 ++++++------------ src/backend-worker/index.ts | 5 +-- src/backend-worker/utils/api-tools.ts | 47 +++++++++++++++++++++- 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/backend-worker/handlers/get-session.ts b/src/backend-worker/handlers/get-session.ts index 233ee48..2b2a8a7 100644 --- a/src/backend-worker/handlers/get-session.ts +++ b/src/backend-worker/handlers/get-session.ts @@ -1,31 +1,17 @@ import { SessionData } from 'roleypoly/common/types'; -import { getSessionID, respond } from '../utils/api-tools'; -import { Sessions } from '../utils/kv'; +import { respond, withSession } from '../utils/api-tools'; -const NotAuthenticated = (extra?: string) => - respond( - { - err: extra || 'not authenticated', - }, - { status: 403 } - ); +export const GetSession = withSession( + (session?: SessionData) => (): Response => { + const { user, guilds, sessionID } = session || {}; -export const GetSession = async (request: Request): Promise => { - const sessionID = getSessionID(request); - if (!sessionID) { - return NotAuthenticated('missing auth header'); + return respond({ + user, + guilds, + sessionID, + }); + }, + { + mustAuthenticate: true, } - - console.log(sessionID); - - const sessionData = await Sessions.get(sessionID.id); - if (!sessionData) { - return NotAuthenticated('session not found'); - } - - const { tokens, ...withoutTokens } = sessionData; - - return respond({ - ...withoutTokens, - }); -}; +); diff --git a/src/backend-worker/index.ts b/src/backend-worker/index.ts index 6b8b1c8..c55d9de 100644 --- a/src/backend-worker/index.ts +++ b/src/backend-worker/index.ts @@ -15,7 +15,7 @@ router.add('GET', 'bot-join', BotJoin); router.add('GET', 'login-bounce', LoginBounce); router.add('GET', 'login-callback', LoginCallback); router.add('GET', 'get-session', GetSession); -<<<<<<< HEAD +router.add('GET', 'get-slug', GetSlug); router.add('GET', 'x-headers', (request) => { const headers: { [x: string]: string } = {}; @@ -25,9 +25,6 @@ router.add('GET', 'x-headers', (request) => { return new Response(JSON.stringify(headers)); }); -======= -router.add('GET', 'get-slug', GetSlug); ->>>>>>> init to fetch guild slug addEventListener('fetch', (event: FetchEvent) => { event.respondWith(router.handle(event.request)); diff --git a/src/backend-worker/utils/api-tools.ts b/src/backend-worker/utils/api-tools.ts index ee4fb86..b746745 100644 --- a/src/backend-worker/utils/api-tools.ts +++ b/src/backend-worker/utils/api-tools.ts @@ -1,9 +1,10 @@ -import { UserGuildPermissions } from '../../common/types'; +import { SessionData, UserGuildPermissions } from '../../common/types'; import { evaluatePermission, permissions as Permissions, } from '../../common/utils/hasPermission'; -import { WrappedKVNamespace } from './kv'; +import { Handler } from '../router'; +import { Sessions, WrappedKVNamespace } from './kv'; export const formData = (obj: Record): string => { return Object.keys(obj) @@ -63,6 +64,8 @@ export const discordFetch = async ( const response = await fetch('https://discord.com/api/v8' + url, { headers: { authorization: `${authType} ${auth}`, + 'user-agent': + 'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)', }, }); @@ -92,3 +95,43 @@ export const cacheLayer = ( return fallbackValue; }; + +const NotAuthenticated = (extra?: string) => + respond( + { + err: extra || 'not authenticated', + }, + { status: 403 } + ); + +type WithSessionOpts = { + mustAuthenticate?: boolean; +}; + +export const withSession = ( + wrappedHandler: (session?: SessionData) => Handler, + { mustAuthenticate }: WithSessionOpts = {} +): Handler => async (request: Request): Promise => { + const sessionID = getSessionID(request); + if (!sessionID) { + if (mustAuthenticate) { + return NotAuthenticated('missing authentication'); + } else { + return await wrappedHandler(undefined)(request); + } + } + + const session = await Sessions.get(sessionID.id); + if (!session) { + if (mustAuthenticate) { + return NotAuthenticated('authentication expired or not found'); + } else { + return await wrappedHandler(undefined)(request); + } + } + + return await wrappedHandler(session)(request); +}; + +export const mustBeAuthenticated = (handler: Handler) => + withSession(() => handler, { mustAuthenticate: true });