Skip to content

Commit

Permalink
update & fix http stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Jan 21, 2024
1 parent 4935a4a commit 3ae6b62
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 56 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
"@eartharoid/dbf": "^0.4.1",
"@eartharoid/dtf": "^2.0.1",
"@eartharoid/i18n": "^1.2.1",
"@fastify/cookie": "^9.1.0",
"@fastify/jwt": "^7.2.2",
"@fastify/oauth2": "^7.5.0",
"@fastify/cookie": "^9.3.1",
"@fastify/jwt": "^8.0.0",
"@fastify/oauth2": "^7.8.0",
"@prisma/client": "^4.16.1",
"boxen": "^7.1.0",
"cryptr": "^6.2.0",
"discord.js": "^14.11.0",
"dotenv": "^16.0.3",
"fastify": "^4.24.2",
"fastify": "^4.25.2",
"figlet": "^1.6.0",
"fs-extra": "^10.1.0",
"keyv": "^4.5.2",
Expand Down
73 changes: 39 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ module.exports = async client => {
fastify.states = new Map();
fastify.register(oauth, {
callbackUri: `${process.env.HTTP_EXTERNAL}/auth/callback`,
checkStateFunction: (req, callback) => {
// if (fastify.states.has(req.query.state)) {
// callback();
// return;
// }
console.log(req.session)
if (req.query.state === req.session.state) {
callback();
return;
// checkStateFunction: (req, callback) => {
// if (req.query.state === req.cookies['oauth2-redirect-state']) {
// callback();
// return;
// }
// callback(new Error('Invalid state'));
// },
checkStateFunction: async req => {
if (req.query.state !== req.cookies['oauth2-redirect-state']) {
throw new Error('Invalid state');
}
callback(new Error('Invalid state'));
return true;
},
credentials: {
auth: oauth.DISCORD_CONFIGURATION,
Expand Down
23 changes: 15 additions & 8 deletions src/routes/auth/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ const { domain } = require('../../lib/http');

module.exports.get = () => ({
handler: async function (req, res) { // MUST NOT use arrow function syntax
const {
access_token: accessToken,
expires_in: expiresIn,
} = await this.discord.getAccessTokenFromAuthorizationCodeFlow(req);
const data = await (await fetch('https://discord.com/api/oauth2/token', {
body: new URLSearchParams({
client_id: req.routeOptions.config.client.user.id,
client_secret: process.env.DISCORD_SECRET,
code: req.query.code,
grant_type: 'authorization_code',
redirect_uri: `${process.env.HTTP_EXTERNAL}/auth/callback`,
}).toString(),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
})).json();
const redirect = this.states.get(req.query.state) || '/';
this.states.delete(req.query.state);
const user = await (await fetch('https://discordapp.com/api/users/@me', { headers: { 'Authorization': `Bearer ${accessToken}` } })).json();
const user = await (await fetch('https://discordapp.com/api/users/@me', { headers: { 'Authorization': `Bearer ${data.access_token}` } })).json();
const token = this.jwt.sign({
accessToken,
accessToken: data.access_token,
avatar: user.avatar,
discriminator: user.discriminator,
expiresAt: Date.now() + (expiresIn * 1000),
expiresAt: Date.now() + (data.expires_in * 1000),
id: user.id,
locale: user.locale,
username: user.username,
});
res.setCookie('token', token, {
domain,
httpOnly: true,
maxAge: expiresIn,
maxAge: data.expires_in,
path: '/',
sameSite: 'Lax',
secure: false,
Expand Down

0 comments on commit 3ae6b62

Please sign in to comment.