prostgles-server 4.2.271 → 4.2.272
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Auth/AuthHandler.d.ts +12 -6
- package/dist/Auth/AuthHandler.d.ts.map +1 -1
- package/dist/Auth/AuthHandler.js +12 -6
- package/dist/Auth/AuthHandler.js.map +1 -1
- package/dist/Auth/AuthTypes.d.ts +5 -1
- package/dist/Auth/AuthTypes.d.ts.map +1 -1
- package/dist/Auth/AuthTypes.js.map +1 -1
- package/dist/Auth/endpoints/setCatchAllRequestHandler.d.ts.map +1 -1
- package/dist/Auth/endpoints/setCatchAllRequestHandler.js +24 -17
- package/dist/Auth/endpoints/setCatchAllRequestHandler.js.map +1 -1
- package/dist/Auth/getClientAuth.d.ts +1 -1
- package/dist/Auth/getClientAuth.d.ts.map +1 -1
- package/dist/Auth/getClientAuth.js +10 -1
- package/dist/Auth/getClientAuth.js.map +1 -1
- package/dist/Auth/login.d.ts +2 -1
- package/dist/Auth/login.d.ts.map +1 -1
- package/dist/Auth/login.js +20 -13
- package/dist/Auth/login.js.map +1 -1
- package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
- package/dist/Auth/setupAuthRoutes.js +11 -3
- package/dist/Auth/setupAuthRoutes.js.map +1 -1
- package/dist/Auth/utils/getSidAndUserFromRequest.d.ts +3 -2
- package/dist/Auth/utils/getSidAndUserFromRequest.d.ts.map +1 -1
- package/dist/Auth/utils/getSidAndUserFromRequest.js +3 -2
- package/dist/Auth/utils/getSidAndUserFromRequest.js.map +1 -1
- package/dist/Auth/utils/getUserOrError.d.ts +3 -2
- package/dist/Auth/utils/getUserOrError.d.ts.map +1 -1
- package/dist/Auth/utils/handleGetUser.d.ts +3 -2
- package/dist/Auth/utils/handleGetUser.d.ts.map +1 -1
- package/dist/Auth/utils/handleGetUser.js +3 -9
- package/dist/Auth/utils/handleGetUser.js.map +1 -1
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/Prostgles.js +6 -0
- package/dist/Prostgles.js.map +1 -1
- package/dist/PublishParser/PublishParser.d.ts +2 -2
- package/dist/PublishParser/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser/PublishParser.js +8 -1
- package/dist/PublishParser/PublishParser.js.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.js +3 -0
- package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
- package/dist/onSocketConnected.d.ts.map +1 -1
- package/dist/onSocketConnected.js +12 -11
- package/dist/onSocketConnected.js.map +1 -1
- package/dist/runClientRequest.d.ts.map +1 -1
- package/dist/runClientRequest.js +3 -0
- package/dist/runClientRequest.js.map +1 -1
- package/lib/Auth/AuthHandler.ts +17 -7
- package/lib/Auth/AuthTypes.ts +4 -2
- package/lib/Auth/endpoints/setCatchAllRequestHandler.ts +27 -22
- package/lib/Auth/getClientAuth.ts +18 -4
- package/lib/Auth/login.ts +24 -17
- package/lib/Auth/setupAuthRoutes.ts +12 -3
- package/lib/Auth/utils/getSidAndUserFromRequest.ts +5 -3
- package/lib/Auth/utils/getUserOrError.ts +2 -2
- package/lib/Auth/utils/handleGetUser.ts +6 -10
- package/lib/Prostgles.ts +6 -0
- package/lib/PublishParser/PublishParser.ts +10 -3
- package/lib/PublishParser/getSchemaFromPublish.ts +3 -1
- package/lib/onSocketConnected.ts +12 -9
- package/lib/runClientRequest.ts +3 -0
- package/package.json +1 -1
|
@@ -42,17 +42,23 @@ export function setupAuthRoutes(this: AuthHandler) {
|
|
|
42
42
|
if (onUseOrSocketConnected) {
|
|
43
43
|
const prostglesUseMiddleware: RequestHandler = async (req, res, next) => {
|
|
44
44
|
const reqInfo = { httpReq: req, res };
|
|
45
|
-
const
|
|
45
|
+
const errorInfoOrSession = await onUseOrSocketConnected(
|
|
46
46
|
this.getSIDNoError(reqInfo),
|
|
47
47
|
getClientRequestIPsInfo(reqInfo),
|
|
48
48
|
reqInfo
|
|
49
49
|
);
|
|
50
50
|
|
|
51
|
-
if (
|
|
52
|
-
const { error, httpCode } =
|
|
51
|
+
if (errorInfoOrSession && "error" in errorInfoOrSession) {
|
|
52
|
+
const { error, httpCode } = errorInfoOrSession;
|
|
53
53
|
res.status(httpCode).json({ error });
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
|
+
|
|
57
|
+
if (errorInfoOrSession && "session" in errorInfoOrSession) {
|
|
58
|
+
const { session } = errorInfoOrSession;
|
|
59
|
+
this.validateSessionAndSetCookie(session, { req, res });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
56
62
|
next();
|
|
57
63
|
};
|
|
58
64
|
upsertNamedExpressMiddleware(app, prostglesUseMiddleware, "prostglesonUseOrSocketConnected");
|
|
@@ -66,6 +72,9 @@ export function setupAuthRoutes(this: AuthHandler) {
|
|
|
66
72
|
next,
|
|
67
73
|
getUser: async () => {
|
|
68
74
|
const userOrErr = await this.getUserOrError({ httpReq: req, res });
|
|
75
|
+
if (userOrErr === "new-session-redirect") {
|
|
76
|
+
throw "new-session-redirect";
|
|
77
|
+
}
|
|
69
78
|
if (userOrErr.error) {
|
|
70
79
|
res.status(HTTP_FAIL_CODES.BAD_REQUEST).json(userOrErr.error);
|
|
71
80
|
throw userOrErr.error;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { isObject } from "prostgles-types";
|
|
1
2
|
import { DBOFullyTyped } from "../../DBSchemaBuilder";
|
|
2
3
|
import { AuthHandler, getClientRequestIPsInfo } from "../AuthHandler";
|
|
3
4
|
import { AuthClientRequest, AuthResultWithSID } from "../AuthTypes";
|
|
4
5
|
import { throttledAuthCall } from "./throttledReject";
|
|
6
|
+
import type { GetUserOrRedirected } from "./handleGetUser";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* For a given sid return the user data if available using the auth handler's getUser method.
|
|
@@ -11,7 +13,7 @@ import { throttledAuthCall } from "./throttledReject";
|
|
|
11
13
|
export async function getSidAndUserFromRequest(
|
|
12
14
|
this: AuthHandler,
|
|
13
15
|
clientReq: AuthClientRequest
|
|
14
|
-
): Promise<
|
|
16
|
+
): Promise<GetUserOrRedirected> {
|
|
15
17
|
/**
|
|
16
18
|
* Get cached session if available
|
|
17
19
|
*/
|
|
@@ -71,14 +73,14 @@ export async function getSidAndUserFromRequest(
|
|
|
71
73
|
// return { sid, preferredLogin: !clientInfo?.user ? clientInfo?.preferredLogin : undefined };
|
|
72
74
|
// }, 100);
|
|
73
75
|
const result = await this.handleGetUser(clientReq);
|
|
74
|
-
if (result.error) {
|
|
76
|
+
if (isObject(result) && result.error) {
|
|
75
77
|
throw result.error;
|
|
76
78
|
}
|
|
77
79
|
await this.prostgles.opts.onLog?.({
|
|
78
80
|
type: "auth",
|
|
79
81
|
command: "getClientInfo",
|
|
80
82
|
duration: Date.now() - authStart,
|
|
81
|
-
sid: result.sid,
|
|
83
|
+
sid: isObject(result) ? result.sid : undefined,
|
|
82
84
|
socketId: clientReq.socket?.id,
|
|
83
85
|
});
|
|
84
86
|
return result;
|
|
@@ -3,7 +3,7 @@ import { DBOFullyTyped } from "../../DBSchemaBuilder";
|
|
|
3
3
|
import type { AuthHandler } from "../AuthHandler";
|
|
4
4
|
import { AuthClientRequest, AuthResultWithSID } from "../AuthTypes";
|
|
5
5
|
import { getClientRequestIPsInfo } from "../utils/getClientRequestIPsInfo";
|
|
6
|
-
import { isAuthError } from "./handleGetUser";
|
|
6
|
+
import { isAuthError, type GetUserOrRedirected } from "./handleGetUser";
|
|
7
7
|
import { throttledAuthCall } from "./throttledReject";
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -14,7 +14,7 @@ import { throttledAuthCall } from "./throttledReject";
|
|
|
14
14
|
export async function getUserOrError(
|
|
15
15
|
this: AuthHandler,
|
|
16
16
|
clientReq: AuthClientRequest
|
|
17
|
-
): Promise<
|
|
17
|
+
): Promise<GetUserOrRedirected> {
|
|
18
18
|
// const sid = this.getValidatedSid(clientReq);
|
|
19
19
|
// if (!sid) return { sid };
|
|
20
20
|
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
import { AuthResponse, isObject } from "prostgles-types";
|
|
1
2
|
import type { DBOFullyTyped } from "../../DBSchemaBuilder";
|
|
2
|
-
import { tout } from "../../PubSubManager/initPubSubManager";
|
|
3
3
|
import { getClientRequestIPsInfo, type AuthHandler } from "../AuthHandler";
|
|
4
4
|
import type { AuthClientRequest, AuthResultOrError, AuthResultWithSID } from "../AuthTypes";
|
|
5
5
|
import { throttledAuthCall } from "./throttledReject";
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
export type GetUserOrRedirected = AuthResultWithSID | "new-session-redirect";
|
|
7
8
|
|
|
8
9
|
export async function handleGetUserThrottled(
|
|
9
10
|
this: AuthHandler,
|
|
10
11
|
clientReq: AuthClientRequest
|
|
11
|
-
): Promise<
|
|
12
|
+
): Promise<GetUserOrRedirected> {
|
|
12
13
|
const getSessionForCaching = this.opts.cacheSession?.getSession;
|
|
13
14
|
const result = await throttledAuthCall(async () => {
|
|
14
15
|
const clientInfoOrErr = await this.opts.getUser(
|
|
@@ -31,13 +32,8 @@ export async function handleGetUserThrottled(
|
|
|
31
32
|
if (!("httpReq" in clientReq) || !clientReq.httpReq)
|
|
32
33
|
throw "httpReq missing. new-session not implemented for sockets.";
|
|
33
34
|
const { httpReq, res } = clientReq;
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
await tout(200);
|
|
37
|
-
return {
|
|
38
|
-
error: { success: false, code: "something-went-wrong" },
|
|
39
|
-
sid: this.getValidatedSid(clientReq),
|
|
40
|
-
} satisfies AuthResultWithSID;
|
|
35
|
+
this.validateSessionAndSetCookie(clientInfo.session, { req: httpReq, res });
|
|
36
|
+
return "new-session-redirect" as const;
|
|
41
37
|
}
|
|
42
38
|
|
|
43
39
|
const sid = this.getValidatedSid(clientReq);
|
package/lib/Prostgles.ts
CHANGED
|
@@ -367,6 +367,9 @@ export class Prostgles {
|
|
|
367
367
|
: { type: "http" as const, ...clientReq };
|
|
368
368
|
|
|
369
369
|
const userData = await this.authHandler?.getSidAndUserFromRequest(clientInfo);
|
|
370
|
+
if (userData === "new-session-redirect") {
|
|
371
|
+
throw "new-session-redirect";
|
|
372
|
+
}
|
|
370
373
|
const { publishParser } = this;
|
|
371
374
|
let fullSchema: Awaited<ReturnType<PublishParser["getSchemaFromPublish"]>> | undefined;
|
|
372
375
|
let publishValidationError;
|
|
@@ -427,6 +430,9 @@ export class Prostgles {
|
|
|
427
430
|
});
|
|
428
431
|
|
|
429
432
|
const authInfo = await this.authHandler?.getClientAuth(clientReq);
|
|
433
|
+
if (authInfo === "new-session-redirect") {
|
|
434
|
+
throw "new-session-redirect";
|
|
435
|
+
}
|
|
430
436
|
|
|
431
437
|
const clientSchema: ClientSchema = {
|
|
432
438
|
schema,
|
|
@@ -10,12 +10,11 @@ import { getTableRulesWithoutFileTable } from "./getTableRulesWithoutFileTable";
|
|
|
10
10
|
import {
|
|
11
11
|
DboTable,
|
|
12
12
|
DboTableCommand,
|
|
13
|
-
|
|
13
|
+
ParsedTableRule,
|
|
14
14
|
PublishMethods,
|
|
15
15
|
type PublishObject,
|
|
16
16
|
PublishParams,
|
|
17
17
|
RULE_TO_METHODS,
|
|
18
|
-
ParsedTableRule,
|
|
19
18
|
parsePublishTableRule,
|
|
20
19
|
} from "./publishTypesAndUtils";
|
|
21
20
|
|
|
@@ -43,9 +42,14 @@ export class PublishParser {
|
|
|
43
42
|
clientReq: AuthClientRequest,
|
|
44
43
|
clientInfo: AuthResultWithSID | undefined
|
|
45
44
|
): Promise<PublishParams> {
|
|
45
|
+
const _clientInfo =
|
|
46
|
+
clientInfo ?? (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
|
|
47
|
+
if (_clientInfo === "new-session-redirect") {
|
|
48
|
+
throw "new-session-redirect";
|
|
49
|
+
}
|
|
46
50
|
return {
|
|
47
51
|
sid: undefined,
|
|
48
|
-
...
|
|
52
|
+
..._clientInfo,
|
|
49
53
|
dbo: this.dbo as DBOFullyTyped,
|
|
50
54
|
db: this.db,
|
|
51
55
|
clientReq,
|
|
@@ -109,6 +113,9 @@ export class PublishParser {
|
|
|
109
113
|
}: DboTableCommand): Promise<ParsedTableRule> {
|
|
110
114
|
const clientInfo =
|
|
111
115
|
clientReq && (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
|
|
116
|
+
if (clientInfo === "new-session-redirect") {
|
|
117
|
+
throw "new-session-redirect";
|
|
118
|
+
}
|
|
112
119
|
const rules = await this.getValidatedRequestRule({ tableName, command, clientReq }, clientInfo);
|
|
113
120
|
return rules;
|
|
114
121
|
}
|
|
@@ -38,7 +38,9 @@ export async function getSchemaFromPublish(
|
|
|
38
38
|
/* Publish tables and views based on socket */
|
|
39
39
|
const clientInfo =
|
|
40
40
|
userData ?? (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
|
|
41
|
-
|
|
41
|
+
if (clientInfo === "new-session-redirect") {
|
|
42
|
+
throw "new-session-redirect";
|
|
43
|
+
}
|
|
42
44
|
let _publish: PublishObject | undefined;
|
|
43
45
|
try {
|
|
44
46
|
_publish = await this.getPublishAsObject(clientReq, clientInfo);
|
package/lib/onSocketConnected.ts
CHANGED
|
@@ -15,6 +15,15 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
15
15
|
this.connectedSockets.push(socket);
|
|
16
16
|
|
|
17
17
|
try {
|
|
18
|
+
const getUser = async () => {
|
|
19
|
+
if (!this.authHandler) throw "authHandler missing";
|
|
20
|
+
const res = await this.authHandler.getSidAndUserFromRequest({ socket });
|
|
21
|
+
if (res === "new-session-redirect") {
|
|
22
|
+
throw "new-session-redirect";
|
|
23
|
+
}
|
|
24
|
+
return res;
|
|
25
|
+
};
|
|
26
|
+
|
|
18
27
|
await this.opts.onLog?.({
|
|
19
28
|
type: "connect",
|
|
20
29
|
sid: this.authHandler?.getValidatedSid({ socket }),
|
|
@@ -35,17 +44,15 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
35
44
|
{ socket }
|
|
36
45
|
);
|
|
37
46
|
if (errorInfo) {
|
|
38
|
-
socket.emit(CHANNELS.CONNECTION, {
|
|
47
|
+
socket.emit(CHANNELS.CONNECTION, {
|
|
48
|
+
error: "error" in errorInfo ? errorInfo.error : "New session setup",
|
|
49
|
+
});
|
|
39
50
|
socket.disconnect();
|
|
40
51
|
return;
|
|
41
52
|
}
|
|
42
53
|
}
|
|
43
54
|
if (this.opts.onSocketConnect) {
|
|
44
55
|
try {
|
|
45
|
-
const getUser = async () => {
|
|
46
|
-
if (!this.authHandler) throw "authHandler missing";
|
|
47
|
-
return await this.authHandler.getSidAndUserFromRequest({ socket });
|
|
48
|
-
};
|
|
49
56
|
await this.opts.onSocketConnect({
|
|
50
57
|
socket,
|
|
51
58
|
dbo: dbo as DBOFullyTyped,
|
|
@@ -97,10 +104,6 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
97
104
|
});
|
|
98
105
|
|
|
99
106
|
if (this.opts.onSocketDisconnect) {
|
|
100
|
-
const getUser = async () => {
|
|
101
|
-
if (!this.authHandler) throw "authHandler missing";
|
|
102
|
-
return await this.authHandler.getSidAndUserFromRequest({ socket });
|
|
103
|
-
};
|
|
104
107
|
void this.opts.onSocketDisconnect({ socket, dbo: dbo as DBOFullyTyped, db, getUser });
|
|
105
108
|
}
|
|
106
109
|
});
|
package/lib/runClientRequest.ts
CHANGED
|
@@ -77,6 +77,9 @@ export const runClientRequest = async function (
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
const clientInfo = await this.authHandler?.getSidAndUserFromRequest(clientReq);
|
|
80
|
+
if (clientInfo === "new-session-redirect") {
|
|
81
|
+
throw clientInfo;
|
|
82
|
+
}
|
|
80
83
|
const validRules = await this.publishParser.getValidatedRequestRule(
|
|
81
84
|
{ tableName, command, clientReq },
|
|
82
85
|
clientInfo
|