prostgles-server 4.2.157 → 4.2.159

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.
Files changed (159) hide show
  1. package/dist/Auth/AuthHandler.js +2 -2
  2. package/dist/Auth/AuthHandler.js.map +1 -1
  3. package/dist/Auth/AuthTypes.d.ts +4 -8
  4. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  5. package/dist/Auth/setAuthProviders.d.ts +1 -1
  6. package/dist/Auth/setAuthProviders.d.ts.map +1 -1
  7. package/dist/Auth/setAuthProviders.js +6 -7
  8. package/dist/Auth/setAuthProviders.js.map +1 -1
  9. package/dist/Auth/setEmailProvider.d.ts +1 -1
  10. package/dist/Auth/setEmailProvider.d.ts.map +1 -1
  11. package/dist/Auth/setEmailProvider.js +22 -2
  12. package/dist/Auth/setEmailProvider.js.map +1 -1
  13. package/dist/Auth/setupAuthRoutes.js +1 -1
  14. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  15. package/dist/Prostgles.d.ts +1 -0
  16. package/dist/Prostgles.d.ts.map +1 -1
  17. package/dist/Prostgles.js +6 -0
  18. package/dist/Prostgles.js.map +1 -1
  19. package/dist/initProstgles.d.ts.map +1 -1
  20. package/dist/initProstgles.js +2 -6
  21. package/dist/initProstgles.js.map +1 -1
  22. package/package.json +1 -1
  23. package/lib/Auth/AuthHandler.ts +0 -436
  24. package/lib/Auth/AuthTypes.ts +0 -285
  25. package/lib/Auth/getSafeReturnURL.ts +0 -35
  26. package/lib/Auth/sendEmail.ts +0 -83
  27. package/lib/Auth/setAuthProviders.ts +0 -129
  28. package/lib/Auth/setEmailProvider.ts +0 -63
  29. package/lib/Auth/setupAuthRoutes.ts +0 -161
  30. package/lib/DBEventsManager.ts +0 -178
  31. package/lib/DBSchemaBuilder.ts +0 -225
  32. package/lib/DboBuilder/DboBuilder.ts +0 -319
  33. package/lib/DboBuilder/DboBuilderTypes.ts +0 -361
  34. package/lib/DboBuilder/QueryBuilder/Functions.ts +0 -1153
  35. package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +0 -288
  36. package/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +0 -263
  37. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +0 -271
  38. package/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +0 -136
  39. package/lib/DboBuilder/QueryBuilder/prepareHaving.ts +0 -22
  40. package/lib/DboBuilder/QueryStreamer.ts +0 -250
  41. package/lib/DboBuilder/TableHandler/DataValidator.ts +0 -428
  42. package/lib/DboBuilder/TableHandler/TableHandler.ts +0 -205
  43. package/lib/DboBuilder/TableHandler/delete.ts +0 -115
  44. package/lib/DboBuilder/TableHandler/insert.ts +0 -183
  45. package/lib/DboBuilder/TableHandler/insertTest.ts +0 -78
  46. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +0 -62
  47. package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +0 -134
  48. package/lib/DboBuilder/TableHandler/update.ts +0 -126
  49. package/lib/DboBuilder/TableHandler/updateBatch.ts +0 -49
  50. package/lib/DboBuilder/TableHandler/updateFile.ts +0 -48
  51. package/lib/DboBuilder/TableHandler/upsert.ts +0 -34
  52. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +0 -393
  53. package/lib/DboBuilder/ViewHandler/count.ts +0 -38
  54. package/lib/DboBuilder/ViewHandler/find.ts +0 -153
  55. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +0 -73
  56. package/lib/DboBuilder/ViewHandler/getExistsFilters.ts +0 -74
  57. package/lib/DboBuilder/ViewHandler/getInfo.ts +0 -32
  58. package/lib/DboBuilder/ViewHandler/getTableJoinQuery.ts +0 -84
  59. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +0 -96
  60. package/lib/DboBuilder/ViewHandler/parseFieldFilter.ts +0 -105
  61. package/lib/DboBuilder/ViewHandler/parseJoinPath.ts +0 -208
  62. package/lib/DboBuilder/ViewHandler/prepareSortItems.ts +0 -163
  63. package/lib/DboBuilder/ViewHandler/prepareWhere.ts +0 -90
  64. package/lib/DboBuilder/ViewHandler/size.ts +0 -37
  65. package/lib/DboBuilder/ViewHandler/subscribe.ts +0 -118
  66. package/lib/DboBuilder/ViewHandler/validateViewRules.ts +0 -70
  67. package/lib/DboBuilder/dboBuilderUtils.ts +0 -222
  68. package/lib/DboBuilder/getColumns.ts +0 -114
  69. package/lib/DboBuilder/getCondition.ts +0 -201
  70. package/lib/DboBuilder/getSubscribeRelatedTables.ts +0 -190
  71. package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +0 -426
  72. package/lib/DboBuilder/insertNestedRecords.ts +0 -355
  73. package/lib/DboBuilder/parseUpdateRules.ts +0 -187
  74. package/lib/DboBuilder/prepareShortestJoinPaths.ts +0 -186
  75. package/lib/DboBuilder/runSQL.ts +0 -182
  76. package/lib/DboBuilder/runTransaction.ts +0 -50
  77. package/lib/DboBuilder/sqlErrCodeToMsg.ts +0 -254
  78. package/lib/DboBuilder/uploadFile.ts +0 -69
  79. package/lib/Event_Trigger_Tags.ts +0 -118
  80. package/lib/FileManager/FileManager.ts +0 -358
  81. package/lib/FileManager/getValidatedFileType.ts +0 -69
  82. package/lib/FileManager/initFileManager.ts +0 -187
  83. package/lib/FileManager/upload.ts +0 -62
  84. package/lib/FileManager/uploadStream.ts +0 -79
  85. package/lib/Filtering.ts +0 -463
  86. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +0 -502
  87. package/lib/JSONBValidation/validation.ts +0 -143
  88. package/lib/Logging.ts +0 -127
  89. package/lib/PostgresNotifListenManager.ts +0 -143
  90. package/lib/Prostgles.ts +0 -479
  91. package/lib/ProstglesTypes.ts +0 -196
  92. package/lib/PubSubManager/PubSubManager.ts +0 -609
  93. package/lib/PubSubManager/addSub.ts +0 -138
  94. package/lib/PubSubManager/addSync.ts +0 -141
  95. package/lib/PubSubManager/getCreatePubSubManagerError.ts +0 -72
  96. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +0 -662
  97. package/lib/PubSubManager/initPubSubManager.ts +0 -79
  98. package/lib/PubSubManager/notifListener.ts +0 -173
  99. package/lib/PubSubManager/orphanTriggerCheck.ts +0 -70
  100. package/lib/PubSubManager/pushSubData.ts +0 -55
  101. package/lib/PublishParser/PublishParser.ts +0 -162
  102. package/lib/PublishParser/getFileTableRules.ts +0 -124
  103. package/lib/PublishParser/getSchemaFromPublish.ts +0 -141
  104. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +0 -177
  105. package/lib/PublishParser/publishTypesAndUtils.ts +0 -399
  106. package/lib/RestApi.ts +0 -127
  107. package/lib/SchemaWatch/SchemaWatch.ts +0 -90
  108. package/lib/SchemaWatch/createSchemaWatchEventTrigger.ts +0 -3
  109. package/lib/SchemaWatch/getValidatedWatchSchemaType.ts +0 -45
  110. package/lib/SchemaWatch/getWatchSchemaTagList.ts +0 -27
  111. package/lib/SyncReplication.ts +0 -557
  112. package/lib/TableConfig/TableConfig.ts +0 -468
  113. package/lib/TableConfig/getColumnDefinitionQuery.ts +0 -111
  114. package/lib/TableConfig/getConstraintDefinitionQueries.ts +0 -95
  115. package/lib/TableConfig/getFutureTableSchema.ts +0 -64
  116. package/lib/TableConfig/getPGIndexes.ts +0 -53
  117. package/lib/TableConfig/getTableColumnQueries.ts +0 -129
  118. package/lib/TableConfig/initTableConfig.ts +0 -326
  119. package/lib/index.ts +0 -13
  120. package/lib/initProstgles.ts +0 -322
  121. package/lib/onSocketConnected.ts +0 -102
  122. package/lib/runClientRequest.ts +0 -129
  123. package/lib/shortestPath.ts +0 -122
  124. package/lib/typeTests/DBoGenerated.d.ts +0 -320
  125. package/lib/typeTests/dboTypeCheck.ts +0 -81
  126. package/lib/utils.ts +0 -15
  127. package/tests/client/hooks.spec.ts +0 -205
  128. package/tests/client/index.ts +0 -139
  129. package/tests/client/package-lock.json +0 -637
  130. package/tests/client/package.json +0 -26
  131. package/tests/client/renderReactHook.ts +0 -177
  132. package/tests/client/tsconfig.json +0 -15
  133. package/tests/client/useProstgles.spec.ts +0 -120
  134. package/tests/clientFileTests.spec.ts +0 -102
  135. package/tests/clientOnlyQueries.spec.ts +0 -667
  136. package/tests/clientRestApi.spec.ts +0 -82
  137. package/tests/config_test/DBoGenerated.d.ts +0 -407
  138. package/tests/config_test/index.html +0 -109
  139. package/tests/config_test/index.js +0 -86
  140. package/tests/config_test/index.js.map +0 -1
  141. package/tests/config_test/index.ts +0 -91
  142. package/tests/config_test/init.sql +0 -48
  143. package/tests/config_test/package.json +0 -29
  144. package/tests/config_test/tsconfig.json +0 -23
  145. package/tests/config_testDBoGenerated.d.ts +0 -407
  146. package/tests/isomorphicQueries.spec.ts +0 -1493
  147. package/tests/server/DBoGenerated.d.ts +0 -537
  148. package/tests/server/index.html +0 -73
  149. package/tests/server/index.ts +0 -289
  150. package/tests/server/init.sql +0 -224
  151. package/tests/server/package-lock.json +0 -2164
  152. package/tests/server/package.json +0 -25
  153. package/tests/server/publishTypeCheck.ts +0 -136
  154. package/tests/server/server.ts +0 -35
  155. package/tests/server/testPublish.ts +0 -147
  156. package/tests/server/testTableConfig.ts +0 -156
  157. package/tests/server/tsconfig.json +0 -22
  158. package/tests/serverOnlyQueries.spec.ts +0 -32
  159. package/tests/test.sh +0 -20
@@ -1,436 +0,0 @@
1
- import { AnyObject, AuthGuardLocation, AuthGuardLocationResponse, CHANNELS, AuthSocketSchema } from "prostgles-types";
2
- import { LocalParams, PRGLIOSocket } from "../DboBuilder/DboBuilder";
3
- import { DBOFullyTyped } from "../DBSchemaBuilder";
4
- import { removeExpressRoute } from "../FileManager/FileManager";
5
- import { DB, DBHandlerServer, Prostgles } from "../Prostgles";
6
- import { Auth, AuthClientRequest, AuthResult, BasicSession, ExpressReq, ExpressRes, LoginClientInfo, LoginParams } from "./AuthTypes"
7
- import { getSafeReturnURL } from "./getSafeReturnURL";
8
- import { setupAuthRoutes } from "./setupAuthRoutes";
9
- import { getProviders } from "./setAuthProviders";
10
-
11
- export const HTTPCODES = {
12
- AUTH_ERROR: 401,
13
- NOT_FOUND: 404,
14
- BAD_REQUEST: 400,
15
- INTERNAL_SERVER_ERROR: 500,
16
- };
17
-
18
- export const getLoginClientInfo = (req: AuthClientRequest): AuthClientRequest & LoginClientInfo => {
19
- if("httpReq" in req){
20
- const ip_address = req.httpReq.ip;
21
- if(!ip_address) throw new Error("ip_address missing from req.httpReq");
22
- const user_agent = req.httpReq.headers["user-agent"];
23
- return {
24
- ...req,
25
- ip_address,
26
- ip_address_remote: req.httpReq.connection.remoteAddress,
27
- x_real_ip: req.httpReq.headers['x-real-ip'] as any,
28
- user_agent,
29
- };
30
- } else {
31
- return {
32
- ...req,
33
- ip_address: req.socket.handshake.address,
34
- ip_address_remote: req.socket.request.connection.remoteAddress,
35
- x_real_ip: req.socket.handshake.headers?.["x-real-ip"],
36
- user_agent: req.socket.handshake.headers?.['user-agent'],
37
- }
38
- }
39
- }
40
-
41
- export const AUTH_ROUTES_AND_PARAMS = {
42
- login: "/login",
43
- loginWithProvider: "/auth",
44
- emailSignup: "/register",
45
- returnUrlParamName: "returnURL",
46
- sidKeyName: "session_id",
47
- logoutGetPath: "/logout",
48
- magicLinksRoute: "/magic-link",
49
- magicLinksExpressRoute: "/magic-link/:id",
50
- confirmEmail: "/confirm-email",
51
- confirmEmailExpressRoute: "/confirm-email/:id",
52
- catchAll: "*",
53
- } as const;
54
-
55
- export class AuthHandler {
56
- protected prostgles: Prostgles;
57
- protected opts?: Auth;
58
- dbo: DBHandlerServer;
59
- db: DB;
60
-
61
- constructor(prostgles: Prostgles) {
62
- this.prostgles = prostgles;
63
- this.opts = prostgles.opts.auth as any;
64
- if(!prostgles.dbo || !prostgles.db) throw "dbo or db missing";
65
- this.dbo = prostgles.dbo;
66
- this.db = prostgles.db;
67
- }
68
-
69
- get sidKeyName() {
70
- return this.opts?.sidKeyName ?? AUTH_ROUTES_AND_PARAMS.sidKeyName;
71
- }
72
-
73
- validateSid = (sid: string | undefined) => {
74
- if (!sid) return undefined;
75
- if (typeof sid !== "string") throw "sid missing or not a string";
76
- return sid;
77
- }
78
-
79
- matchesRoute = (route: string | undefined, clientFullRoute: string) => {
80
- return route && clientFullRoute && (
81
- route === clientFullRoute ||
82
- clientFullRoute.startsWith(route) && ["/", "?", "#"].includes(clientFullRoute[route.length] ?? "")
83
- )
84
- }
85
-
86
- isUserRoute = (pathname: string) => {
87
- const { login, logoutGetPath, magicLinksRoute, loginWithProvider } = AUTH_ROUTES_AND_PARAMS;
88
- const pubRoutes = [
89
- ...this.opts?.expressConfig?.publicRoutes || [],
90
- login, logoutGetPath, magicLinksRoute, loginWithProvider,
91
- ].filter(publicRoute => publicRoute);
92
-
93
- return !pubRoutes.some(publicRoute => {
94
- return this.matchesRoute(publicRoute, pathname);
95
- });
96
- }
97
-
98
- setCookieAndGoToReturnURLIFSet = (cookie: { sid: string; expires: number; }, r: { req: ExpressReq; res: ExpressRes }) => {
99
- const { sid, expires } = cookie;
100
- const { res, req } = r;
101
- if (sid) {
102
- const maxAgeOneDay = 60 * 60 * 24; // 24 hours;
103
- type CD = { maxAge: number } | { expires: Date }
104
- let cookieDuration: CD = {
105
- maxAge: maxAgeOneDay
106
- }
107
- if(expires && Number.isFinite(expires) && !isNaN(+ new Date(expires))){
108
- // const maxAge = (+new Date(expires)) - Date.now();
109
- cookieDuration = { expires: new Date(expires) };
110
- const days = (+cookieDuration.expires - Date.now())/(24 * 60 * 60e3);
111
- if(days >= 400){
112
- console.warn(`Cookie expiration is higher than the Chrome 400 day limit: ${days}days`)
113
- }
114
- }
115
-
116
- const cookieOpts = {
117
- ...cookieDuration,
118
- httpOnly: true, // The cookie only accessible by the web server
119
- //signed: true // Indicates if the cookie should be signed
120
- secure: true,
121
- sameSite: "strict" as const,
122
- ...(this.opts?.expressConfig?.cookieOptions || {})
123
- };
124
- const cookieData = sid;
125
- res.cookie(this.sidKeyName, cookieData, cookieOpts);
126
- const successURL = this.getReturnUrl(req) || "/";
127
- res.redirect(successURL);
128
-
129
- } else {
130
- throw ("no user or session")
131
- }
132
- }
133
-
134
- getUser = async (clientReq: { httpReq: ExpressReq; }): Promise<AuthResult> => {
135
- if(!this.opts?.getUser) {
136
- throw "this.opts.getUser missing";
137
- }
138
- const sid = clientReq.httpReq?.cookies?.[this.sidKeyName];
139
- if (!sid) return undefined;
140
-
141
- try {
142
- return this.throttledFunc(async () => {
143
- return this.opts!.getUser(this.validateSid(sid), this.dbo as any, this.db, getLoginClientInfo(clientReq));
144
- }, 50)
145
- } catch (err) {
146
- console.error(err);
147
- }
148
- return undefined;
149
- }
150
-
151
- init = setupAuthRoutes.bind(this);
152
-
153
- getReturnUrl = (req: ExpressReq) => {
154
- const { returnUrlParamName } = AUTH_ROUTES_AND_PARAMS;
155
- if (returnUrlParamName && req?.query?.[returnUrlParamName]) {
156
- const returnURL = decodeURIComponent(req?.query?.[returnUrlParamName] as string);
157
-
158
- return getSafeReturnURL(returnURL, returnUrlParamName);
159
- }
160
- return null;
161
- }
162
-
163
- destroy = () => {
164
- const app = this.opts?.expressConfig?.app;
165
- const { login, logoutGetPath, magicLinksExpressRoute, catchAll, loginWithProvider, emailSignup, magicLinksRoute, confirmEmail } = AUTH_ROUTES_AND_PARAMS;
166
- removeExpressRoute(app, [login, logoutGetPath, magicLinksExpressRoute, catchAll, loginWithProvider, emailSignup, magicLinksRoute, confirmEmail]);
167
- }
168
-
169
- throttledFunc = <T>(func: () => Promise<T>, throttle = 500): Promise<T> => {
170
-
171
- return new Promise(async (resolve, reject) => {
172
-
173
- let result: any, error: any, finished = false;
174
-
175
- /**
176
- * Throttle reject response times to prevent timing attacks
177
- */
178
- const interval = setInterval(() => {
179
- if (finished) {
180
- clearInterval(interval);
181
- if (error) {
182
- reject(error);
183
- } else {
184
- resolve(result)
185
- }
186
- }
187
- }, throttle);
188
-
189
-
190
- try {
191
- result = await func();
192
- resolve(result);
193
- clearInterval(interval);
194
- } catch (err) {
195
- console.log(err)
196
- error = err;
197
- }
198
-
199
- finished = true;
200
- })
201
- }
202
-
203
- loginThrottled = async (params: LoginParams, client: LoginClientInfo): Promise<BasicSession> => {
204
- if (!this.opts?.login) throw "Auth login config missing";
205
- const { responseThrottle = 500 } = this.opts;
206
-
207
- return this.throttledFunc(async () => {
208
- const result = await this.opts?.login?.(params, this.dbo as DBOFullyTyped, this.db, client);
209
- const err = {
210
- msg: "Bad login result type. \nExpecting: undefined | null | { sid: string; expires: number } but got: " + JSON.stringify(result)
211
- }
212
-
213
- if(!result) throw err;
214
- if(result && (typeof result.sid !== "string" || typeof result.expires !== "number") || !result && ![undefined, null].includes(result)) {
215
- throw err
216
- }
217
- if(result && result.expires < Date.now()){
218
- throw { msg: "auth.login() is returning an expired session. Can only login with a session.expires greater than Date.now()" }
219
- }
220
-
221
- return result;
222
- }, responseThrottle);
223
-
224
- };
225
-
226
- loginThrottledAndSetCookie = async (req: ExpressReq, res: ExpressRes, loginParams: LoginParams) => {
227
- const start = Date.now();
228
- const { sid, expires } = await this.loginThrottled(loginParams, getLoginClientInfo({ httpReq: req })) || {};
229
- await this.prostgles.opts.onLog?.({
230
- type: "auth",
231
- command: "login",
232
- duration: Date.now() - start,
233
- sid,
234
- socketId: undefined,
235
- });
236
-
237
- if (sid) {
238
-
239
- this.setCookieAndGoToReturnURLIFSet({ sid, expires }, { req, res });
240
-
241
- } else {
242
- throw ("Internal error: no user or session")
243
- }
244
- }
245
-
246
-
247
- /**
248
- * Will return first sid value found in:
249
- * Bearer header
250
- * http cookie
251
- * query params
252
- * Based on sid names in auth
253
- */
254
- getSID(localParams: LocalParams): string | undefined {
255
- if (!this.opts) return undefined;
256
-
257
- if (!localParams) return undefined;
258
- const { sidKeyName } = this;
259
- if (localParams.socket) {
260
- const { handshake } = localParams.socket;
261
- const querySid = handshake?.auth?.[sidKeyName] || handshake?.query?.[sidKeyName];
262
- let rawSid = querySid;
263
- if (!rawSid) {
264
- const cookie_str = localParams.socket?.handshake?.headers?.cookie;
265
- const cookie = parseCookieStr(cookie_str);
266
- rawSid = cookie[sidKeyName];
267
- }
268
- return this.validateSid(rawSid);
269
-
270
- } else if (localParams.httpReq) {
271
- const [tokenType, base64Token] = localParams.httpReq.headers.authorization?.split(' ') ?? [];
272
- let bearerSid: string | undefined;
273
- if(tokenType && base64Token){
274
- if(tokenType.trim() !== "Bearer"){
275
- throw "Only Bearer Authorization header allowed";
276
- }
277
- bearerSid = Buffer.from(base64Token, 'base64').toString();
278
- }
279
- return this.validateSid(bearerSid ?? localParams.httpReq?.cookies?.[sidKeyName]);
280
-
281
- } else throw "socket OR httpReq missing from localParams";
282
-
283
- function parseCookieStr(cookie_str: string | undefined): any {
284
- if (!cookie_str || typeof cookie_str !== "string") {
285
- return {}
286
- }
287
-
288
- return cookie_str.replace(/\s/g, '')
289
- .split(";")
290
- .reduce<AnyObject>((prev, current) => {
291
- const [name, value] = current.split('=');
292
- prev[name!] = value;
293
- return prev;
294
- }, {});
295
- }
296
- }
297
-
298
- /**
299
- * Used for logging
300
- */
301
- getSIDNoError = (localParams: LocalParams | undefined): string | undefined => {
302
- if(!localParams) return undefined;
303
- try {
304
- return this.getSID(localParams);
305
- } catch {
306
- return undefined;
307
- }
308
- }
309
-
310
- async getClientInfo(localParams: Pick<LocalParams, "socket" | "httpReq">): Promise<AuthResult> {
311
- if (!this.opts) return {};
312
-
313
- const getSession = this.opts.cacheSession?.getSession;
314
- const isSocket = "socket" in localParams;
315
- if(isSocket){
316
- if(getSession && localParams.socket?.__prglCache){
317
- const { session, user, clientUser } = localParams.socket.__prglCache;
318
- const isValid = this.isValidSocketSession(localParams.socket, session)
319
- if(isValid){
320
-
321
- return {
322
- sid: session.sid,
323
- user,
324
- clientUser,
325
- }
326
- } else return {
327
- sid: session.sid
328
- };
329
- }
330
- }
331
-
332
- const authStart = Date.now();
333
- const res = await this.throttledFunc(async () => {
334
-
335
- const { getUser } = this.opts ?? {};
336
-
337
- if (getUser && localParams && (localParams.httpReq || localParams.socket)) {
338
- const sid = this.getSID(localParams);
339
- const clientReq = localParams.httpReq? { httpReq: localParams.httpReq } : { socket: localParams.socket! };
340
- let user, clientUser;
341
- if(sid){
342
- const res = await getUser(sid, this.dbo as any, this.db, getLoginClientInfo(clientReq)) as any;
343
- user = res?.user;
344
- clientUser = res?.clientUser;
345
- }
346
- if(getSession && isSocket){
347
- const session = await getSession(sid, this.dbo as any, this.db)
348
- if(session?.expires && user && clientUser && localParams.socket){
349
- localParams.socket.__prglCache = {
350
- session,
351
- user,
352
- clientUser,
353
- }
354
- }
355
- }
356
- if(sid) {
357
- return { sid, user, clientUser }
358
- }
359
- }
360
-
361
- return {};
362
- }, 5);
363
-
364
- await this.prostgles.opts.onLog?.({
365
- type: "auth",
366
- command: "getClientInfo",
367
- duration: Date.now() - authStart,
368
- sid: res.sid,
369
- socketId: localParams.socket?.id,
370
- });
371
- return res;
372
- }
373
-
374
- isValidSocketSession = (socket: PRGLIOSocket, session: BasicSession): boolean => {
375
- const hasExpired = Boolean(session && session.expires <= Date.now())
376
- if(this.opts?.expressConfig?.publicRoutes && !this.opts.expressConfig?.disableSocketAuthGuard){
377
- const error = "Session has expired";
378
- if(hasExpired){
379
- if(session.onExpiration === "redirect")
380
- socket.emit(CHANNELS.AUTHGUARD, {
381
- shouldReload: session.onExpiration === "redirect",
382
- error
383
- });
384
- throw error;
385
- }
386
- }
387
- return Boolean(session && !hasExpired);
388
- }
389
-
390
- getClientAuth = async (clientReq: Pick<LocalParams, "socket" | "httpReq">): Promise<{ auth: AuthSocketSchema; userData: AuthResult; }> => {
391
-
392
- let pathGuard = false;
393
- if (this.opts?.expressConfig?.publicRoutes && !this.opts.expressConfig?.disableSocketAuthGuard) {
394
-
395
- pathGuard = true;
396
-
397
- if("socket" in clientReq && clientReq.socket){
398
- const { socket } = clientReq;
399
- socket.removeAllListeners(CHANNELS.AUTHGUARD)
400
- socket.on(CHANNELS.AUTHGUARD, async (params: AuthGuardLocation, cb = (_err: any, _res?: AuthGuardLocationResponse) => { /** EMPTY */ }) => {
401
-
402
- try {
403
-
404
- const { pathname, origin } = typeof params === "string" ? JSON.parse(params) : (params || {});
405
- if (pathname && typeof pathname !== "string") {
406
- console.warn("Invalid pathname provided for AuthGuardLocation: ", pathname);
407
- }
408
-
409
- /** These origins */
410
- const IGNORED_API_ORIGINS = ["file://"]
411
- if (!IGNORED_API_ORIGINS.includes(origin) && pathname && typeof pathname === "string" && this.isUserRoute(pathname) && !(await this.getClientInfo({ socket }))?.user) {
412
- cb(null, { shouldReload: true });
413
- } else {
414
- cb(null, { shouldReload: false });
415
- }
416
-
417
- } catch (err) {
418
- console.error("AUTHGUARD err: ", err);
419
- cb(err)
420
- }
421
- });
422
-
423
- }
424
- }
425
-
426
- const userData = await this.getClientInfo(clientReq);
427
- const auth: AuthSocketSchema = {
428
- providers: getProviders.bind(this)(),
429
- register: this.opts?.expressConfig?.registrations?.email && { type: this.opts?.expressConfig?.registrations?.email.signupType, url: AUTH_ROUTES_AND_PARAMS.emailSignup },
430
- user: userData?.clientUser,
431
- loginType: this.opts?.expressConfig?.registrations?.email?.signupType,
432
- pathGuard,
433
- };
434
- return { auth, userData };
435
- }
436
- }