Removed web project (galvanic authentication support in IL2CPP universal patch)
Moved instance ID to header User instances for profile management .. other stuff
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
// @ts-types = "npm:@types/express"
|
||||
import express from "express";
|
||||
import Logging from "@proxnet/undead-logging";
|
||||
import { decode } from "@gz/jwt";
|
||||
import { Config } from "./config.ts";
|
||||
import { AuthType, User, UserTokenFormat } from "./data/users.ts";
|
||||
|
||||
const config = Config.getConfig();
|
||||
|
||||
const log = new Logging('APIUtils');
|
||||
|
||||
@@ -29,8 +34,6 @@ export function generateRandomString(length: number) {
|
||||
return randomString;
|
||||
}
|
||||
|
||||
const instanceId = generateRandomString(128);
|
||||
|
||||
export function checkQueryTypes<T>(typeDef: T) {
|
||||
return (rq: express.Request, rs: express.Response, nxt: express.NextFunction) => {
|
||||
for (const key in typeDef) {
|
||||
@@ -58,11 +61,11 @@ export function checkBodyTypes<T>(typeDef: T) {
|
||||
}
|
||||
|
||||
export function genericResponseFormat(failure: boolean, msg: string | null = null, data: object | null = null) {
|
||||
return { failed: failure, instance: instanceId, message: msg, data: data };
|
||||
return { failed: failure, message: msg, data: data };
|
||||
}
|
||||
export function genericResponse(failure: boolean, msg: string | null = null, data: object | null = null) {
|
||||
return (_rq: express.Request, rs: express.Response) => {
|
||||
rs.json({ failed: failure, instance: instanceId, message: msg, data: data });
|
||||
rs.json({ failed: failure, message: msg, data: data });
|
||||
};
|
||||
}
|
||||
type RecNetResponse = {
|
||||
@@ -165,4 +168,45 @@ export class RateLimiter {
|
||||
|
||||
}
|
||||
|
||||
export async function UserAuthentication(rq: express.Request, rs: express.Response, nxt: express.NextFunction) {
|
||||
|
||||
function returnUnauthorized() {
|
||||
rs.statusCode = 401;
|
||||
rs.json(genericResponseFormat(true, 'Authorization required.'));
|
||||
}
|
||||
|
||||
const token: string | undefined = rq.header('GalvanicAuth');
|
||||
if (typeof token == 'undefined') {
|
||||
returnUnauthorized();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const decodedToken = await decode<UserTokenFormat>(token, config.auth.secret, { algorithm: "HS512" });
|
||||
|
||||
const valid = ![
|
||||
decodedToken.iss == config.web.publichost,
|
||||
decodedToken.nbf < Math.round(Date.now() / 1000),
|
||||
decodedToken.exp > Math.round(Date.now() / 1000),
|
||||
decodedToken.typ == AuthType.Web
|
||||
].includes(false);
|
||||
if (valid) {
|
||||
rs.locals.user = new User(decodedToken.sub);
|
||||
nxt();
|
||||
}
|
||||
else {
|
||||
returnUnauthorized();
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
returnUnauthorized();
|
||||
log.w(`User Authentication failed: ${err}`);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export type NoBody = Record<string | number | symbol, never>
|
||||
|
||||
export * as APIUtils from "./apiutils.ts"
|
||||
Reference in New Issue
Block a user