forked from zombieb/galvanic-corrosion-rewrite
Initial commit
This commit is contained in:
182
src/server/platforms/base.ts
Normal file
182
src/server/platforms/base.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
import z from "zod";
|
||||
import Command from "../commands/command.ts";
|
||||
import { ServerContentBase } from "../ContentBase.ts";
|
||||
import { transformStringToEnum } from "../../util/validators.ts";
|
||||
import { sign } from "@hono/hono/jwt";
|
||||
|
||||
export enum PlatformType {
|
||||
All = -1,
|
||||
Steam,
|
||||
Oculus,
|
||||
PlayStation,
|
||||
Xbox,
|
||||
WindowsPlatformless,
|
||||
IOS,
|
||||
GooglePlay
|
||||
}
|
||||
|
||||
export enum DeviceClass {
|
||||
Unknown,
|
||||
VR,
|
||||
Screen,
|
||||
Mobile,
|
||||
VRLow,
|
||||
Quest2
|
||||
}
|
||||
|
||||
|
||||
interface DbCachedLogin {
|
||||
accountId: number,
|
||||
lastLoginTime: Date,
|
||||
requirePassword: boolean
|
||||
}
|
||||
export interface CachedLogin extends DbCachedLogin {
|
||||
platformId: string
|
||||
platform: PlatformType
|
||||
}
|
||||
|
||||
export const steamAuthTicketSchema = z.object({
|
||||
Ticket: z.string().min(256),
|
||||
AppId: z.literal("471710")
|
||||
});
|
||||
|
||||
export enum ProfileRole {
|
||||
Developer = "developer",
|
||||
Moderator = "moderator",
|
||||
Screenshare = "screenshare",
|
||||
User = "user"
|
||||
}
|
||||
interface TokenFormat {
|
||||
iss: string,
|
||||
exp: number,
|
||||
sub: number,
|
||||
role: string
|
||||
}
|
||||
|
||||
export class PlatformsManager extends ServerContentBase {
|
||||
|
||||
static platformsKey = "platforms";
|
||||
|
||||
#constructPlatformKey(...keys: (string | number | undefined)[]) {
|
||||
return [PlatformsManager.platformsKey, ...keys.filter(val => typeof val == 'string')];
|
||||
}
|
||||
|
||||
async getToken(accountId: number, role: string) {
|
||||
const secret = Deno.env.get('SECRET');
|
||||
if (!secret) throw new Error("No SECRET in env. Did you forget to set it?");
|
||||
|
||||
const token: TokenFormat = {
|
||||
sub: accountId,
|
||||
role,
|
||||
iss: "https://wsi.proxnet.dev/auth/",
|
||||
exp: Math.round(Date.now() / 1000) + 21_600
|
||||
}
|
||||
return await sign(JSON.parse(JSON.stringify(token)), secret);
|
||||
}
|
||||
|
||||
async updateLastLoginTime(platform: PlatformType, platformId: string, accountId: number) {
|
||||
const key = this.#constructPlatformKey(platform, platformId);
|
||||
const set = await this.kv.getKv().get<Set<DbCachedLogin>>(key);
|
||||
|
||||
if (set.value) {
|
||||
const existing = set.value.values().toArray().find(val => val.accountId == accountId);
|
||||
if (!existing) return;
|
||||
|
||||
existing.lastLoginTime = new Date();
|
||||
await this.kv.getKv().set(key, set.value);
|
||||
}
|
||||
}
|
||||
|
||||
async addCachedLogin(platform: PlatformType, platformId: string, accountId: number) {
|
||||
const key = this.#constructPlatformKey(platform, platformId);
|
||||
const set = await this.kv.getKv().get<Set<DbCachedLogin>>(key);
|
||||
|
||||
if (set.value) {
|
||||
const existing = set.value.values().toArray().find(val => val.accountId == accountId);
|
||||
if (!existing) {
|
||||
set.value.add({ accountId, lastLoginTime: new Date(), requirePassword: false });
|
||||
await this.kv.getKv().set(key, set.value);
|
||||
}
|
||||
|
||||
return set.value.values().toArray();
|
||||
} else {
|
||||
const newSet = new Set<DbCachedLogin>([{ accountId, lastLoginTime: new Date(), requirePassword: false }]);
|
||||
await this.kv.getKv().set(key, newSet);
|
||||
return newSet.values().toArray();
|
||||
};
|
||||
}
|
||||
|
||||
async getCachedLogins(platform: PlatformType, platformId: string, format: true): Promise<CachedLogin[] | null>
|
||||
async getCachedLogins(platform: PlatformType, platformId: string, format: false): Promise<DbCachedLogin[] | null>
|
||||
async getCachedLogins(platform: PlatformType, platformId: string, format?: boolean) {
|
||||
const set = await this.kv.getKv().get<Set<DbCachedLogin>>(this.#constructPlatformKey(platform, platformId));
|
||||
if (set.value && format) return set.value.values().toArray().map(val => ({
|
||||
platform,
|
||||
platformId,
|
||||
accountId: val.accountId,
|
||||
lastLoginTime: val.lastLoginTime,
|
||||
requirePassword: val.requirePassword
|
||||
} as CachedLogin));
|
||||
else if (set.value) return set.value.values().toArray();
|
||||
else return null;
|
||||
}
|
||||
|
||||
async deleteCachedLogin(platform: PlatformType, platformId: string, accountId: number) {
|
||||
const key = this.#constructPlatformKey(platform, platformId);
|
||||
const set = await this.kv.getKv().get<Set<DbCachedLogin>>(key);
|
||||
|
||||
if (set.value) {
|
||||
const existing = set.value.values().toArray().find(val => val.accountId == accountId);
|
||||
if (existing) {
|
||||
set.value.delete(existing);
|
||||
await this.kv.getKv().set(key, set.value);
|
||||
}
|
||||
|
||||
return set.value.values().toArray();
|
||||
} else return null;
|
||||
}
|
||||
|
||||
override start() {
|
||||
this.server.Commands.addRootCommand(new Command({
|
||||
key: ['platforms', 'pm', 'platformmanager', 'platformanager'],
|
||||
subcommands: [
|
||||
new Command({
|
||||
key: ['get', 'g', 'list', 'l'],
|
||||
exec: async (type: PlatformType, platformId: string) => {
|
||||
return await this.getCachedLogins(type, platformId, false);
|
||||
},
|
||||
zod: z.tuple([
|
||||
z.string().transform(transformStringToEnum<PlatformType>(PlatformType)),
|
||||
z.string().min(4)
|
||||
]),
|
||||
help: 'List all cachedlogins for platformId: <type: PlatformType, platformId: string>'
|
||||
}),
|
||||
new Command({
|
||||
key: ['set', 's', 'add', 'a'],
|
||||
exec: async (type: PlatformType, platformId: string, accountId: number) => {
|
||||
return await this.addCachedLogin(type, platformId, accountId);
|
||||
},
|
||||
zod: z.tuple([
|
||||
z.string().transform(transformStringToEnum<PlatformType>(PlatformType)),
|
||||
z.string(),
|
||||
z.string().transform(Number)
|
||||
]),
|
||||
help: 'Add a cachedlogin for platformId: <type: PlatformType>, <platformId: string>, <accountId: number>'
|
||||
}),
|
||||
new Command({
|
||||
key: ['del', 'd', 'rem', 'remove', 'r'],
|
||||
exec: async (type: PlatformType, platformId: string, accountId: number) => {
|
||||
return await this.deleteCachedLogin(type, platformId, accountId);
|
||||
},
|
||||
zod: z.tuple([
|
||||
z.string().transform(transformStringToEnum<PlatformType>(PlatformType)),
|
||||
z.string(),
|
||||
z.string().transform(Number)
|
||||
]),
|
||||
help: 'Remove a cachedlogin for platformId: <type: PlatformType>, <platformId: string>, <accountId: number>'
|
||||
})
|
||||
]
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user