mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 03:49:11 +00:00
fix(api): define auth token type as enum, export userAgent and use when discordFetch isn't
This commit is contained in:
parent
e1120fd88a
commit
041fe49b05
3 changed files with 19 additions and 10 deletions
|
@ -6,10 +6,12 @@ import {
|
||||||
SessionData,
|
SessionData,
|
||||||
} from '../../common/types';
|
} from '../../common/types';
|
||||||
import {
|
import {
|
||||||
|
AuthType,
|
||||||
discordFetch,
|
discordFetch,
|
||||||
formData,
|
formData,
|
||||||
parsePermissions,
|
parsePermissions,
|
||||||
resolveFailures,
|
resolveFailures,
|
||||||
|
userAgent,
|
||||||
} from '../utils/api-tools';
|
} from '../utils/api-tools';
|
||||||
import { Bounce } from '../utils/bounce';
|
import { Bounce } from '../utils/bounce';
|
||||||
import { apiPublicURI, botClientID, botClientSecret, uiPublicURI } from '../utils/config';
|
import { apiPublicURI, botClientID, botClientSecret, uiPublicURI } from '../utils/config';
|
||||||
|
@ -62,6 +64,7 @@ export const LoginCallback = resolveFailures(
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'application/x-www-form-urlencoded',
|
'content-type': 'application/x-www-form-urlencoded',
|
||||||
|
'user-agent': userAgent,
|
||||||
},
|
},
|
||||||
body: formData(tokenRequest),
|
body: formData(tokenRequest),
|
||||||
});
|
});
|
||||||
|
@ -98,7 +101,11 @@ export const LoginCallback = resolveFailures(
|
||||||
);
|
);
|
||||||
|
|
||||||
const getUser = async (accessToken: string): Promise<DiscordUser | null> => {
|
const getUser = async (accessToken: string): Promise<DiscordUser | null> => {
|
||||||
const user = await discordFetch<DiscordUser>('/users/@me', accessToken, 'Bearer');
|
const user = await discordFetch<DiscordUser>(
|
||||||
|
'/users/@me',
|
||||||
|
accessToken,
|
||||||
|
AuthType.Bearer
|
||||||
|
);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -122,7 +129,7 @@ const getGuilds = async (accessToken: string) => {
|
||||||
const guilds = await discordFetch<UserGuildsPayload>(
|
const guilds = await discordFetch<UserGuildsPayload>(
|
||||||
'/users/@me/guilds',
|
'/users/@me/guilds',
|
||||||
accessToken,
|
accessToken,
|
||||||
'Bearer'
|
AuthType.Bearer
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!guilds) {
|
if (!guilds) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { SessionData } from 'roleypoly/common/types';
|
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 { botClientID, botClientSecret } from '../utils/config';
|
||||||
import { Sessions } from '../utils/kv';
|
import { Sessions } from '../utils/kv';
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ export const RevokeSession = withSession(
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'application/x-www-form-urlencoded',
|
'content-type': 'application/x-www-form-urlencoded',
|
||||||
|
'user-agent': userAgent,
|
||||||
},
|
},
|
||||||
body: formData(tokenRequest),
|
body: formData(tokenRequest),
|
||||||
});
|
});
|
||||||
|
|
|
@ -58,17 +58,22 @@ export const getSessionID = (request: Request): { type: string; id: string } | n
|
||||||
return { type, id };
|
return { type, id };
|
||||||
};
|
};
|
||||||
|
|
||||||
const userAgent =
|
export const userAgent =
|
||||||
'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)';
|
'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)';
|
||||||
|
|
||||||
|
export enum AuthType {
|
||||||
|
Bearer = 'Bearer',
|
||||||
|
Bot = 'Bot',
|
||||||
|
}
|
||||||
|
|
||||||
export const discordFetch = async <T>(
|
export const discordFetch = async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
auth: string,
|
auth: string,
|
||||||
authType: 'Bearer' | 'Bot' = 'Bearer'
|
authType: AuthType = AuthType.Bearer
|
||||||
): Promise<T | null> => {
|
): Promise<T | null> => {
|
||||||
const response = await fetch('https://discord.com/api/v8' + url, {
|
const response = await fetch('https://discord.com/api/v8' + url, {
|
||||||
headers: {
|
headers: {
|
||||||
authorization: `${authType} ${auth}`,
|
authorization: `${AuthType[authType]} ${auth}`,
|
||||||
'user-agent': userAgent,
|
'user-agent': userAgent,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -116,10 +121,6 @@ const NotAuthenticated = (extra?: string) =>
|
||||||
{ status: 403 }
|
{ status: 403 }
|
||||||
);
|
);
|
||||||
|
|
||||||
type WithSessionOpts = {
|
|
||||||
mustAuthenticate?: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const withSession = (
|
export const withSession = (
|
||||||
wrappedHandler: (session: SessionData) => Handler
|
wrappedHandler: (session: SessionData) => Handler
|
||||||
): Handler => async (request: Request): Promise<Response> => {
|
): Handler => async (request: Request): Promise<Response> => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue