import { RegisterSingleton, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; import { cacheFor, ICacheContainer, ObjectCache } from "../../../common/cacheFor.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import { globalEnv } from "../../../common/globalEnv.js"; @RegisterSingleton export default class AccessTokenService implements ICacheContainer { cache: ObjectCache; async login(tokenID, secret) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); const tokens = await db.accessTokens .where({ tokenID, secret } , (x, p) => x.tokenID === p.tokenID as any && x.secret === p.secret) .updateSelect((x) => ({ lastUsed: Sql.date.now() })); const token = tokens[0]; if (!token) { throw new EntityAccessError(`Invalid access token`); } await db.changeSet.getEntry(token).loadNavigationAsync((x) => x.user); return token.user; } @cacheFor() async verifyToken(authorization: string) { using scope = ServiceProvider.createScope(this); authorization = authorization.split(" ").pop(); const db = scope.resolve(SocialMailContext); const [key, secret] = Buffer.from(authorization, "base64").toString("utf8").split(":").map((x) => decodeURIComponent(x)) as any[]; return await db.accessTokens.where({ key, secret }, (x, p) => x.tokenID === p.key && x.secret === p.secret) .firstOrFail(); } @cacheFor() async createLoginSession(authorization: string, ip: string) { using scope = ServiceProvider.createScope(this); authorization = authorization.split(" ").pop(); let userID; const db = scope.resolve(SocialMailContext); // check if it is parentApiToken.. if(globalEnv.virtualServers.clusterApiToken) { // allow direct login... // from parent cluster if(authorization === globalEnv.virtualServers.clusterApiToken) { const u = await db.userRoles .where((x) => x.roleName === "Administrator") .orderBy((x) => x.userID) .firstOrFail(); userID = u.userID; } } if(!userID) { const [key, secret] = Buffer.from(authorization, "base64").toString("utf8").split(":").map((x) => decodeURIComponent(x)) as any[]; const users = await db.accessTokens.where({ key, secret }, (x, p) => x.tokenID === p.key && x.secret === p.secret) .updateSelect((_x) => ({ lastUsed: Sql.date.now() })); const user = users[0]; if (!user) { return null; } userID = user.userID; } const now = DateTime.now; const session = await db.loginSessions.statements.insert({ userID, status: "created", invalid: false, ipAddress: ip, expiry: now.addDays(30), dateUpdated: now, start: now }); return session; } }