forked from Opensource-IIITH/Discord-CAS
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
170 lines (143 loc) · 4.5 KB
/
app.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const express = require("express");
const config = require("./utils/config");
const logger = require("./utils/logger");
const app = express();
app.use(
express.json({
verify: (req, res, buf, encoding) => {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || "utf8");
}
},
}),
);
app.get(`${config.SUBPATH}/test`, (req, res) => {
res.send("Hello World!");
});
app.post(`${config.SUBPATH}/webhooks/update`, async (req, res) => {
res.send("This endpoint has been removed.");
});
app.get(`${config.SUBPATH}/`, (req, res) => {
res.redirect(`${config.SUBPATH}/discord`);
});
app.get(`${config.SUBPATH}/discord`, (req, res) => {
// this endpoint has been removed, redirect to /cas endpoint which spits the
// error message
res.redirect(`${config.SUBPATH}/cas`);
});
app.get(`${config.SUBPATH}/discord/invite`, (req, res) => {
let redirect_uri = `${config.BASE_URL}/bot`;
res.redirect(
`https://discord.com/oauth2/authorize?client_id=${config.DISCORD_CLIENT_ID}&permissions=275347671040&redirect_uri=${redirect_uri}&response_type=code&scope=bot`,
);
});
async function makeQuery(code, redirect_uri) {
const creds = btoa(`${config.DISCORD_CLIENT_ID}:${config.DISCORD_SECRET}`);
const data = {
grant_type: "authorization_code",
code: code,
redirect_uri,
};
const _encode = (obj) => {
let string = "";
for (const [key, value] of Object.entries(obj)) {
if (!value) continue;
string += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}
return string.substring(1);
};
const params = _encode(data);
const response = await fetch("https://discordapp.com/api/oauth2/token", {
method: "POST",
headers: {
Authorization: `Basic ${creds}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: params,
});
return await response.json();
}
app.get(`${config.SUBPATH}/discord/callback`, async (req, res) => {
// this endpoint has been removed, redirect to /cas endpoint which spits the
// error message
res.redirect(`${config.SUBPATH}/cas`);
});
app.get(`${config.SUBPATH}/bot`, async (req, res) => {
if (!req.query.code || !req.query.guild_id) {
res.send("You are not discord :angry:", 400);
return;
}
const code = req.query.code;
const redirect_uri = `${config.BASE_URL}/bot`;
const responseJson = await makeQuery(code, redirect_uri);
if (responseJson && responseJson.access_token) {
res.send("Added successfully!");
} else {
logger.error(responseJson);
res.send("Unkown error occured");
}
});
const CAS = require("cas");
const cas = new CAS({
base_url: config.CAS_LINK,
service: config.BASE_URL,
version: 2.0,
});
app.get(`${config.SUBPATH}/cas`, async (req, res) => {
if (!req.query.token) {
res.send(
"Your verification link is invalid (may be expired or used already).\n" +
"Please run /verify again and use the new link",
400,
);
return;
}
await cas.authenticate(req, res, async (err, status, username, extended) => {
if (err) {
res.send("Some error occured with the CAS server :pensive:", 500);
} else {
if (!status) {
/* TODO: Identify what status false means */
res.send("Status false?", 500);
}
let rollno;
try {
rollno = extended.attributes.rollno[0];
} catch (e) {
rollno = "not-existent";
logger.info("User roll number does not exist");
}
let name = extended.attributes.name[0];
name = name
.split(" ")
.map((val) => val[0].toUpperCase() + val.substring(1))
.join(" ");
let form_data = new FormData();
form_data.append("name", name);
form_data.append("rollno", rollno);
form_data.append("email", extended.attributes["e-mail"][0]);
fetch(`http://${config.BOT_PRIVATE_IP}/${req.query.token}`, {
method: "POST",
body: form_data,
})
.then((response) => {
if (response.ok) {
res.send("You have successfully verified with the CAS login!");
} else if (response.status === 404) {
res.send(
"Your verification link expired!\n" +
"Please run /verify again and use the new link",
400,
);
} else {
throw new Error(response.status);
}
})
.catch((err) => {
res.send("Internal server error", 500);
logger.error(`Error in /cas fetch: ${err}`);
});
}
});
});
module.exports = app;