oh lord
This commit is contained in:
parent
a01abce47b
commit
ec6f858cf2
7 changed files with 57 additions and 1 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -6,7 +6,8 @@
|
|||
"build": "bun build . --compile --outfile=build/garf-api"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
"@types/bun": "latest",
|
||||
"@types/node": "^20.11.29"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
|
|
2
src/env.d.ts
vendored
2
src/env.d.ts
vendored
|
@ -2,5 +2,7 @@ declare module "bun" {
|
|||
interface Env {
|
||||
DISCORD_CLIENT_ID: string;
|
||||
DISCORD_CLIENT_SECRET: string;
|
||||
ENCRYPTION_KEY: string;
|
||||
ENCRYPTION_SALT: string;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import token from "./routes/token";
|
||||
import config from "./routes/config";
|
||||
import negotiate from "./routes/negotiate";
|
||||
|
||||
Bun.serve({
|
||||
port: 3000,
|
||||
|
@ -10,8 +11,15 @@ Bun.serve({
|
|||
|
||||
if (url.pathname === "/") return await config(req);
|
||||
if (url.pathname === "/token") return await token(req);
|
||||
if (url.pathname === "/negotiate") return await negotiate(req);
|
||||
return new Response("garf dont know what want...");
|
||||
},
|
||||
websocket: {
|
||||
message(ws, message) {},
|
||||
open(ws) {},
|
||||
close(ws, code, message) {},
|
||||
drain(ws) {},
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Listening on 0.0.0.0:3000");
|
||||
|
|
37
src/lib/encryption.ts
Normal file
37
src/lib/encryption.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { createCipheriv, createDecipheriv, scrypt, randomBytes } from "node:crypto";
|
||||
|
||||
const KEY = ((await scryptAsync(process.env.ENCRYPTION_KEY, 32)) as Buffer).toString("hex");
|
||||
|
||||
function scryptAsync(password: string, length: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const pwBuf = Buffer.from(password);
|
||||
scrypt(pwBuf, process.env.ENCRYPTION_SALT, length, (err, key) => {
|
||||
if (err) return reject(err);
|
||||
resolve(key);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function encrypt(data: string) {
|
||||
const iv = randomBytes(16);
|
||||
const cipher = createCipheriv("aes-256-ccm", KEY, iv);
|
||||
|
||||
let encrypted = cipher.update(data, "utf8", "base64url");
|
||||
encrypted += cipher.final("base64url");
|
||||
|
||||
return { encrypted, iv: iv.toString("hex") };
|
||||
}
|
||||
|
||||
export async function decrypt(data: string, iv: string) {
|
||||
const ivBuf = Buffer.from(iv, "hex");
|
||||
const decipher = createDecipheriv("aes-256-ccm", KEY, ivBuf);
|
||||
|
||||
let decrypted = decipher.update(data, "base64url", "utf-8");
|
||||
try {
|
||||
decrypted += decipher.final("utf8");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return { decrypted };
|
||||
}
|
3
src/routes/negotiate.ts
Normal file
3
src/routes/negotiate.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default async function (req: Request): Promise<Response> {
|
||||
return new Response();
|
||||
}
|
|
@ -30,6 +30,11 @@ export default async function (req: Request): Promise<Response> {
|
|||
});
|
||||
|
||||
const { access_token } = (await response.json()) as TokenResponse;
|
||||
const userResponse = await fetch("https://discord/api/v10/user/@me", {
|
||||
headers: { Authorization: "Bearer " + access_token },
|
||||
});
|
||||
|
||||
console.log(await userResponse.json());
|
||||
|
||||
return new Response(JSON.stringify({ access_token }), {
|
||||
headers: {
|
||||
|
|
Loading…
Reference in a new issue