UNPKG

@xmobitea/gn-server

Version:

GearN Server by XmobiTea (Pro)

183 lines (182 loc) 7.93 kB
import express from "express"; import { AuthInfo } from "./../../GN-common/entity/AuthInfo"; import * as mongodb from "mongodb"; import { Socket } from "socket.io"; import { SecretInfo } from "./../../GN-common/entity/SecretInfo"; import jwt from "jsonwebtoken"; import { TokenPayload } from "./../../GN-common/entity/TokenPayload"; /** * Central auth/secret middleware shared by HTTP and socket transports. */ export declare class ApiMiddleware { private static readonly FIVE_MINUTE_TS; private static readonly TS_REMOVE_AUTH_INFO_DIC; private static readonly DEFAULT_GAME_ID; private authInfoDict; private authInfoCollection; private secretInfoDict; private secretInfoCollection; private playerGameCollection; private expiredToken; private privateKeyToken; private headerIP; private dauLast30Min; private maxCCU; private gameIds; /** * Sets the secret info collection. * @param secretInfoCollection Provides the secret info collection used by this operation. */ setSecretInfoCollection(secretInfoCollection: mongodb.Collection<mongodb.Document>): void; /** * Sets the auth info collection. * @param authInfoCollection Provides the auth info collection used by this operation. */ setAuthInfoCollection(authInfoCollection: mongodb.Collection<mongodb.Document>): void; /** * Sets the player game collection. * @param playerGameCollection Provides the player game collection used by this operation. */ setPlayerGameCollection(playerGameCollection: mongodb.Collection<mongodb.Document>): void; /** * Sets the expired token. * @param expiredToken Provides the expired token value used by this operation. */ setExpiredToken(expiredToken: number): void; /** * Sets the private key token. * @param privateKeyToken Provides the private key token value used by this operation. */ setPrivateKeyToken(privateKeyToken: string): void; /** * Sets the header IP. * @param headerIP Provides the header IP value used by this operation. */ setHeaderIP(headerIP: string): void; /** * Returns the header IP. */ getHeaderIP(): string; /** * Sets the game IDs. * @param gameIds Provides the game IDs value used by this operation. */ setGameIds(gameIds: string[]): void; /** * Starts periodic maintenance for the auth cache and DAU/CCU helper state. */ run(): void; /** * Refreshes the recent-user cache consulted by the optional CCU gate. */ private getDAULast30Min; /** * Evicts stale auth entries from the in-memory cache without touching MongoDB. */ private checkAndRemoveDict; /** * Mints a signed auth token, mirrors it to MongoDB, and caches the resolved `AuthInfo`. * @param payload Provides the payload value used by this operation. * @returns Returns the operation result. */ generateAuthInfo(payload: TokenPayload): Promise<AuthInfo>; /** * Mints a signed auth token for admin, mirrors it to MongoDB, and caches the resolved `AuthInfo`. * @param payload Provides the payload value used by this operation. * @returns Returns the operation result. */ generateAuthInfoForAdmin(payload: TokenPayload): Promise<AuthInfo>; /** * Mints a signed auth token for cloudScript, mirrors it to MongoDB, and caches the resolved `AuthInfo`. * @param payload Provides the payload value used by this operation. * @returns Returns the operation result. */ generateAuthInfoForCloudScript(payload: TokenPayload, expiredAfterMs: number): Promise<AuthInfo>; /** * Executes the verify auth token workflow. * @param authToken Provides the auth token value used by this operation. * @returns Returns the operation result. */ verifyAuthToken(authToken: string): jwt.JwtPayload; /** * Returns the secret info by secret key in database. * @param secretKey Provides the secret key value used by this operation. * @returns Returns the secret info by secret key in database. */ getSecretInfoBySecretKeyInDatabase(secretKey: string): Promise<SecretInfo>; /** * Returns the auth info by GearN token in database. * @param gnToken Provides the GearN token value used by this operation. * @returns Returns the auth info by GearN token in database. */ getAuthInfoByGNTokenInDatabase(gnToken: string): Promise<AuthInfo>; /** * Returns the auth infos by user ID in mem cache. * @param userId Indicates whether r ID should be used. * @returns Returns the auth infos by user ID in mem cache. */ getAuthInfosByUserIdInMemCache(userId: string): AuthInfo[]; /** * Executes the update secret info workflow asynchronously. * @param secretInfo Provides the secret info value used by this operation. * @param withoutSave If yes, no save to db. */ updateSecretInfo(secretInfo: SecretInfo, withoutSave?: boolean): Promise<void>; /** * Executes the update auth info workflow asynchronously. * @param authInfo Provides the auth info value used by this operation. * @param withoutSave If yes, no save to db. */ updateAuthInfo(authInfo: AuthInfo, withoutSave?: boolean): Promise<void>; removeAllAuthInfosInDb(userId: string): Promise<void>; /** * Executes the authenticate token via JSON workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ authenticateTokenViaJson(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Executes the authenticate upload token workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ authenticateUploadToken(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Executes the authenticate token via msg pack workflow asynchronously. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ authenticateTokenViaMsgPack(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Returns the IP. * @param req Provides the req value used by this operation. * @param res Provides the res value used by this operation. * @param next Provides the next value used by this operation. */ getIP(req: express.Request, res: express.Response, next: Function): Promise<any>; /** * Resolves request auth state for HTTP, validates the active secret, and returns the final gate result. * @param req Provides the req value used by this operation. * @returns Returns the return code authenticate. */ getReturnCodeAuthenticate(req: express.Request): Promise<number>; /** * Performs the same auth/secret resolution for sockets, but keeps the result on `socket.data`. * @param socket Provides the socket value used by this operation. * @param next Provides the next value used by this operation. */ socketAuthenticateToken(socket: Socket, next: (returnCode: number) => void): Promise<void>; /** * Normalizes IPv6-mapped IPv4 addresses to the plain IPv4 string used across the codebase. * @param ip Provides the IP value used by this operation. * @returns Returns the operation result. */ createIP(ip: string): string; /** * Initializes a new API middleware instance. */ constructor(); }