prostgles-server 4.2.270 → 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 +15 -8
- package/dist/Auth/AuthHandler.d.ts.map +1 -1
- package/dist/Auth/AuthHandler.js +34 -58
- package/dist/Auth/AuthHandler.js.map +1 -1
- package/dist/Auth/AuthTypes.d.ts +12 -2
- 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 +26 -19
- 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 +12 -4
- 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 +4 -2
- package/dist/Auth/utils/getSidAndUserFromRequest.d.ts.map +1 -1
- package/dist/Auth/utils/getSidAndUserFromRequest.js +40 -25
- package/dist/Auth/utils/getSidAndUserFromRequest.js.map +1 -1
- package/dist/Auth/utils/getUserOrError.d.ts +10 -0
- package/dist/Auth/utils/getUserOrError.d.ts.map +1 -0
- package/dist/Auth/utils/getUserOrError.js +48 -0
- package/dist/Auth/utils/getUserOrError.js.map +1 -0
- package/dist/Auth/utils/handleGetUser.d.ts +7 -0
- package/dist/Auth/utils/handleGetUser.d.ts.map +1 -0
- package/dist/Auth/utils/handleGetUser.js +47 -0
- package/dist/Auth/utils/handleGetUser.js.map +1 -0
- package/dist/DboBuilder/DboBuilderTypes.d.ts +2 -1
- package/dist/DboBuilder/DboBuilderTypes.d.ts.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 +11 -5
- package/dist/PublishParser/PublishParser.js.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.js +70 -69
- package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
- package/dist/onSocketConnected.d.ts.map +1 -1
- package/dist/onSocketConnected.js +15 -14
- 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 +42 -83
- package/lib/Auth/AuthTypes.ts +13 -3
- package/lib/Auth/endpoints/setCatchAllRequestHandler.ts +29 -24
- package/lib/Auth/getClientAuth.ts +28 -14
- package/lib/Auth/login.ts +24 -17
- package/lib/Auth/setupAuthRoutes.ts +12 -3
- package/lib/Auth/utils/getSidAndUserFromRequest.ts +42 -33
- package/lib/Auth/utils/getUserOrError.ts +56 -0
- package/lib/Auth/utils/handleGetUser.ts +63 -0
- package/lib/DboBuilder/DboBuilderTypes.ts +2 -1
- package/lib/Prostgles.ts +7 -0
- package/lib/PublishParser/PublishParser.ts +14 -6
- package/lib/PublishParser/getSchemaFromPublish.ts +97 -94
- package/lib/onSocketConnected.ts +15 -12
- package/lib/runClientRequest.ts +3 -0
- package/package.json +2 -2
|
@@ -5,25 +5,43 @@ import {
|
|
|
5
5
|
CHANNELS,
|
|
6
6
|
getObjectEntries,
|
|
7
7
|
isEmpty,
|
|
8
|
+
isObject,
|
|
8
9
|
} from "prostgles-types";
|
|
9
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
AuthClientRequest,
|
|
12
|
+
LoginWithOAuthConfig,
|
|
13
|
+
AuthResultWithSID,
|
|
14
|
+
type AuthResult,
|
|
15
|
+
} from "./AuthTypes";
|
|
10
16
|
import { AUTH_ROUTES_AND_PARAMS, AuthHandler } from "./AuthHandler";
|
|
11
17
|
|
|
12
18
|
export async function getClientAuth(
|
|
13
19
|
this: AuthHandler,
|
|
14
20
|
clientReq: AuthClientRequest
|
|
15
|
-
): Promise<{ auth: AuthSocketSchema; userData: AuthResultWithSID }> {
|
|
21
|
+
): Promise<{ auth: AuthSocketSchema; userData: AuthResultWithSID } | "new-session-redirect"> {
|
|
16
22
|
let pathGuard = false;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
const {
|
|
24
|
+
loginWithOAuth,
|
|
25
|
+
signupWithEmail: signupWithEmailAndPassword,
|
|
26
|
+
localLoginMode,
|
|
27
|
+
login,
|
|
28
|
+
publicRoutes,
|
|
29
|
+
disableSocketAuthGuard,
|
|
30
|
+
} = this.opts.loginSignupConfig ?? {};
|
|
31
|
+
|
|
32
|
+
if (publicRoutes && !disableSocketAuthGuard) {
|
|
21
33
|
pathGuard = true;
|
|
22
34
|
|
|
23
35
|
/**
|
|
24
36
|
* Due to SPA nature of some clients, we need to check if the connected client ends up on a protected route
|
|
25
37
|
*/
|
|
26
38
|
if (clientReq.socket) {
|
|
39
|
+
const getUserFromRequest = async (clientReq: AuthClientRequest): Promise<AuthResult> => {
|
|
40
|
+
const sidAndUser = await this.getSidAndUserFromRequest(clientReq);
|
|
41
|
+
if (isObject(sidAndUser) && sidAndUser.sid && sidAndUser.user) {
|
|
42
|
+
return sidAndUser;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
27
45
|
const { socket } = clientReq;
|
|
28
46
|
socket.removeAllListeners(CHANNELS.AUTHGUARD);
|
|
29
47
|
socket.on(
|
|
@@ -48,7 +66,7 @@ export async function getClientAuth(
|
|
|
48
66
|
pathname &&
|
|
49
67
|
typeof pathname === "string" &&
|
|
50
68
|
this.isUserRoute(pathname) &&
|
|
51
|
-
!(await
|
|
69
|
+
!(await getUserFromRequest({ socket }))
|
|
52
70
|
) {
|
|
53
71
|
cb(null, { shouldReload: true });
|
|
54
72
|
} else {
|
|
@@ -64,13 +82,9 @@ export async function getClientAuth(
|
|
|
64
82
|
}
|
|
65
83
|
|
|
66
84
|
const userData = await this.getSidAndUserFromRequest(clientReq);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
localLoginMode,
|
|
71
|
-
login,
|
|
72
|
-
} = this.opts.loginSignupConfig ?? {};
|
|
73
|
-
|
|
85
|
+
if (userData === "new-session-redirect") {
|
|
86
|
+
return userData;
|
|
87
|
+
}
|
|
74
88
|
const auth: AuthSocketSchema = {
|
|
75
89
|
providers: getOAuthProviders(loginWithOAuth),
|
|
76
90
|
signupWithEmailAndPassword: signupWithEmailAndPassword && {
|
package/lib/Auth/login.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DBOFullyTyped } from "../DBSchemaBuilder";
|
|
2
2
|
import { AuthHandler, getClientRequestIPsInfo, HTTP_FAIL_CODES } from "./AuthHandler";
|
|
3
|
-
import { ExpressReq, LoginParams } from "./AuthTypes";
|
|
3
|
+
import { ExpressReq, LoginParams, type BasicSession } from "./AuthTypes";
|
|
4
4
|
import { LoginResponseHandler } from "./endpoints/setLoginRequestHandler";
|
|
5
5
|
import { throttledAuthCall } from "./utils/throttledReject";
|
|
6
6
|
|
|
@@ -30,22 +30,9 @@ export async function login(
|
|
|
30
30
|
return result;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
const
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
return "server-error";
|
|
37
|
-
}
|
|
38
|
-
if (sid && (typeof sid !== "string" || typeof expires !== "number")) {
|
|
39
|
-
console.error(
|
|
40
|
-
"Bad login result type. \nExpecting: undefined | null | { sid: string; expires: number }"
|
|
41
|
-
);
|
|
42
|
-
return "server-error";
|
|
43
|
-
}
|
|
44
|
-
if (expires < Date.now()) {
|
|
45
|
-
console.error(
|
|
46
|
-
"auth.login() is returning an expired session. Can only login with a session.expires greater than Date.now()"
|
|
47
|
-
);
|
|
48
|
-
return "server-error";
|
|
33
|
+
const sessionErrorCode = getBasicSessionErrorCode(result.session);
|
|
34
|
+
if (sessionErrorCode) {
|
|
35
|
+
return sessionErrorCode;
|
|
49
36
|
}
|
|
50
37
|
|
|
51
38
|
return result;
|
|
@@ -76,3 +63,23 @@ export async function login(
|
|
|
76
63
|
}
|
|
77
64
|
this.setCookieAndGoToReturnURLIFSet(loginResponse.session, { req, res });
|
|
78
65
|
}
|
|
66
|
+
|
|
67
|
+
export const getBasicSessionErrorCode = (session: Pick<BasicSession, "expires" | "sid">) => {
|
|
68
|
+
const { sid, expires } = session;
|
|
69
|
+
if (!sid) {
|
|
70
|
+
console.error("Invalid sid");
|
|
71
|
+
return "server-error";
|
|
72
|
+
}
|
|
73
|
+
if (sid && (typeof sid !== "string" || typeof expires !== "number")) {
|
|
74
|
+
console.error(
|
|
75
|
+
"Bad login result type. \nExpecting: undefined | null | { sid: string; expires: number }"
|
|
76
|
+
);
|
|
77
|
+
return "server-error";
|
|
78
|
+
}
|
|
79
|
+
if (expires < Date.now()) {
|
|
80
|
+
console.error(
|
|
81
|
+
"auth.login() is returning an expired session. Can only login with a session.expires greater than Date.now()"
|
|
82
|
+
);
|
|
83
|
+
return "server-error";
|
|
84
|
+
}
|
|
85
|
+
};
|
|
@@ -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,22 +1,25 @@
|
|
|
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.
|
|
8
10
|
* Use socket session cache if configured in Auth
|
|
11
|
+
* Used in Publish Parser and AuthHandler
|
|
9
12
|
*/
|
|
10
13
|
export async function getSidAndUserFromRequest(
|
|
11
14
|
this: AuthHandler,
|
|
12
15
|
clientReq: AuthClientRequest
|
|
13
|
-
): Promise<
|
|
16
|
+
): Promise<GetUserOrRedirected> {
|
|
14
17
|
/**
|
|
15
18
|
* Get cached session if available
|
|
16
19
|
*/
|
|
17
20
|
const getSessionForCaching = this.opts.cacheSession?.getSession;
|
|
18
21
|
if (clientReq.socket && getSessionForCaching && clientReq.socket.__prglCache) {
|
|
19
|
-
const { session,
|
|
22
|
+
const { session, userData } = clientReq.socket.__prglCache;
|
|
20
23
|
const isValid = this.isNonExpiredSocketSession(clientReq.socket, session);
|
|
21
24
|
if (isValid) {
|
|
22
25
|
return {
|
|
@@ -33,45 +36,51 @@ export async function getSidAndUserFromRequest(
|
|
|
33
36
|
* Get sid from request and fetch user data
|
|
34
37
|
*/
|
|
35
38
|
const authStart = Date.now();
|
|
36
|
-
const result = await throttledAuthCall(async () => {
|
|
37
|
-
|
|
39
|
+
// const result = await throttledAuthCall(async () => {
|
|
40
|
+
// const clientInfoOrErr = await this.opts.getUser(
|
|
41
|
+
// this.getValidatedSid(clientReq),
|
|
42
|
+
// this.dbo as DBOFullyTyped,
|
|
43
|
+
// this.db,
|
|
44
|
+
// getClientRequestIPsInfo(clientReq),
|
|
45
|
+
// clientReq
|
|
46
|
+
// );
|
|
47
|
+
// if (clientInfoOrErr && (typeof clientInfoOrErr === "string" || "success" in clientInfoOrErr))
|
|
48
|
+
// throw clientInfoOrErr;
|
|
49
|
+
// const clientInfo = clientInfoOrErr;
|
|
38
50
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.db,
|
|
46
|
-
getClientRequestIPsInfo(clientReq),
|
|
47
|
-
clientReq
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
if (clientInfoOrErr && (typeof clientInfoOrErr === "string" || "success" in clientInfoOrErr))
|
|
51
|
-
throw clientInfoOrErr;
|
|
52
|
-
const clientInfo = clientInfoOrErr;
|
|
53
|
-
if (getSessionForCaching && clientReq.socket && sid) {
|
|
54
|
-
const session = await getSessionForCaching(sid, this.dbo as DBOFullyTyped, this.db);
|
|
55
|
-
if (session && session.expires && clientInfo?.user) {
|
|
56
|
-
clientReq.socket.__prglCache = {
|
|
57
|
-
...clientInfo,
|
|
58
|
-
session,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
}
|
|
51
|
+
// if (clientInfo && "type" in clientInfo) {
|
|
52
|
+
// if (!("httpReq" in clientReq) || !clientReq.httpReq) throw "httpReq missing";
|
|
53
|
+
// const { httpReq, res } = clientReq;
|
|
54
|
+
// this.setCookieAndGoToReturnURLIFSet(clientInfo.session, { req: httpReq, res });
|
|
55
|
+
// return;
|
|
56
|
+
// }
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
// const sid = this.getValidatedSid(clientReq);
|
|
59
|
+
// if (getSessionForCaching && clientReq.socket && sid) {
|
|
60
|
+
// const session = await getSessionForCaching(sid, this.dbo as DBOFullyTyped, this.db);
|
|
61
|
+
// if (session && session.expires && clientInfo?.user) {
|
|
62
|
+
// clientReq.socket.__prglCache = {
|
|
63
|
+
// userData: clientInfo,
|
|
64
|
+
// session,
|
|
65
|
+
// };
|
|
66
|
+
// }
|
|
67
|
+
// }
|
|
66
68
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
// if (clientInfo?.user && sid) {
|
|
70
|
+
// return { sid, ...clientInfo };
|
|
71
|
+
// }
|
|
69
72
|
|
|
73
|
+
// return { sid, preferredLogin: !clientInfo?.user ? clientInfo?.preferredLogin : undefined };
|
|
74
|
+
// }, 100);
|
|
75
|
+
const result = await this.handleGetUser(clientReq);
|
|
76
|
+
if (isObject(result) && result.error) {
|
|
77
|
+
throw result.error;
|
|
78
|
+
}
|
|
70
79
|
await this.prostgles.opts.onLog?.({
|
|
71
80
|
type: "auth",
|
|
72
81
|
command: "getClientInfo",
|
|
73
82
|
duration: Date.now() - authStart,
|
|
74
|
-
sid: result.sid,
|
|
83
|
+
sid: isObject(result) ? result.sid : undefined,
|
|
75
84
|
socketId: clientReq.socket?.id,
|
|
76
85
|
});
|
|
77
86
|
return result;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AuthResponse } from "prostgles-types";
|
|
2
|
+
import { DBOFullyTyped } from "../../DBSchemaBuilder";
|
|
3
|
+
import type { AuthHandler } from "../AuthHandler";
|
|
4
|
+
import { AuthClientRequest, AuthResultWithSID } from "../AuthTypes";
|
|
5
|
+
import { getClientRequestIPsInfo } from "../utils/getClientRequestIPsInfo";
|
|
6
|
+
import { isAuthError, type GetUserOrRedirected } from "./handleGetUser";
|
|
7
|
+
import { throttledAuthCall } from "./throttledReject";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Used by:
|
|
11
|
+
* - setCatchAllRequestHandler
|
|
12
|
+
* - loginSignupConfig.use
|
|
13
|
+
*/
|
|
14
|
+
export async function getUserOrError(
|
|
15
|
+
this: AuthHandler,
|
|
16
|
+
clientReq: AuthClientRequest
|
|
17
|
+
): Promise<GetUserOrRedirected> {
|
|
18
|
+
// const sid = this.getValidatedSid(clientReq);
|
|
19
|
+
// if (!sid) return { sid };
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
// const userOrErrorCode = await throttledAuthCall(async () => {
|
|
23
|
+
// return this.opts.getUser(
|
|
24
|
+
// this.validateSid(sid),
|
|
25
|
+
// this.dbo as DBOFullyTyped,
|
|
26
|
+
// this.db,
|
|
27
|
+
// getClientRequestIPsInfo(clientReq),
|
|
28
|
+
// clientReq
|
|
29
|
+
// );
|
|
30
|
+
// }, 50);
|
|
31
|
+
|
|
32
|
+
// if (isAuthError(userOrErrorCode)) {
|
|
33
|
+
// const error: AuthResponse.AuthFailure | undefined =
|
|
34
|
+
// typeof userOrErrorCode === "string" ?
|
|
35
|
+
// { success: false, code: userOrErrorCode }
|
|
36
|
+
// : userOrErrorCode;
|
|
37
|
+
|
|
38
|
+
// return {
|
|
39
|
+
// sid,
|
|
40
|
+
// error,
|
|
41
|
+
// };
|
|
42
|
+
// }
|
|
43
|
+
// if (sid && userOrErrorCode?.user) {
|
|
44
|
+
// return { sid, ...userOrErrorCode };
|
|
45
|
+
// }
|
|
46
|
+
// return {
|
|
47
|
+
// sid,
|
|
48
|
+
// };
|
|
49
|
+
return this.handleGetUser(clientReq);
|
|
50
|
+
} catch (_err) {
|
|
51
|
+
return {
|
|
52
|
+
sid: this.getValidatedSid(clientReq),
|
|
53
|
+
error: { success: false, code: "server-error" },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { AuthResponse, isObject } from "prostgles-types";
|
|
2
|
+
import type { DBOFullyTyped } from "../../DBSchemaBuilder";
|
|
3
|
+
import { getClientRequestIPsInfo, type AuthHandler } from "../AuthHandler";
|
|
4
|
+
import type { AuthClientRequest, AuthResultOrError, AuthResultWithSID } from "../AuthTypes";
|
|
5
|
+
import { throttledAuthCall } from "./throttledReject";
|
|
6
|
+
|
|
7
|
+
export type GetUserOrRedirected = AuthResultWithSID | "new-session-redirect";
|
|
8
|
+
|
|
9
|
+
export async function handleGetUserThrottled(
|
|
10
|
+
this: AuthHandler,
|
|
11
|
+
clientReq: AuthClientRequest
|
|
12
|
+
): Promise<GetUserOrRedirected> {
|
|
13
|
+
const getSessionForCaching = this.opts.cacheSession?.getSession;
|
|
14
|
+
const result = await throttledAuthCall(async () => {
|
|
15
|
+
const clientInfoOrErr = await this.opts.getUser(
|
|
16
|
+
this.getValidatedSid(clientReq),
|
|
17
|
+
this.dbo as DBOFullyTyped,
|
|
18
|
+
this.db,
|
|
19
|
+
getClientRequestIPsInfo(clientReq),
|
|
20
|
+
clientReq
|
|
21
|
+
);
|
|
22
|
+
if (isAuthError(clientInfoOrErr)) {
|
|
23
|
+
return {
|
|
24
|
+
error:
|
|
25
|
+
isObject(clientInfoOrErr) ? clientInfoOrErr : { success: false, code: clientInfoOrErr },
|
|
26
|
+
sid: this.getValidatedSid(clientReq),
|
|
27
|
+
} satisfies AuthResultWithSID;
|
|
28
|
+
}
|
|
29
|
+
const clientInfo = clientInfoOrErr;
|
|
30
|
+
|
|
31
|
+
if (clientInfo && "type" in clientInfo) {
|
|
32
|
+
if (!("httpReq" in clientReq) || !clientReq.httpReq)
|
|
33
|
+
throw "httpReq missing. new-session not implemented for sockets.";
|
|
34
|
+
const { httpReq, res } = clientReq;
|
|
35
|
+
this.validateSessionAndSetCookie(clientInfo.session, { req: httpReq, res });
|
|
36
|
+
return "new-session-redirect" as const;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sid = this.getValidatedSid(clientReq);
|
|
40
|
+
if (getSessionForCaching && clientReq.socket && sid) {
|
|
41
|
+
const session = await getSessionForCaching(sid, this.dbo as DBOFullyTyped, this.db);
|
|
42
|
+
if (session && session.expires && clientInfo?.user) {
|
|
43
|
+
clientReq.socket.__prglCache = {
|
|
44
|
+
userData: clientInfo,
|
|
45
|
+
session,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (clientInfo?.user && sid) {
|
|
51
|
+
return { sid, ...clientInfo };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { sid, preferredLogin: !clientInfo?.user ? clientInfo?.preferredLogin : undefined };
|
|
55
|
+
}, 100);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const isAuthError = (
|
|
60
|
+
dataOrError: AuthResultOrError
|
|
61
|
+
): dataOrError is AuthResponse.AuthFailure["code"] | AuthResponse.AuthFailure => {
|
|
62
|
+
return Boolean(typeof dataOrError === "string" || (dataOrError && "success" in dataOrError));
|
|
63
|
+
};
|
package/lib/Prostgles.ts
CHANGED
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
CHANNELS,
|
|
33
33
|
ClientSchema,
|
|
34
34
|
SQLRequest,
|
|
35
|
+
includes,
|
|
35
36
|
isObject,
|
|
36
37
|
omitKeys,
|
|
37
38
|
tryCatchV2,
|
|
@@ -366,6 +367,9 @@ export class Prostgles {
|
|
|
366
367
|
: { type: "http" as const, ...clientReq };
|
|
367
368
|
|
|
368
369
|
const userData = await this.authHandler?.getSidAndUserFromRequest(clientInfo);
|
|
370
|
+
if (userData === "new-session-redirect") {
|
|
371
|
+
throw "new-session-redirect";
|
|
372
|
+
}
|
|
369
373
|
const { publishParser } = this;
|
|
370
374
|
let fullSchema: Awaited<ReturnType<PublishParser["getSchemaFromPublish"]>> | undefined;
|
|
371
375
|
let publishValidationError;
|
|
@@ -426,6 +430,9 @@ export class Prostgles {
|
|
|
426
430
|
});
|
|
427
431
|
|
|
428
432
|
const authInfo = await this.authHandler?.getClientAuth(clientReq);
|
|
433
|
+
if (authInfo === "new-session-redirect") {
|
|
434
|
+
throw "new-session-redirect";
|
|
435
|
+
}
|
|
429
436
|
|
|
430
437
|
const clientSchema: ClientSchema = {
|
|
431
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
|
}
|
|
@@ -159,13 +166,14 @@ export class PublishParser {
|
|
|
159
166
|
}
|
|
160
167
|
}
|
|
161
168
|
|
|
162
|
-
if (tableRule[rtm.rule]) {
|
|
163
|
-
return tableRule;
|
|
164
|
-
} else
|
|
169
|
+
if (!tableRule[rtm.rule]) {
|
|
165
170
|
throw {
|
|
166
171
|
stack: ["getValidatedRequestRule()"],
|
|
167
172
|
message: `Invalid or disallowed command: ${tableName}.${command}`,
|
|
168
173
|
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return tableRule;
|
|
169
177
|
}
|
|
170
178
|
|
|
171
179
|
async getTableRules(
|