From 758e2ef7e2f5f2b4c5445c5fcbf9bc96a4bfc78a Mon Sep 17 00:00:00 2001 From: oat Date: Thu, 3 Sep 2020 10:28:22 +0300 Subject: [PATCH] discord auth bay bee this just, , tells you what your username is right now but will definetly be used later on to login --- .env.example | 2 ++ .gitignore | 3 ++- package-lock.json | 18 ++++++++++++++++++ package.json | 2 ++ src/auth.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/index.ts | 5 +++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 .env.example create mode 100644 src/auth.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..1446139 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +DISCORD_OAUTH_CLIENTID="" +DISCORD_OAUTH_CLIENTSECRET="" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 590ec1c..4acb044 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules built config/config.json -*.log \ No newline at end of file +*.log +.env \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9ec1de8..e9cd86e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -422,6 +422,14 @@ "resolved": "https://registry.npmjs.org/async/-/async-3.2.0.tgz", "integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==" }, + "axios": { + "version": "0.20.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz", + "integrity": "sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -699,6 +707,11 @@ "esutils": "^2.0.2" } }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -1051,6 +1064,11 @@ "resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz", "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" }, + "follow-redirects": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz", + "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==" + }, "forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", diff --git a/package.json b/package.json index b3188f9..cbac83e 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,8 @@ "dependencies": { "@types/express": "github:types/express", "@types/mongoose": "^5.7.36", + "axios": "^0.20.0", + "dotenv": "^8.2.0", "express": "^4.17.1", "express-fileupload": "^1.2.0", "mongoose": "^5.10.2", diff --git a/src/auth.ts b/src/auth.ts new file mode 100644 index 0000000..5b730f2 --- /dev/null +++ b/src/auth.ts @@ -0,0 +1,40 @@ +const API_ENDPOINT = 'https://discord.com/api/v6'; + +const axios = require('axios').default; + +export function run(app) { + app.get('/discordauth', async (req, res) => { + const code = req.query.code; + + if (code) { + try { + const data = new URLSearchParams({ + client_id: process.env.DISCORD_OAUTH_CLIENTID, + client_secret: process.env.DISCORD_OAUTH_CLIENTSECRET, + grant_type: 'authorization_code', + code: code, + redirect_uri: 'http://localhost:8080/discordauth', + scope: 'identify' + }); + + const postRes = await axios.post(`${API_ENDPOINT}/oauth2/token`, data, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }); + + const userInfo = await axios.get(`${API_ENDPOINT}/users/@me`, { + headers: { + authorization: `${postRes.data.token_type} ${postRes.data.access_token}` + } + }); + + res.send(`hi ${userInfo.data.username}#${userInfo.data.discriminator}`); + } catch(err) { + res.send(`whoooops
${err}`); + } + } else { + res.send('https://discord.com/api/oauth2/authorize?client_id=750952563079250012&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fdiscordauth&response_type=code&scope=identify'); + } + }); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index cc1f8e9..f3a01a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,10 @@ import * as format from './lib/format'; import { File } from './schema'; import * as upload from './upload'; +import * as auth from './auth'; + +// .env stuff +require('dotenv').config(); const config = JSON.parse(fs.readFileSync('./config/config.json', {encoding: 'utf8'})); @@ -55,6 +59,7 @@ db.then(() => { app.set('logger', logger); upload.run(app); + auth.run(app); app.get('/list', async (req, res) => { // only for testing const docs = await File.find({});