mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-05 18:32:33 +01:00
Remove Discord oauth login support
This commit is contained in:
parent
31dee31339
commit
9e1b6a675b
18
package-lock.json
generated
18
package-lock.json
generated
@ -14,8 +14,8 @@
|
||||
"@sentry/integrations": "^7.17.2",
|
||||
"@sentry/node": "^7.17.2",
|
||||
"@sentry/tracing": "^7.17.2",
|
||||
"ajv": "^8.6.2",
|
||||
"ajv-formats": "^2.1.1",
|
||||
"ajv": "8.6.2",
|
||||
"ajv-formats": "2.1.1",
|
||||
"amqplib": "^0.10.3",
|
||||
"bcrypt": "^5.0.1",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
@ -43,7 +43,7 @@
|
||||
"proxy-agent": "^5.0.0",
|
||||
"sqlite3": "^5.1.1",
|
||||
"typeorm": "^0.3.10",
|
||||
"typescript-json-schema": "^0.50.1",
|
||||
"typescript-json-schema": "0.50.1",
|
||||
"ws": "^8.9.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -1911,9 +1911,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/ajv": {
|
||||
"version": "8.11.2",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.11.2.tgz",
|
||||
"integrity": "sha512-E4bfmKAhGiSTvMfL1Myyycaub+cUEU2/IvpylXkUu7CHBkBj1f/ikdzbD7YQ6FKUbixDxeYvB/xY4fvyroDlQg==",
|
||||
"version": "8.6.2",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.6.2.tgz",
|
||||
"integrity": "sha512-9807RlWAgT564wT+DjeyU5OFMPjmzxVobvDFmNAhY+5zD6A2ly3jDp6sgnfyDtlIQ+7H97oc/DGCzzfu9rjw9w==",
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"json-schema-traverse": "^1.0.0",
|
||||
@ -7394,9 +7394,9 @@
|
||||
}
|
||||
},
|
||||
"ajv": {
|
||||
"version": "8.11.2",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.11.2.tgz",
|
||||
"integrity": "sha512-E4bfmKAhGiSTvMfL1Myyycaub+cUEU2/IvpylXkUu7CHBkBj1f/ikdzbD7YQ6FKUbixDxeYvB/xY4fvyroDlQg==",
|
||||
"version": "8.6.2",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.6.2.tgz",
|
||||
"integrity": "sha512-9807RlWAgT564wT+DjeyU5OFMPjmzxVobvDFmNAhY+5zD6A2ly3jDp6sgnfyDtlIQ+7H97oc/DGCzzfu9rjw9w==",
|
||||
"requires": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"json-schema-traverse": "^1.0.0",
|
||||
|
@ -54,8 +54,8 @@
|
||||
"@sentry/integrations": "^7.17.2",
|
||||
"@sentry/node": "^7.17.2",
|
||||
"@sentry/tracing": "^7.17.2",
|
||||
"ajv": "^8.6.2",
|
||||
"ajv-formats": "^2.1.1",
|
||||
"ajv": "8.6.2",
|
||||
"ajv-formats": "2.1.1",
|
||||
"amqplib": "^0.10.3",
|
||||
"bcrypt": "^5.0.1",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
@ -83,7 +83,7 @@
|
||||
"proxy-agent": "^5.0.0",
|
||||
"sqlite3": "^5.1.1",
|
||||
"typeorm": "^0.3.10",
|
||||
"typescript-json-schema": "^0.50.1",
|
||||
"typescript-json-schema": "0.50.1",
|
||||
"ws": "^8.9.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
|
@ -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;
|
@ -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 };
|
@ -8,4 +8,3 @@ export * from "./utility/String";
|
||||
export * from "./handlers/Voice";
|
||||
export * from "./utility/captcha";
|
||||
export * from "./utility/EmbedHandlers";
|
||||
export * from "./handlers/Oauth";
|
Loading…
Reference in New Issue
Block a user