@@ -13,37 +13,113 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1313 };
1414 Object.defineProperty(exports, "__esModule", { value: true });
1515 const koa_router_1 = __importDefault(require("koa-router"));
16+const axios_1 = __importDefault(require("axios"));
17+const querystring_1 = require("querystring");
1618 const router = new koa_router_1.default({ prefix: "/discord" });
17-// TODO: Consider using something `disco-oauth2`
18-router.get("/join", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
19+router.get("/authorize", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
20+ var _a;
21+ if (!ctx.session) {
22+ ctx.status = 403;
23+ return yield next();
24+ }
1925 // Front end will send the user here, and then from here into the authorization flow:
20- //https://discord.com/api/oauth2/authorize?client_id=1064167640148476004&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fdiscord%2Fcallback&response_type=code&scope=guilds.join%20guilds.members.read
21- // TODO: Add `state` url param for extra security
26+ // https://discord.com/api/oauth2/authorize?
27+ // client_id=1064167640148476004&
28+ // redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fdiscord%2Fcallback&
29+ // response_type=code&
30+ // scope=guilds.join%20guilds.members.read
2231 const authorize_url = "https://discord.com/api/oauth2/authorize" +
2332 `?client_id=${process.env.DISCORD_CLIENT_ID}` +
2433 `&redirect_uri=${encodeURI(process.env.DISCORD_REDIRECT_URL || "")}` +
2534 `&response_type=code` +
26- `&scope=guilds.join guilds.members.read`;
35+ `&scope=identify guilds guilds.join guilds.members.read` +
36+ `&state=${(_a = ctx.session) === null || _a === void 0 ? void 0 : _a.user.id}`;
2737 ctx.redirect(authorize_url);
2838 yield next();
2939 }));
3040 router.get("/callback", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
31- // OAuth callback
32- // TODO: use the token to get the user ID and join the server.
33- // https://discordapp.com/api/guilds/${SERVER_ID}/members/${userID}
34- var _a;
35- const code = (_a = ctx.request.query) === null || _a === void 0 ? void 0 : _a.code;
41+ const code = ctx.request.query.code;
42+ const state = Number(ctx.request.query.state);
3643 if (code) {
3744 // Success, we should exchange the code for a token:
3845 // https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-access-token-exchange-example
46+ const response = yield axios_1.default.post("https://discord.com/api/v10/oauth2/token", (0, querystring_1.stringify)({
47+ client_id: process.env.DISCORD_CLIENT_ID,
48+ client_secret: process.env.DISCORD_CLIENT_SECRET,
49+ grant_type: "authorization_code",
50+ code: code,
51+ redirect_uri: process.env.DISCORD_REDIRECT_URL,
52+ }), {
53+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
54+ });
55+ const discordToken = yield ctx.prisma.discordToken.create({
56+ data: {
57+ accessToken: response.data.access_token,
58+ expiresIn: response.data.expires_in,
59+ refreshToken: response.data.refresh_token,
60+ scope: response.data.scope,
61+ tokenType: response.data.token_type,
62+ },
63+ });
64+ yield ctx.prisma.user.update({
65+ where: { id: state },
66+ data: { discordTokenId: discordToken.id },
67+ });
68+ ctx.status = 201;
69+ }
70+ ctx.redirect(process.env.CLIENT_URL || "");
71+ yield next();
72+}));
73+router.post("/join", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
74+ if (!ctx.session) {
75+ ctx.status = 403;
76+ return yield next();
3977 }
40- else {
41- // error?
78+ // Use the token acquired to join the server
79+ const accessToken = ctx.session.user.discordToken.accessToken;
80+ const discordUserResponse = yield axios_1.default.get("https://discord.com/api/v10/users/@me", {
81+ headers: {
82+ Authorization: `Bearer ${ctx.session.user.discordToken.accessToken}`,
83+ "Content-Type": "application/x-www-form-urlencoded",
84+ },
85+ });
86+ const discordGuildAddResponse = yield axios_1.default.put(`https://discord.com/api/v10/guilds/${process.env.QUEST_GUILD_ID}/members/${discordUserResponse === null || discordUserResponse === void 0 ? void 0 : discordUserResponse.data.id}`, {
87+ access_token: accessToken,
88+ }, {
89+ headers: {
90+ Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
91+ "Content-Type": "application/json",
92+ },
93+ });
94+ if ((discordGuildAddResponse === null || discordGuildAddResponse === void 0 ? void 0 : discordGuildAddResponse.status) === 201) {
95+ // user added successfully
96+ console.log("added");
97+ }
98+ else if ((discordGuildAddResponse === null || discordGuildAddResponse === void 0 ? void 0 : discordGuildAddResponse.status) === 204) {
99+ // user already in guild
100+ console.log("existing");
42101 }
102+ ctx.status = 200;
43103 yield next();
44104 }));
45105 router.get("/verify", (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
106+ if (!ctx.session) {
107+ ctx.status = 403;
108+ return yield next();
109+ }
46110 // use the discord api to see the list of members and check if user is in the list
47111 // Needs to use the token generated in the callback route
112+ const guildId = process.env.QUEST_GUILD_ID;
113+ // Use the token acquired to get the user and it's joined guilds
114+ const authorizationHeader = {
115+ Authorization: `Bearer ${ctx.session.user.discordToken.accessToken}`,
116+ "Content-Type": "application/x-www-form-urlencoded",
117+ };
118+ const discordUserResponse = yield axios_1.default.get(`https://discord.com/api/v10/users/@me/guilds/${guildId}/member`, {
119+ headers: authorizationHeader,
120+ });
121+ ctx.body = discordUserResponse.data;
122+ ctx.status = 200;
123+ yield next();
48124 }));
49125 exports.default = router;