diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-17 19:41:49 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-17 19:41:49 +1100 |
commit | bf420aac3284d8d83e7658cddfb347e7243076ce (patch) | |
tree | 7223b67b888e7aa60ca695f24588d5107bd88d05 /src | |
parent | Use staging's GLOBAL_ENV (diff) | |
download | server-bf420aac3284d8d83e7658cddfb347e7243076ce.tar.xz |
Remove Discord oauth login support
Diffstat (limited to 'src')
-rw-r--r-- | src/api/routes/oauth2/callback.ts | 38 | ||||
-rw-r--r-- | src/api/util/handlers/Oauth.ts | 83 | ||||
-rw-r--r-- | src/api/util/index.ts | 3 |
3 files changed, 1 insertions, 123 deletions
diff --git a/src/api/routes/oauth2/callback.ts b/src/api/routes/oauth2/callback.ts deleted file mode 100644 index ddfcdac2..00000000 --- a/src/api/routes/oauth2/callback.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Router, Request, Response } from "express"; -import { route, OauthCallbackHandlers } from "@fosscord/api"; -import { FieldErrors, generateToken, User } from "@fosscord/util"; -const router = Router(); - -router.get("/:type", route({}), async (req: Request, res: Response) => { - const { type } = req.params; - const handler = OauthCallbackHandlers[type]; - if (!handler) throw FieldErrors({ - type: { - code: "BASE_TYPE_CHOICES", - message: `Value must be one of (${Object.keys(OauthCallbackHandlers).join(", ")}).`, - } - }); - - const { code } = req.query; - if (!code || typeof code !== "string") throw FieldErrors({ code: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED"), } }); - const access = await handler.getAccessToken(code); - - const oauthUser = await handler.getUserDetals(access.access_token); - - let user = await User.findOne({ where: { email: oauthUser.email } }); - if (!user) { - user = await User.register({ - email: oauthUser.email, - username: oauthUser.username, - req - }); - - // TODO: upload pfp, banner? - } - - const token = await generateToken(user.id); - - return res.json({ token }) -}); - -export default router; \ No newline at end of file diff --git a/src/api/util/handlers/Oauth.ts b/src/api/util/handlers/Oauth.ts deleted file mode 100644 index cc662161..00000000 --- a/src/api/util/handlers/Oauth.ts +++ /dev/null @@ -1,83 +0,0 @@ -// TODO: Puyo's connections PR would replace this file - -import { Config } from "@fosscord/util"; -import fetch from "node-fetch"; - -export interface OauthAccessToken { - access_token: string; - token_type: string; - expires_in: string; - refresh_token: string; - scope: string; -}; - -export interface OauthUserDetails { - id: string; - email: string; - username: string; - avatar_url: string | null; -} - -interface Connection { - getAccessToken: (code: string) => Promise<OauthAccessToken>; - getUserDetals: (token: string) => Promise<OauthUserDetails>; -} - -const DiscordConnection: Connection = { - getAccessToken: async (code) => { - const { external } = Config.get(); - const { discord } = external; - - if (!discord.id || !discord.secret || !discord.redirect) - throw new Error("Discord Oauth has not been configured.") - - const body = new URLSearchParams( - Object.entries({ - client_id: discord.id as string, - client_secret: discord.secret as string, - redirect_uri: discord.redirect as string, - code: code as string, - grant_type: "authorization_code", - }) - ).toString(); - - const resp = await fetch("https://discord.com/api/oauth2/token", { - method: "POST", - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - body: body, - }); - if (resp.status !== 200) throw new Error(`Failed to get access token.`,); - - const json = await resp.json(); - - return json; - }, - - getUserDetals: async (token) => { - const resp = await fetch("https://discord.com/api/users/@me", { - headers: { - Authorization: `Bearer ${token}` - }, - }); - - const json = await resp.json(); - if (!json.username || !json.email) throw new Error("Failed to get user details via oauth"); - - return { - id: json.id, - email: json.email, - username: json.username, - avatar_url: json.avatar - ? `https://cdn.discordapp.com/avatars/${json.id}/${json.avatar}?size=2048` - : null, - }; - } -}; - -const OauthCallbackHandlers: { [key: string]: Connection; } = { - discord: DiscordConnection -}; - -export { OauthCallbackHandlers }; \ No newline at end of file diff --git a/src/api/util/index.ts b/src/api/util/index.ts index 49542ceb..ffad0607 100644 --- a/src/api/util/index.ts +++ b/src/api/util/index.ts @@ -7,5 +7,4 @@ export * from "./handlers/route"; export * from "./utility/String"; export * from "./handlers/Voice"; export * from "./utility/captcha"; -export * from "./utility/EmbedHandlers"; -export * from "./handlers/Oauth"; \ No newline at end of file +export * from "./utility/EmbedHandlers"; \ No newline at end of file |