summary refs log tree commit diff
path: root/src/api/util/handlers/Oauth.ts
blob: cc6621615c6de3e305d0ac6d5a6d651a9827a21b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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 };