Skip to content

Commit

Permalink
Add oauth lib code & more structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Oct 31, 2023
1 parent bf853bc commit cbb6db0
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/

config.json
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# backend
The backend for DisGitHook which handles settings & webhooks
# DisGitHook Backend

This repository contains the backend code for the DisGitHook project.

It contains the API for the website and the code for handling webhooks.
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
const { db } = require("../config.json")

const mysql = require("mysql2")
const mainDB = mysql.createPool(db)
const pool = mainDB.promise()

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / Codestandards

'pool' is assigned a value but never used. Allowed unused vars must match /_|updateSlashcommands|updateLinkedroles/u

const express = require("express")
const app = express()

// - Dashboard -

app.get("/", (req, res) => {
res.send({ hello: "world" })
res.send("ok")
})

app.get("/auth/login", (req, res) => {
res.send("ok")
})
app.get("/auth/logout", (req, res) => {
res.send("ok")
})

// - Hooks -

const hookFunc = async (req, res) => {
res.send({ hello: "world" })
res.sendStatus(204)
}
app.get("/hook/:id/:secret", hookFunc)
app.post("/hook/:id/:secret", hookFunc)
Expand Down
96 changes: 96 additions & 0 deletions package-lock.json

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

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
"name": "disgithook-backend",
"version": "1.0.0",
"private": true,
"description": "The backend for DisGitHook which handles settings & webhooks",
"description": "The backend for DisGitHook which handles settings & webhooks",
"main": "index.js",
"author": "DisGitHook community",
"license": "ISC",
"scripts": {
"nodemon": "nodemon index.js"
},
"dependencies": {
"express": "^4.18.2"
"express": "^4.18.2",
"mysql2": "^3.6.2",
"nodemon": "^3.0.1"
},
"devDependencies": {
"eslint": "^8.52.0",
Expand Down
73 changes: 73 additions & 0 deletions util/oauth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { botId, botSecret, userAgent } = require("../config.json")

const userCache = {}
module.exports.getUser = async token => {
if (userCache[token]) return userCache[token]
const res = await fetch("https://discord.com/api/v10/users/@me", {
headers: {
"User-Agent": userAgent,
Authorization: "Bearer " + token,
Accept: "application/json"
}
})
if (!res.ok) return new Error("Couldnt get user data, failed with " + res.status + " " + res.statusText)
const json = await res.json()
userCache[token] = json
setTimeout(() => delete userCache[token], 1000 * 60 * 10)
return json
}

const guildCache = {}
module.exports.getUserGuilds = async token => {
if (guildCache[token]) return guildCache[token]
const res = await fetch("https://discord.com/api/v10/users/@me/guilds", {
headers: {
"User-Agent": userAgent,
Authorization: "Bearer " + token,
Accept: "application/json"
}
})
if (!res.ok) {
console.log("Couldnt get guild data, failed with " + res.status + " " + res.statusText)
return new Error("Couldnt get guild data, failed with " + res.status + " " + res.statusText)
}
const json = await res.json()
guildCache[token] = json
setTimeout(() => delete guildCache[token], 1000 * 60 * 10)
return json
}

module.exports.getAccessToken = async (userId, tokens, bot) => {

Check failure on line 40 in util/oauth.js

View workflow job for this annotation

GitHub Actions / Codestandards

'bot' is defined but never used. Allowed unused args must match /args|next/u
if (Date.now() > tokens.expires_at) {
const body = new URLSearchParams({
client_id: botId,
client_secret: botSecret,
grant_type: "refresh_token",
refresh_token: tokens.refresh_token
})
const res = await fetch("https://discord.com/api/v10/oauth2/token", {
method: "POST",
body,
headers: {
"User-Agent": userAgent,
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json"
}
})
if (res.ok) {
const newtokens = await res.json()
newtokens.expires_at = Date.now() + newtokens.expires_in * 1000
newtokens.type = tokens.type
if (tokens.username) newtokens.username = tokens.username
delete newtokens.expires_in

// TODO: Store "newtokens" in the database

Check failure on line 64 in util/oauth.js

View workflow job for this annotation

GitHub Actions / Codestandards

Unexpected 'todo' comment: 'TODO: Store "newtokens" in the database'
return newtokens.access_token
} else {
console.log("Error refreshing access token: " + res.status + " " + res.statusText)
const json = await res.json()
console.log(json)
}
}
return tokens.access_token
}

0 comments on commit cbb6db0

Please sign in to comment.