95 lines
2.9 KiB
TypeScript
95 lines
2.9 KiB
TypeScript
import {MongoClient, type InsertOneResult} from "mongodb";
|
|
import {API} from "osu-api-v2-js";
|
|
import type { Handler } from "..";
|
|
|
|
export interface Token {
|
|
access_token: string;
|
|
expires: Date;
|
|
}
|
|
|
|
export const token: Handler = async (params) => {
|
|
const service = params.get("service");
|
|
if (!service) {
|
|
return new Response("Bad Request", {status: 400});
|
|
}
|
|
|
|
const client = new MongoClient(process.env["URL_MONGODB"]!);
|
|
await client.connect();
|
|
|
|
const db = client.db("tokens");
|
|
const collection = db.collection<Token>(service);
|
|
const tokens = await collection.find().toArray();
|
|
|
|
const now = new Date();
|
|
const token = tokens.find((t) => t.expires > now);
|
|
const expiredTokens = tokens.filter((t) => now > t.expires);
|
|
|
|
const promises: Promise<void>[] = [];
|
|
|
|
if (!token) {
|
|
const collections = await db.listCollections().toArray();
|
|
if (!collections.find((c) => c.name === service)) {client.close(); return new Response("Not Found", {status: 404});}
|
|
|
|
promises.push(new Promise(async (resolve, reject) => {
|
|
console.log(`Setting a new token for ${service}...`);
|
|
let insertion: InsertOneResult;
|
|
|
|
if (service === "osu") {
|
|
const api = await API.createAsync(11451, process.env["API_OSU"]!);
|
|
insertion = await collection.insertOne({
|
|
access_token: api.access_token,
|
|
expires: api.expires,
|
|
});
|
|
}
|
|
|
|
else if (service === "umami") {
|
|
const response = await fetch("https://visitors.taevas.xyz/api/auth/login", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
},
|
|
body: `username=${process.env["USERNAME_UMAMI"]}&password=${process.env["PASSWORD_UMAMI"]}`
|
|
});
|
|
const json: {token: string} = await response.json();
|
|
|
|
// Assume it expires in one day
|
|
const date = new Date();
|
|
date.setHours(date.getHours() + 24);
|
|
insertion = await collection.insertOne({
|
|
access_token: json.token,
|
|
expires: date,
|
|
});
|
|
}
|
|
|
|
else {
|
|
console.error(`Service "${service}" doesn't exist! Unable to set a new token...`);
|
|
return reject();
|
|
}
|
|
|
|
console.log(`New ${service} token in the database: ${insertion.insertedId.toString()}`);
|
|
resolve();
|
|
}));
|
|
}
|
|
|
|
if (expiredTokens.length) {
|
|
promises.push(new Promise(async (resolve) => {
|
|
console.log(`Deleting old tokens for ${service}...`);
|
|
await Promise.all(expiredTokens.map(async (t) => {
|
|
return new Promise<void>(async (resolve) => {
|
|
const deletion = await collection.deleteOne({_id: t._id});
|
|
if (deletion.deletedCount) {
|
|
console.log(`Old ${service} token deleted from the database: ${t._id.toString()}`);
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
}));
|
|
resolve();
|
|
}));
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
void client.close();
|
|
|
|
return new Response(null, {status: 200});
|
|
};
|