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,285 +0,0 @@
1
- import { Express, NextFunction, Request, Response } from "express";
2
- import { AnyObject, FieldFilter, IdentityProvider, UserLike } from "prostgles-types";
3
- import { DB } from "../Prostgles";
4
- import { DBOFullyTyped } from "../DBSchemaBuilder";
5
- import { PRGLIOSocket } from "../DboBuilder/DboBuilderTypes";
6
- import type { AuthenticateOptions } from "passport";
7
- import type { StrategyOptions as GoogleStrategy, Profile as GoogleProfile } from "passport-google-oauth20";
8
- import type { StrategyOptions as GitHubStrategy, Profile as GitHubProfile } from "passport-github2";
9
- import type { MicrosoftStrategyOptions } from "passport-microsoft";
10
- import type { StrategyOptions as FacebookStrategy, Profile as FacebookProfile } from "passport-facebook";
11
- import Mail from "nodemailer/lib/mailer";
12
-
13
- type Awaitable<T> = T | Promise<T>;
14
-
15
- export type ExpressReq = Request;
16
- export type ExpressRes = Response;
17
-
18
- export type LoginClientInfo = {
19
- ip_address: string;
20
- ip_address_remote: string | undefined;
21
- x_real_ip: string | undefined;
22
- user_agent: string | undefined;
23
- };
24
-
25
- export type BasicSession = {
26
-
27
- /** Must be hard to bruteforce */
28
- sid: string;
29
-
30
- /** UNIX millisecond timestamp */
31
- expires: number;
32
-
33
- /** On expired */
34
- onExpiration: "redirect" | "show_error";
35
- };
36
- export type AuthClientRequest = { socket: PRGLIOSocket } | { httpReq: ExpressReq };
37
-
38
- type ThirdPartyProviders = {
39
- facebook?: Pick<FacebookStrategy, "clientID" | "clientSecret"> & {
40
- authOpts?: AuthenticateOptions;
41
- };
42
- google?: Pick<GoogleStrategy, "clientID" | "clientSecret"> & {
43
- authOpts?: AuthenticateOptions;
44
- };
45
- github?: Pick<GitHubStrategy, "clientID" | "clientSecret"> & {
46
- authOpts?: AuthenticateOptions;
47
- };
48
- microsoft?: Pick<MicrosoftStrategyOptions, "clientID" | "clientSecret"> & {
49
- authOpts?: AuthenticateOptions;
50
- };
51
- };
52
-
53
- export type SMTPConfig = {
54
- type: "smtp";
55
- host: string;
56
- port: number;
57
- secure: boolean;
58
- user: string;
59
- pass: string;
60
- } | {
61
- type: "aws-ses";
62
- region: string;
63
- accessKeyId: string;
64
- secretAccessKey: string;
65
- /**
66
- * Sending rate per second
67
- * Defaults to 1
68
- */
69
- sendingRate?: number;
70
- }
71
-
72
- export type Email = {
73
- from: string;
74
- to: string;
75
- subject: string;
76
- html: string;
77
- text?: string;
78
- attachments?: { filename: string; content: string; }[] | Mail.Attachment[];
79
- }
80
-
81
- type EmailWithoutTo = Omit<Email, "to">;
82
-
83
- type EmailProvider =
84
- | {
85
- signupType: "withMagicLink";
86
- onRegistered: (data: { username: string; }) => void | Promise<void>;
87
- emailMagicLink: {
88
- onSend: (data: { email: string; magicLinkPath: string; }) => EmailWithoutTo | Promise<EmailWithoutTo>;
89
- smtp: SMTPConfig;
90
- };
91
- }
92
- | {
93
- signupType: "withPassword";
94
- onRegistered: (data: { username: string; password: string; }) => void | Promise<void>;
95
- /**
96
- * Defaults to 8
97
- */
98
- minPasswordLength: number;
99
- /**
100
- * If provided, the user will be required to confirm their email address
101
- */
102
- emailConfirmation?: {
103
- onSend: (data: { email: string; confirmationUrlPath: string; }) => EmailWithoutTo | Promise<EmailWithoutTo>;
104
- smtp: SMTPConfig;
105
- onConfirmed: (data: { confirmationUrlPath: string; }) => void | Promise<void>;
106
- };
107
- };
108
-
109
- export type AuthProviderUserData =
110
- | {
111
- provider: "google";
112
- profile: GoogleProfile;
113
- accessToken: string;
114
- refreshToken: string;
115
- }
116
- | {
117
- provider: "github";
118
- profile: GitHubProfile;
119
- accessToken: string;
120
- refreshToken: string;
121
- }
122
- | {
123
- provider: "facebook";
124
- profile: FacebookProfile;
125
- accessToken: string;
126
- refreshToken: string;
127
- }
128
- | {
129
- provider: "microsoft";
130
- profile: any;
131
- accessToken: string;
132
- refreshToken: string;
133
- }
134
-
135
- export type RegistrationData =
136
- | {
137
- provider: "email";
138
- profile: {
139
- username: string;
140
- password: string;
141
- }
142
- }
143
- | AuthProviderUserData;
144
-
145
- export type AuthRegistrationConfig<S> = {
146
- email?: EmailProvider;
147
-
148
- OAuthProviders?: ThirdPartyProviders;
149
-
150
- /**
151
- * Required for social login callback
152
- */
153
- websiteUrl: string;
154
-
155
- /**
156
- * Do something with the registered user
157
- */
158
- onRegister: (data: RegistrationData) => void | Promise<any>;
159
-
160
- /**
161
- * Used to stop abuse
162
- */
163
- onProviderLoginStart: (data: { provider: IdentityProvider; dbo: DBOFullyTyped<S>, db: DB; req: ExpressReq, res: ExpressRes; clientInfo: LoginClientInfo }) => Promise<{ error: string; } | { ok: true; }>;
164
-
165
- /**
166
- * Used to identify abuse
167
- */
168
- onProviderLoginFail: (data: { provider: IdentityProvider; error: any; dbo: DBOFullyTyped<S>, db: DB; req: ExpressReq, res: ExpressRes; clientInfo: LoginClientInfo }) => void | Promise<void>;
169
- };
170
-
171
- export type SessionUser<ServerUser extends UserLike = UserLike, ClientUser extends UserLike = UserLike> = {
172
- /**
173
- * This user will be available in all serverside prostgles options
174
- * id and type values will be available in the prostgles.user session variable in postgres
175
- * */
176
- user: ServerUser;
177
- /**
178
- * Controls which fields from user are available in postgres session variable
179
- */
180
- sessionFields?: FieldFilter<ServerUser>;
181
- /**
182
- * User data sent to the authenticated client
183
- */
184
- clientUser: ClientUser;
185
- }
186
-
187
- export type AuthResult<SU = SessionUser> = SU & { sid: string; } | {
188
- user?: undefined;
189
- clientUser?: undefined;
190
- sid?: string;
191
- } | undefined;
192
-
193
- export type AuthRequestParams<S, SUser extends SessionUser> = { db: DB, dbo: DBOFullyTyped<S>; getUser: () => Promise<AuthResult<SUser>> }
194
-
195
- export type Auth<S = void, SUser extends SessionUser = SessionUser> = {
196
- /**
197
- * Name of the cookie or socket hadnshake query param that represents the session id.
198
- * Defaults to "session_id"
199
- */
200
- sidKeyName?: string;
201
-
202
- /**
203
- * Response time rounding in milliseconds to prevent timing attacks on login. Login response time should always be a multiple of this value. Defaults to 500 milliseconds
204
- */
205
- responseThrottle?: number;
206
-
207
- /**
208
- * Will setup auth routes
209
- * /login
210
- * /logout
211
- * /magic-link/:id
212
- */
213
- expressConfig?: {
214
-
215
- /**
216
- * Express app instance. If provided Prostgles will attempt to set sidKeyName to user cookie
217
- */
218
- app: Express;
219
-
220
- /**
221
- * Options used in setting the cookie after a successful login
222
- */
223
- cookieOptions?: AnyObject;
224
-
225
- /**
226
- * False by default. If false and userRoutes are provided then the socket will request window.location.reload if the current url is on a user route.
227
- */
228
- disableSocketAuthGuard?: boolean;
229
-
230
- /**
231
- * If provided, any client requests to NOT these routes (or their subroutes) will be redirected to loginRoute (if logged in) and then redirected back to the initial route after logging in
232
- * If logged in the user is allowed to access these routes
233
- */
234
- publicRoutes?: string[];
235
-
236
- /**
237
- * Will attach a app.use listener and will expose getUser
238
- * Used for blocking access
239
- */
240
- use?: (args: { req: ExpressReq; res: ExpressRes, next: NextFunction } & AuthRequestParams<S, SUser>) => void | Promise<void>;
241
-
242
- /**
243
- * Will be called after a GET request is authorised
244
- * This means that
245
- */
246
- onGetRequestOK?: (
247
- req: ExpressReq,
248
- res: ExpressRes,
249
- params: AuthRequestParams<S, SUser>
250
- ) => any;
251
-
252
- /**
253
- * If defined, will check the magic link id and log in the user and redirect to the returnUrl if set
254
- */
255
- magicLinks?: {
256
-
257
- /**
258
- * Used in creating a session/logging in using a magic link
259
- */
260
- check: (magicId: string, dbo: DBOFullyTyped<S>, db: DB, client: LoginClientInfo) => Awaitable<BasicSession | undefined>;
261
- }
262
-
263
- registrations?: AuthRegistrationConfig<S>;
264
- }
265
-
266
- /**
267
- * undefined sid is allowed to enable public users
268
- */
269
- getUser: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB, client: AuthClientRequest & LoginClientInfo) => Awaitable<AuthResult<SUser>>;
270
-
271
- login?: (params: LoginParams, dbo: DBOFullyTyped<S>, db: DB, client: LoginClientInfo) => Awaitable<BasicSession> | BasicSession;
272
- logout?: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB) => Awaitable<any>;
273
-
274
- /**
275
- * If provided then session info will be saved on socket.__prglCache and reused from there
276
- */
277
- cacheSession?: {
278
- getSession: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB) => Awaitable<BasicSession>
279
- }
280
- }
281
-
282
-
283
- export type LoginParams =
284
- | { type: "username"; username: string; password: string; [key: string]: any }
285
- | ({ type: "provider"; } & AuthProviderUserData)
@@ -1,35 +0,0 @@
1
- export const getSafeReturnURL = (returnURL: string, returnUrlParamName: string, quiet = false) => {
2
- /** Dissalow redirect to other domains */
3
- if(returnURL) {
4
- const allowedOrigin = "https://localhost";
5
- const { origin, pathname, search, searchParams } = new URL(returnURL, allowedOrigin);
6
- if(
7
- origin !== allowedOrigin ||
8
- returnURL !== `${pathname}${search}` ||
9
- searchParams.get(returnUrlParamName)
10
- ){
11
- if(!quiet){
12
- console.error(`Unsafe returnUrl: ${returnURL}. Redirecting to /`);
13
- }
14
- return "/";
15
- }
16
-
17
- return returnURL;
18
- }
19
- }
20
-
21
- const issue = ([
22
- ["https://localhost", "/"],
23
- ["//localhost.bad.com", "/"],
24
- ["//localhost.com", "/"],
25
- ["/localhost/com", "/localhost/com"],
26
- ["/localhost/com?here=there", "/localhost/com?here=there"],
27
- ["/localhost/com?returnUrl=there", "/"],
28
- ["//http://localhost.com", "/"],
29
- ["//abc.com", "/"],
30
- ["///abc.com", "/"],
31
- ] as const).find(([returnURL, expected]) => getSafeReturnURL(returnURL, "returnUrl", true) !== expected);
32
-
33
- if(issue){
34
- throw new Error(`getSafeReturnURL failed for ${issue[0]}. Expected: ${issue[1]}`);
35
- }
@@ -1,83 +0,0 @@
1
- import { Email, SMTPConfig } from "./AuthTypes";
2
- import nodemailer from "nodemailer";
3
- import aws from "@aws-sdk/client-ses";
4
- import SESTransport from "nodemailer/lib/ses-transport";
5
-
6
- type SESTransporter = nodemailer.Transporter<SESTransport.SentMessageInfo, SESTransport.Options>;
7
- type SMTPTransporter = nodemailer.Transporter<nodemailer.SentMessageInfo, nodemailer.TransportOptions>;
8
- type Transporter = SESTransporter | SMTPTransporter;
9
-
10
- const transporterCache: Map<string, Transporter> = new Map();
11
-
12
- /**
13
- * Allows sending emails using nodemailer default config or AWS SES
14
- * https://www.nodemailer.com/transports/ses/
15
- */
16
- export const sendEmail = (smptConfig: SMTPConfig, email: Email) => {
17
- const configStr = JSON.stringify(smptConfig);
18
- const transporter = transporterCache.get(configStr) ?? getTransporter(smptConfig);
19
- if(!transporterCache.has(configStr)){
20
- transporterCache.set(configStr, transporter);
21
- }
22
-
23
- return send(transporter, email);
24
- }
25
-
26
- const getTransporter = (smptConfig: SMTPConfig) => {
27
- let transporter: Transporter | undefined;
28
- if(smptConfig.type === "aws-ses"){
29
- const {
30
- region,
31
- accessKeyId,
32
- secretAccessKey,
33
- /**
34
- * max 1 messages/second
35
- */
36
- sendingRate = 1
37
- } = smptConfig;
38
- const ses = new aws.SES({
39
- apiVersion: "2010-12-01",
40
- region,
41
- credentials: {
42
- accessKeyId,
43
- secretAccessKey
44
- }
45
- });
46
-
47
- transporter = nodemailer.createTransport({
48
- SES: { ses, aws },
49
- maxConnections: 1,
50
- sendingRate
51
- });
52
-
53
- } else {
54
- const { user, pass, host, port, secure } = smptConfig;
55
- transporter = nodemailer.createTransport({
56
- host,
57
- port,
58
- secure,
59
- auth: { user, pass }
60
- });
61
- }
62
-
63
- return transporter;
64
- }
65
-
66
- const send = (transporter: Transporter, email: Email) => {
67
- return new Promise((resolve, reject) => {
68
- transporter.once('idle', () => {
69
- if (transporter.isIdle()) {
70
- transporter.sendMail(
71
- email,
72
- (err, info) => {
73
- if(err){
74
- reject(err);
75
- } else {
76
- resolve(info);
77
- }
78
- }
79
- );
80
- }
81
- });
82
- });
83
- };
@@ -1,129 +0,0 @@
1
- import type e from "express";
2
- import { RequestHandler } from "express";
3
- import { Strategy as FacebookStrategy } from "passport-facebook";
4
- import { Strategy as GitHubStrategy } from "passport-github2";
5
- import { Strategy as GoogleStrategy } from "passport-google-oauth20";
6
- import { Strategy as MicrosoftStrategy } from "passport-microsoft";
7
- import { AuthSocketSchema, getObjectEntries, isEmpty } from "prostgles-types";
8
- import { getErrorAsObject } from "../DboBuilder/dboBuilderUtils";
9
- import { removeExpressRouteByName } from "../FileManager/FileManager";
10
- import { AUTH_ROUTES_AND_PARAMS, AuthHandler, getLoginClientInfo } from "./AuthHandler";
11
- import { Auth } from './AuthTypes';
12
- import { setEmailProvider } from "./setEmailProvider";
13
- /** For some reason normal import is undefined */
14
- const passport = require("passport") as typeof import("passport");
15
-
16
- export const upsertNamedExpressMiddleware = (app: e.Express, handler: RequestHandler, name: string) => {
17
- const funcName = name;
18
- Object.defineProperty(handler, "name", { value: funcName });
19
- removeExpressRouteByName(app, name);
20
- app.use(handler);
21
- }
22
-
23
- export function setAuthProviders (this: AuthHandler, { registrations, app }: Required<Auth>["expressConfig"]) {
24
- if(!registrations) return;
25
- const { onRegister, onProviderLoginFail, onProviderLoginStart, websiteUrl, OAuthProviders } = registrations;
26
-
27
- setEmailProvider.bind(this)(app);
28
-
29
- if(!OAuthProviders || isEmpty(OAuthProviders)){
30
- return;
31
- }
32
-
33
- upsertNamedExpressMiddleware(app, passport.initialize(), "prostglesPassportMiddleware");
34
-
35
- getObjectEntries(OAuthProviders).forEach(([providerName, providerConfig]) => {
36
-
37
- if(!providerConfig?.clientID){
38
- return;
39
- }
40
-
41
- const { authOpts, ...config } = providerConfig;
42
-
43
- const strategy = providerName === "google" ? GoogleStrategy :
44
- providerName === "github" ? GitHubStrategy :
45
- providerName === "facebook" ? FacebookStrategy :
46
- providerName === "microsoft" ? MicrosoftStrategy :
47
- undefined
48
- ;
49
-
50
- const callbackPath = `${AUTH_ROUTES_AND_PARAMS.loginWithProvider}/${providerName}/callback`;
51
- passport.use(
52
- new (strategy as typeof GoogleStrategy)(
53
- {
54
- ...config,
55
- callbackURL: `${websiteUrl}${callbackPath}`,
56
- },
57
- async (accessToken, refreshToken, profile, done) => {
58
- // This callback is where you would normally store or retrieve user info from the database
59
- await onRegister({ provider: providerName as "google", accessToken, refreshToken, profile });
60
- return done(null, profile, { accessToken, refreshToken, profile });
61
- }
62
- )
63
- );
64
-
65
- app.get(`${AUTH_ROUTES_AND_PARAMS.loginWithProvider}/${providerName}`,
66
- passport.authenticate(providerName, authOpts ?? {})
67
- );
68
-
69
- app.get(
70
- callbackPath,
71
- async (req, res) => {
72
- try {
73
- const clientInfo = getLoginClientInfo({ httpReq: req });
74
- const db = this.db;
75
- const dbo = this.dbo as any;
76
- const args = { provider: providerName, req, res, clientInfo, db, dbo };
77
- const startCheck = await onProviderLoginStart(args);
78
- if("error" in startCheck){
79
- res.status(500).json({ error: startCheck.error });
80
- return;
81
- }
82
- passport.authenticate(
83
- providerName,
84
- {
85
- session: false,
86
- failureRedirect: "/login",
87
- failWithError: true,
88
- },
89
- async (error: any, _profile: any, authInfo: any) => {
90
- if(error){
91
- await onProviderLoginFail({ ...args, error });
92
- res.status(500).json({
93
- error: "Failed to login with provider",
94
- });
95
- } else {
96
- this.loginThrottledAndSetCookie(req, res, { type: "provider", provider: providerName, ...authInfo })
97
- .catch((e: any) => {
98
- res.status(500).json(getErrorAsObject(e));
99
- });
100
- }
101
- }
102
- )(req, res);
103
-
104
- } catch (_e) {
105
- res.status(500).json({ error: "Something went wrong" });
106
- }
107
- }
108
- );
109
-
110
- });
111
- }
112
-
113
- export function getProviders(this: AuthHandler): AuthSocketSchema["providers"] | undefined {
114
- const { registrations } = this.opts?.expressConfig ?? {}
115
- if(!registrations) return undefined;
116
- const { OAuthProviders } = registrations;
117
- if(!OAuthProviders || isEmpty(OAuthProviders)) return undefined;
118
-
119
- const result: AuthSocketSchema["providers"] = {}
120
- getObjectEntries(OAuthProviders).forEach(([providerName, config]) => {
121
- if(config?.clientID){
122
- result[providerName] = {
123
- url: `${AUTH_ROUTES_AND_PARAMS.loginWithProvider}/${providerName}`,
124
- }
125
- }
126
- });
127
-
128
- return result;
129
- }
@@ -1,63 +0,0 @@
1
- import e from "express";
2
- import { AUTH_ROUTES_AND_PARAMS, AuthHandler } from "./AuthHandler";
3
- import { Email, SMTPConfig } from "./AuthTypes";
4
- import { sendEmail } from "./sendEmail";
5
-
6
- export function setEmailProvider(this: AuthHandler, app: e.Express) {
7
-
8
- const { email, websiteUrl } = this.opts?.expressConfig?.registrations ?? {};
9
- if(!email) return;
10
-
11
- app.post(AUTH_ROUTES_AND_PARAMS.emailSignup, async (req, res) => {
12
- const { username, password } = req.body;
13
- let validationError = "";
14
- if(typeof username !== "string"){
15
- validationError = "Invalid username";
16
- }
17
- if(email.signupType === "withPassword"){
18
- const { minPasswordLength = 8 } = email;
19
- if(typeof password !== "string"){
20
- validationError = "Invalid password";
21
- } else if(password.length < minPasswordLength){
22
- validationError = `Password must be at least ${minPasswordLength} characters long`;
23
- }
24
- }
25
- if(validationError){
26
- res.status(400).json({ error: validationError });
27
- return;
28
- }
29
- try {
30
- let emailMessage: undefined | { message: Email; smtp: SMTPConfig };
31
- if(email.signupType === "withPassword"){
32
- if(email.emailConfirmation){
33
- const { onSend, smtp } = email.emailConfirmation;
34
- const message = await onSend({ email: username, confirmationUrlPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.confirmEmail}` });
35
- emailMessage = { message: { ...message, to: username }, smtp };
36
- }
37
- } else {
38
- const { emailMagicLink } = email;
39
- const message = await emailMagicLink.onSend({ email: username, magicLinkPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.magicLinksRoute}` });
40
- emailMessage = { message: { ...message, to: username }, smtp: emailMagicLink.smtp };
41
- }
42
-
43
- if(emailMessage){
44
- await sendEmail(emailMessage.smtp, emailMessage.message);
45
- res.json({ msg: "Email sent" });
46
- }
47
- } catch {
48
- res.status(500).json({ error: "Failed to send email" });
49
- }
50
- });
51
-
52
- if(email.signupType === "withPassword" && email.emailConfirmation){
53
- app.get(AUTH_ROUTES_AND_PARAMS.confirmEmailExpressRoute, async (req, res) => {
54
- const { id } = req.params ?? {};
55
- try {
56
- await email.emailConfirmation?.onConfirmed({ confirmationUrlPath: id });
57
- res.json({ msg: "Email confirmed" });
58
- } catch (_e) {
59
- res.status(500).json({ error: "Failed to confirm email" });
60
- }
61
- });
62
- }
63
- }