prostgles-server 4.2.156 → 4.2.158

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 (40) hide show
  1. package/dist/Auth/AuthHandler.d.ts +2 -0
  2. package/dist/Auth/AuthHandler.d.ts.map +1 -1
  3. package/dist/Auth/AuthHandler.js +4 -2
  4. package/dist/Auth/AuthHandler.js.map +1 -1
  5. package/dist/Auth/AuthTypes.d.ts +63 -16
  6. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  7. package/dist/Auth/sendEmail.d.ts +7 -0
  8. package/dist/Auth/sendEmail.d.ts.map +1 -0
  9. package/dist/Auth/sendEmail.js +69 -0
  10. package/dist/Auth/sendEmail.js.map +1 -0
  11. package/dist/Auth/setAuthProviders.d.ts +4 -4
  12. package/dist/Auth/setAuthProviders.d.ts.map +1 -1
  13. package/dist/Auth/setAuthProviders.js +30 -52
  14. package/dist/Auth/setAuthProviders.js.map +1 -1
  15. package/dist/Auth/setEmailProvider.d.ts +4 -0
  16. package/dist/Auth/setEmailProvider.d.ts.map +1 -0
  17. package/dist/Auth/setEmailProvider.js +86 -0
  18. package/dist/Auth/setEmailProvider.js.map +1 -0
  19. package/dist/Auth/setupAuthRoutes.js +1 -1
  20. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  21. package/dist/Prostgles.d.ts +1 -0
  22. package/dist/Prostgles.d.ts.map +1 -1
  23. package/dist/Prostgles.js +6 -0
  24. package/dist/Prostgles.js.map +1 -1
  25. package/dist/initProstgles.d.ts.map +1 -1
  26. package/dist/initProstgles.js +2 -6
  27. package/dist/initProstgles.js.map +1 -1
  28. package/lib/Auth/AuthHandler.ts +4 -2
  29. package/lib/Auth/AuthTypes.ts +55 -18
  30. package/lib/Auth/sendEmail.ts +83 -0
  31. package/lib/Auth/setAuthProviders.ts +42 -65
  32. package/lib/Auth/setEmailProvider.ts +85 -0
  33. package/lib/Auth/setupAuthRoutes.ts +1 -1
  34. package/lib/Prostgles.ts +6 -0
  35. package/lib/initProstgles.ts +2 -5
  36. package/package.json +11 -7
  37. package/tests/client/package-lock.json +8 -2689
  38. package/tests/client/package.json +1 -1
  39. package/tests/server/index.ts +5 -3
  40. package/tests/server/package-lock.json +11 -3
@@ -0,0 +1,85 @@
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
+ import { promises } from "node:dns";
6
+
7
+ export async function setEmailProvider(this: AuthHandler, app: e.Express) {
8
+
9
+ const { email, websiteUrl } = this.opts?.expressConfig?.registrations ?? {};
10
+ if(!email) return;
11
+ if(websiteUrl){
12
+ await checkDmarc(websiteUrl);
13
+ }
14
+
15
+ app.post(AUTH_ROUTES_AND_PARAMS.emailSignup, async (req, res) => {
16
+ const { username, password } = req.body;
17
+ let validationError = "";
18
+ if(typeof username !== "string"){
19
+ validationError = "Invalid username";
20
+ }
21
+ if(email.signupType === "withPassword"){
22
+ const { minPasswordLength = 8 } = email;
23
+ if(typeof password !== "string"){
24
+ validationError = "Invalid password";
25
+ } else if(password.length < minPasswordLength){
26
+ validationError = `Password must be at least ${minPasswordLength} characters long`;
27
+ }
28
+ }
29
+ if(validationError){
30
+ res.status(400).json({ error: validationError });
31
+ return;
32
+ }
33
+ try {
34
+ let emailMessage: undefined | { message: Email; smtp: SMTPConfig };
35
+ if(email.signupType === "withPassword"){
36
+ if(email.emailConfirmation){
37
+ const { onSend, smtp } = email.emailConfirmation;
38
+ const message = await onSend({ email: username, confirmationUrlPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.confirmEmail}` });
39
+ emailMessage = { message: { ...message, to: username }, smtp };
40
+ }
41
+ } else {
42
+ const { emailMagicLink } = email;
43
+ const message = await emailMagicLink.onSend({ email: username, magicLinkPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.magicLinksRoute}` });
44
+ emailMessage = { message: { ...message, to: username }, smtp: emailMagicLink.smtp };
45
+ }
46
+
47
+ if(emailMessage){
48
+ await sendEmail(emailMessage.smtp, emailMessage.message);
49
+ res.json({ msg: "Email sent" });
50
+ }
51
+ } catch {
52
+ res.status(500).json({ error: "Failed to send email" });
53
+ }
54
+ });
55
+
56
+ if(email.signupType === "withPassword" && email.emailConfirmation){
57
+ app.get(AUTH_ROUTES_AND_PARAMS.confirmEmailExpressRoute, async (req, res) => {
58
+ const { id } = req.params ?? {};
59
+ try {
60
+ await email.emailConfirmation?.onConfirmed({ confirmationUrlPath: id });
61
+ res.json({ msg: "Email confirmed" });
62
+ } catch (_e) {
63
+ res.status(500).json({ error: "Failed to confirm email" });
64
+ }
65
+ });
66
+ }
67
+ }
68
+
69
+ const checkDmarc = async (websiteUrl: string) => {
70
+ const { host } = new URL(websiteUrl);
71
+ const ignoredHosts = ["localhost", "127.0.0.1"]
72
+ if(!host || ignoredHosts.includes(host)){
73
+ return;
74
+ }
75
+ const dmarc = await promises.resolveTxt(`_dmarc.${host}`);
76
+ const dmarkTxt = dmarc[0]?.[0];
77
+ if(
78
+ !dmarkTxt?.includes("v=DMARC1") ||
79
+ (!dmarkTxt?.includes("p=reject") && !dmarkTxt?.includes("p=quarantine"))
80
+ ){
81
+ throw new Error("DMARC not set to reject/quarantine");
82
+ } else {
83
+ console.log("DMARC set to reject")
84
+ }
85
+ }
@@ -27,7 +27,7 @@ export async function setupAuthRoutes(this: AuthHandler) {
27
27
  throw "Invalid or empty string provided within publicRoutes "
28
28
  }
29
29
 
30
- setAuthProviders.bind(this)(expressConfig);
30
+ await setAuthProviders.bind(this)(expressConfig);
31
31
 
32
32
  if(use){
33
33
  const prostglesUseMiddleware: RequestHandler = (req, res, next) => {
package/lib/Prostgles.ts CHANGED
@@ -226,6 +226,12 @@ export class Prostgles {
226
226
  }
227
227
  }
228
228
 
229
+ initAuthHandler = async () => {
230
+ this.authHandler?.destroy();
231
+ this.authHandler = new AuthHandler(this as any);
232
+ await this.authHandler.init();
233
+ }
234
+
229
235
  initTableConfig = async (reason: OnInitReason) => {
230
236
  const res = await tryCatch(async () => {
231
237
 
@@ -130,8 +130,7 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
130
130
  }
131
131
 
132
132
  /* 3.9 Check auth config */
133
- this.authHandler = new AuthHandler(this as any);
134
- await this.authHandler.init();
133
+ await this.initAuthHandler();
135
134
 
136
135
  this.publishParser = new PublishParser(this.opts.publish, this.opts.publishMethods as any, this.opts.publishRawSQL, this.dbo!, this.db, this as any);
137
136
  this.dboBuilder.publishParser = this.publishParser;
@@ -194,9 +193,7 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
194
193
  await this.refreshDBO();
195
194
  }
196
195
  if("auth" in newOpts){
197
- this.authHandler?.destroy();
198
- this.authHandler = new AuthHandler(this as any);
199
- await this.authHandler.init();
196
+ await this.initAuthHandler();
200
197
  }
201
198
 
202
199
  if(isEmpty(newOpts)) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.156",
3
+ "version": "4.2.158",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -35,28 +35,32 @@
35
35
  ],
36
36
  "homepage": "https://prostgles.com",
37
37
  "dependencies": {
38
+ "@aws-sdk/client-ses": "^3.699.0",
39
+ "@aws-sdk/credential-provider-node": "^3.699.0",
40
+ "@types/passport": "^1.0.17",
41
+ "@types/passport-facebook": "^3.0.3",
42
+ "@types/passport-github2": "^1.2.9",
43
+ "@types/passport-google-oauth20": "^2.0.16",
44
+ "@types/passport-microsoft": "^1.0.3",
38
45
  "body-parser": "^1.20.3",
39
46
  "check-disk-space": "^3.4.0",
40
47
  "file-type": "^18.5.0",
48
+ "nodemailer": "^6.9.16",
41
49
  "passport": "^0.7.0",
42
50
  "passport-facebook": "^3.0.0",
43
51
  "passport-github2": "^0.1.12",
44
52
  "passport-google-oauth20": "^2.0.0",
45
53
  "passport-microsoft": "^2.1.0",
46
- "@types/passport": "^1.0.17",
47
- "@types/passport-facebook": "^3.0.3",
48
- "@types/passport-github2": "^1.2.9",
49
- "@types/passport-google-oauth20": "^2.0.16",
50
- "@types/passport-microsoft": "^1.0.3",
51
54
  "pg": "^8.11.5",
52
55
  "pg-cursor": "^2.11.0",
53
56
  "pg-promise": "^11.9.1",
54
- "prostgles-types": "^4.0.105"
57
+ "prostgles-types": "^4.0.107"
55
58
  },
56
59
  "devDependencies": {
57
60
  "@types/express": "^4.17.21",
58
61
  "@types/json-schema": "^7.0.15",
59
62
  "@types/node": "^22.8.1",
63
+ "@types/nodemailer": "^6.4.17",
60
64
  "@types/pg": "^8.11.5",
61
65
  "@types/pg-cursor": "^2.7.2",
62
66
  "@types/sharp": "^0.30.4",