Skip to content

Commit 3ae6b62

Browse files
committed
update & fix http stuff
1 parent 4935a4a commit 3ae6b62

File tree

4 files changed

+69
-56
lines changed

4 files changed

+69
-56
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949
"@eartharoid/dbf": "^0.4.1",
5050
"@eartharoid/dtf": "^2.0.1",
5151
"@eartharoid/i18n": "^1.2.1",
52-
"@fastify/cookie": "^9.1.0",
53-
"@fastify/jwt": "^7.2.2",
54-
"@fastify/oauth2": "^7.5.0",
52+
"@fastify/cookie": "^9.3.1",
53+
"@fastify/jwt": "^8.0.0",
54+
"@fastify/oauth2": "^7.8.0",
5555
"@prisma/client": "^4.16.1",
5656
"boxen": "^7.1.0",
5757
"cryptr": "^6.2.0",
5858
"discord.js": "^14.11.0",
5959
"dotenv": "^16.0.3",
60-
"fastify": "^4.24.2",
60+
"fastify": "^4.25.2",
6161
"figlet": "^1.6.0",
6262
"fs-extra": "^10.1.0",
6363
"keyv": "^4.5.2",

pnpm-lock.yaml

+39-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/http.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ module.exports = async client => {
2525
fastify.states = new Map();
2626
fastify.register(oauth, {
2727
callbackUri: `${process.env.HTTP_EXTERNAL}/auth/callback`,
28-
checkStateFunction: (req, callback) => {
29-
// if (fastify.states.has(req.query.state)) {
30-
// callback();
31-
// return;
32-
// }
33-
console.log(req.session)
34-
if (req.query.state === req.session.state) {
35-
callback();
36-
return;
28+
// checkStateFunction: (req, callback) => {
29+
// if (req.query.state === req.cookies['oauth2-redirect-state']) {
30+
// callback();
31+
// return;
32+
// }
33+
// callback(new Error('Invalid state'));
34+
// },
35+
checkStateFunction: async req => {
36+
if (req.query.state !== req.cookies['oauth2-redirect-state']) {
37+
throw new Error('Invalid state');
3738
}
38-
callback(new Error('Invalid state'));
39+
return true;
3940
},
4041
credentials: {
4142
auth: oauth.DISCORD_CONFIGURATION,

src/routes/auth/callback.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ const { domain } = require('../../lib/http');
22

33
module.exports.get = () => ({
44
handler: async function (req, res) { // MUST NOT use arrow function syntax
5-
const {
6-
access_token: accessToken,
7-
expires_in: expiresIn,
8-
} = await this.discord.getAccessTokenFromAuthorizationCodeFlow(req);
5+
const data = await (await fetch('https://discord.com/api/oauth2/token', {
6+
body: new URLSearchParams({
7+
client_id: req.routeOptions.config.client.user.id,
8+
client_secret: process.env.DISCORD_SECRET,
9+
code: req.query.code,
10+
grant_type: 'authorization_code',
11+
redirect_uri: `${process.env.HTTP_EXTERNAL}/auth/callback`,
12+
}).toString(),
13+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
14+
method: 'POST',
15+
})).json();
916
const redirect = this.states.get(req.query.state) || '/';
1017
this.states.delete(req.query.state);
11-
const user = await (await fetch('https://discordapp.com/api/users/@me', { headers: { 'Authorization': `Bearer ${accessToken}` } })).json();
18+
const user = await (await fetch('https://discordapp.com/api/users/@me', { headers: { 'Authorization': `Bearer ${data.access_token}` } })).json();
1219
const token = this.jwt.sign({
13-
accessToken,
20+
accessToken: data.access_token,
1421
avatar: user.avatar,
1522
discriminator: user.discriminator,
16-
expiresAt: Date.now() + (expiresIn * 1000),
23+
expiresAt: Date.now() + (data.expires_in * 1000),
1724
id: user.id,
1825
locale: user.locale,
1926
username: user.username,
2027
});
2128
res.setCookie('token', token, {
2229
domain,
2330
httpOnly: true,
24-
maxAge: expiresIn,
31+
maxAge: data.expires_in,
2532
path: '/',
2633
sameSite: 'Lax',
2734
secure: false,

0 commit comments

Comments
 (0)