Compare commits

...

5 Commits

Author SHA1 Message Date
Bas950
411a70f567 chore: release v0.0.7 2024-09-11 21:02:59 +02:00
Bas950
4d1b092ee5 chore: hash the key 2024-09-11 21:02:44 +02:00
Bas950
aa41f1cdae chore: release v0.0.6 2024-09-11 20:31:04 +02:00
Bas950
04b5d54697 chore: update arktype 2024-09-11 20:30:48 +02:00
Bas950
fb096bc4be chore: release v0.0.4 2024-09-11 20:13:30 +02:00
7 changed files with 1650 additions and 764 deletions

View File

@@ -1,7 +1,7 @@
{
"name": "@premid/api-master",
"type": "module",
"version": "0.0.3",
"version": "0.0.4",
"private": true,
"description": "PreMiD's api master",
"license": "MPL-2.0",

View File

@@ -16,7 +16,7 @@ export async function clearOldSessions() {
for (const key of sessionKeys) {
const session = await redis.hgetall(key);
if (!session.t || !session.u) {
if (!session.t || !session.u || !session.s) {
await redis.del(key);
cleared++;
continue;
@@ -32,7 +32,7 @@ export async function clearOldSessions() {
discord.setToken(session.t);
await discord.post("/users/@me/headless-sessions/delete", {
body: {
token: key.split(":")[2], // Extract session token from key
token: session.s,
},
});
}

View File

@@ -1,7 +1,7 @@
{
"name": "@premid/api-worker",
"type": "module",
"version": "0.0.5",
"version": "0.0.7",
"private": true,
"description": "PreMiD's api",
"license": "MPL-2.0",
@@ -28,7 +28,7 @@
"@opentelemetry/node": "^0.24.0",
"@premid/db": "workspace:*",
"@sentry/node": "^8.17.0",
"arktype": "2.0.0-beta.2",
"arktype": "2.0.0-rc.6",
"defu": "^6.1.4",
"discord-api-types": "^0.37.92",
"fastify": "^4.28.1",

View File

@@ -5,6 +5,7 @@ import WebSocket from "ws";
import type { FastifyRequest } from "fastify";
import type { RawData } from "ws";
import { redis } from "../functions/createServer.js";
import { shortHash } from "../functions/shortHash.js";
import { counter } from "../tracing.js";
const schema = scope({
@@ -75,14 +76,15 @@ export class Socket {
const now = Math.floor(Date.now() / 1000);
await redis.hmset(
`pmd:session:${this.currentSession.token}`,
`pmd:session:${shortHash(this.currentSession.token, this.currentToken.token)}`,
{
t: this.currentToken.token,
u: now,
s: this.currentSession.token,
},
);
await redis.expire(`pmd:session:${this.currentSession.token}`, 60); // Expire after 1 minute
await redis.expire(`pmd:session:${shortHash(this.currentSession.token, this.currentToken.token)}`, 60); // Expire after 1 minute
}
async isTokenValid(token: typeof schema.token.infer) {

View File

@@ -0,0 +1,5 @@
import { createHash } from "node:crypto";
export function shortHash(...input: string[]) {
return createHash("sha256").update(input.join("")).digest("hex").slice(0, 16);
}

View File

@@ -4,6 +4,7 @@ import { type } from "arktype";
import { Routes } from "discord-api-types/v10";
import type { FastifyReply, FastifyRequest } from "fastify";
import { redis } from "../functions/createServer.js";
import { shortHash } from "../functions/shortHash.js";
const schema = type({
token: "string.trim",
@@ -26,15 +27,16 @@ export async function sessionKeepAlive(request: FastifyRequest, reply: FastifyRe
const now = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
await redis.hmset(
`pmd:session:${out.session}`,
`pmd:session:${shortHash(out.session, out.token)}`,
{
t: out.token,
u: now,
s: out.session,
},
);
// Set expiration for the hash
await redis.expire(`pmd:session:${out.session}`, 60); // Expire after 1 minute
await redis.expire(`pmd:session:${shortHash(out.session, out.token)}`, 60); // Expire after 1 minute
const interval = Number.parseInt(process.env.SESSION_KEEP_ALIVE_INTERVAL ?? "5000");

2387
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff