Skip to content

Commit

Permalink
add auth logs
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed May 15, 2024
1 parent fd8810d commit 7e319cb
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
26 changes: 20 additions & 6 deletions lib/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export type Auth<S = void, SUser extends SessionUser = SessionUser> = {
}
}

export default class AuthHandler {
export class AuthHandler {
protected prostgles: Prostgles;
protected opts?: Auth;
dbo: DBHandlerServer;
Expand Down Expand Up @@ -292,13 +292,14 @@ export default class AuthHandler {
}

getUser = async (clientReq: { httpReq: ExpressReq; }): Promise<AuthResult> => {
if(!this.sidKeyName || !this.opts?.getUser) throw "sidKeyName or this.opts.getUser missing"
if(!this.sidKeyName || !this.opts?.getUser) {
throw "sidKeyName or this.opts.getUser missing";
}
const sid = clientReq.httpReq?.cookies?.[this.sidKeyName];
if (!sid) return undefined;

try {
return this.throttledFunc(async () => {

return this.opts!.getUser(this.validateSid(sid), this.dbo as any, this.db, getLoginClientInfo(clientReq));
}, 50)
} catch (err) {
Expand Down Expand Up @@ -376,8 +377,15 @@ export default class AuthHandler {

app.post(loginRoute, async (req: ExpressReq, res: ExpressRes) => {
try {
const start = Date.now();
const { sid, expires } = await this.loginThrottled(req.body || {}, getLoginClientInfo({ httpReq: req })) || {};

await this.prostgles.opts.onLog?.({
type: "auth",
command: "login",
duration: Date.now() - start,
sid,
socketId: undefined,
})
if (sid) {

this.setCookieAndGoToReturnURLIFSet({ sid, expires }, { req, res });
Expand All @@ -390,7 +398,6 @@ export default class AuthHandler {
res.status(404).json({ err });
}


});

if (this.routes.logoutGetPath && this.opts.logout) {
Expand All @@ -399,7 +406,6 @@ export default class AuthHandler {
if (sid) {
try {
await this.throttledFunc(() => {

return this.opts!.logout!(req?.cookies?.[sidKeyName], this.dbo as any, this.db);
})
} catch (err) {
Expand Down Expand Up @@ -612,6 +618,7 @@ export default class AuthHandler {
}
}

const authStart = Date.now();
const res = await this.throttledFunc(async () => {

const { getUser } = this.opts ?? {};
Expand Down Expand Up @@ -643,6 +650,13 @@ export default class AuthHandler {
return {};
}, 5);

await this.prostgles.opts.onLog?.({
type: "auth",
command: "getClientInfo",
duration: Date.now() - authStart,
sid: res.sid,
socketId: localParams.socket?.id,
});
return res;
}

Expand Down
7 changes: 7 additions & 0 deletions lib/Logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export namespace EventTypes {
args: any[];
localParams: LocalParams;
};
export type Auth = ClientInfo & DebugInfo & {
type: "auth";
} & (
| { command: "getClientInfo"; }
| { command: "login"; }
);

export type Debug = DebugInfo & {
type: "debug";
Expand All @@ -108,6 +114,7 @@ export namespace EventTypes {
}

export type EventInfo =
| EventTypes.Auth
| EventTypes.Table
| EventTypes.Method
| EventTypes.Sync
Expand Down
8 changes: 3 additions & 5 deletions lib/Prostgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*--------------------------------------------------------------------------------------------*/

import * as pgPromise from 'pg-promise';
import AuthHandler, { Auth, AuthRequestParams, SessionUser } from "./AuthHandler";
import { Auth, AuthHandler, AuthRequestParams, SessionUser } from "./AuthHandler";
import { EventTriggerTagFilter } from "./Event_Trigger_Tags";
import { CloudClient, FileManager, ImageOptions, LocalConfig } from "./FileManager/FileManager";
import { SchemaWatch } from "./SchemaWatch/SchemaWatch";
import { DbConnection, DbConnectionOpts, OnInitReason, OnReadyCallback, OnReadyCallbackBasic, initProstgles } from "./initProstgles";
import { DbConnection, DbConnectionOpts, OnInitReason, OnReadyCallback, initProstgles } from "./initProstgles";
import { clientCanRunSqlRequest, runClientMethod, runClientRequest, runClientSqlRequest } from "./runClientRequest";
import pg = require('pg-promise/typescript/pg-subset');
const { version } = require('../package.json');
Expand All @@ -17,7 +17,6 @@ import { ExpressApp, RestApi, RestApiConfig } from "./RestApi";
import TableConfigurator, { TableConfig } from "./TableConfig/TableConfig";

import { DBHandlerServer, DboBuilder, LocalParams, PRGLIOSocket, getErrorAsObject } from "./DboBuilder/DboBuilder";
import { PubSubManager, log } from "./PubSubManager/PubSubManager";
export { DBHandlerServer };
export type PGP = pgPromise.IMain<{}, pg.IClient>;

Expand All @@ -30,6 +29,7 @@ import {
getKeys,
isObject, omitKeys, tryCatch
} from "prostgles-types";
import type { Server } from "socket.io";
import { DBEventsManager } from "./DBEventsManager";
import { Publish, PublishMethods, PublishParams, PublishParser } from "./PublishParser/PublishParser";

Expand All @@ -46,7 +46,6 @@ export type Join = {
type: typeof JOIN_TYPES[number];
};
export type Joins = Join[] | "inferred";
import type { Server } from "socket.io";


type Keywords = {
Expand Down Expand Up @@ -626,7 +625,6 @@ export class Prostgles {
if(!clientInfo) throw "Invalid client";
if(!this.authHandler) throw "this.authHandler missing";
const userData = await this.authHandler.getClientInfo(clientInfo);

const { publishParser } = this;
let fullSchema: {
schema: TableSchemaForClient;
Expand Down
2 changes: 1 addition & 1 deletion lib/initProstgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as promise from "bluebird";
import * as pgPromise from "pg-promise";
import pg from "pg-promise/typescript/pg-subset";
import { isEmpty, pickKeys } from "prostgles-types";
import AuthHandler from "./AuthHandler";
import { AuthHandler } from "./AuthHandler";
import { DBEventsManager } from "./DBEventsManager";
import { DBOFullyTyped } from "./DBSchemaBuilder";
import { DBHandlerServer, Prostgles, ProstglesInitOptions, getIsSuperUser } from "./Prostgles";
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.60",
"version": "4.2.61",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e319cb

Please sign in to comment.