diff --git a/src/backend-worker/handlers/login-callback.ts b/src/backend-worker/handlers/login-callback.ts index f37c5b6..75abe7d 100644 --- a/src/backend-worker/handlers/login-callback.ts +++ b/src/backend-worker/handlers/login-callback.ts @@ -6,10 +6,12 @@ import { SessionData, } from '../../common/types'; import { + AuthType, discordFetch, formData, parsePermissions, resolveFailures, + userAgent, } from '../utils/api-tools'; import { Bounce } from '../utils/bounce'; import { apiPublicURI, botClientID, botClientSecret, uiPublicURI } from '../utils/config'; @@ -62,6 +64,7 @@ export const LoginCallback = resolveFailures( method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', + 'user-agent': userAgent, }, body: formData(tokenRequest), }); @@ -98,7 +101,11 @@ export const LoginCallback = resolveFailures( ); const getUser = async (accessToken: string): Promise => { - const user = await discordFetch('/users/@me', accessToken, 'Bearer'); + const user = await discordFetch( + '/users/@me', + accessToken, + AuthType.Bearer + ); if (!user) { return null; @@ -122,7 +129,7 @@ const getGuilds = async (accessToken: string) => { const guilds = await discordFetch( '/users/@me/guilds', accessToken, - 'Bearer' + AuthType.Bearer ); if (!guilds) { diff --git a/src/backend-worker/handlers/revoke-session.ts b/src/backend-worker/handlers/revoke-session.ts index dc7a1a0..4e8504b 100644 --- a/src/backend-worker/handlers/revoke-session.ts +++ b/src/backend-worker/handlers/revoke-session.ts @@ -1,5 +1,5 @@ import { SessionData } from 'roleypoly/common/types'; -import { formData, respond, withSession } from '../utils/api-tools'; +import { formData, respond, userAgent, withSession } from '../utils/api-tools'; import { botClientID, botClientSecret } from '../utils/config'; import { Sessions } from '../utils/kv'; @@ -15,6 +15,7 @@ export const RevokeSession = withSession( method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', + 'user-agent': userAgent, }, body: formData(tokenRequest), }); diff --git a/src/backend-worker/utils/api-tools.ts b/src/backend-worker/utils/api-tools.ts index 27951e5..16f50b5 100644 --- a/src/backend-worker/utils/api-tools.ts +++ b/src/backend-worker/utils/api-tools.ts @@ -58,17 +58,22 @@ export const getSessionID = (request: Request): { type: string; id: string } | n return { type, id }; }; -const userAgent = +export const userAgent = 'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)'; +export enum AuthType { + Bearer = 'Bearer', + Bot = 'Bot', +} + export const discordFetch = async ( url: string, auth: string, - authType: 'Bearer' | 'Bot' = 'Bearer' + authType: AuthType = AuthType.Bearer ): Promise => { const response = await fetch('https://discord.com/api/v8' + url, { headers: { - authorization: `${authType} ${auth}`, + authorization: `${AuthType[authType]} ${auth}`, 'user-agent': userAgent, }, }); @@ -116,10 +121,6 @@ const NotAuthenticated = (extra?: string) => { status: 403 } ); -type WithSessionOpts = { - mustAuthenticate?: boolean; -}; - export const withSession = ( wrappedHandler: (session: SessionData) => Handler ): Handler => async (request: Request): Promise => {