aaspai-authx 0.1.5 → 0.1.6

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/index.d.cts CHANGED
@@ -1142,7 +1142,7 @@ declare class EmailService {
1142
1142
  constructor();
1143
1143
  sign(payload: any, ttlSec?: number): string;
1144
1144
  verify<T = any>(token: string): T;
1145
- send(to: string, subject: string, html: string): Promise<nodemailer_lib_smtp_transport.SentMessageInfo>;
1145
+ send(to: string, subject: string, html: string, from?: string): Promise<nodemailer_lib_smtp_transport.SentMessageInfo>;
1146
1146
  canSend(lastEmailSent: Date[]): {
1147
1147
  ok: boolean;
1148
1148
  reason: string;
package/dist/index.d.ts CHANGED
@@ -1142,7 +1142,7 @@ declare class EmailService {
1142
1142
  constructor();
1143
1143
  sign(payload: any, ttlSec?: number): string;
1144
1144
  verify<T = any>(token: string): T;
1145
- send(to: string, subject: string, html: string): Promise<nodemailer_lib_smtp_transport.SentMessageInfo>;
1145
+ send(to: string, subject: string, html: string, from?: string): Promise<nodemailer_lib_smtp_transport.SentMessageInfo>;
1146
1146
  canSend(lastEmailSent: Date[]): {
1147
1147
  ok: boolean;
1148
1148
  reason: string;
package/dist/index.js CHANGED
@@ -48,8 +48,8 @@ function loadConfig() {
48
48
  cookies: {
49
49
  domain: process.env.COOKIE_DOMAIN,
50
50
  secure: (process.env.COOKIE_SECURE || "true") === "true",
51
- accessTtlMs: 24 * 60 * 60 * 1e3,
52
- refreshTtlMs: 7 * 24 * 60 * 60 * 1e3
51
+ accessTtlMs: 7 * 24 * 60 * 60 * 1e3,
52
+ refreshTtlMs: 30 * 24 * 60 * 60 * 1e3
53
53
  },
54
54
  oidc: {
55
55
  jwtSecret: process.env.JWT_SECRET
@@ -605,11 +605,11 @@ var AuthAdminService = class {
605
605
  system: true
606
606
  };
607
607
  const accessToken = jwt2.sign(payload, process.env.JWT_SECRET, {
608
- expiresIn: "1h"
608
+ expiresIn: "1d"
609
609
  });
610
610
  this.token = {
611
611
  accessToken,
612
- exp: now + 3600
612
+ exp: now + 84800
613
613
  };
614
614
  return this.token.accessToken;
615
615
  }
@@ -634,7 +634,7 @@ var EmailService = class {
634
634
  }
635
635
  });
636
636
  }
637
- sign(payload, ttlSec = 60 * 60 * 24) {
637
+ sign(payload, ttlSec = 60 * 60 * 24 * 30) {
638
638
  return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, {
639
639
  expiresIn: ttlSec
640
640
  });
@@ -642,10 +642,10 @@ var EmailService = class {
642
642
  verify(token) {
643
643
  return jwt3.verify(token, process.env.EMAIL_JWT_SECRET);
644
644
  }
645
- async send(to, subject, html) {
645
+ async send(to, subject, html, from) {
646
646
  try {
647
647
  const info = await this.transporter.sendMail({
648
- from: process.env.EMAIL_FROM,
648
+ from: from ? `${from} ` + process.env.EMAIL_FROM : process.env.EMAIL_FROM,
649
649
  to,
650
650
  subject,
651
651
  html
@@ -1084,7 +1084,7 @@ function createAuthRouter(options = {}) {
1084
1084
  // default: secure in prod
1085
1085
  domain: options.cookie?.domain ?? void 0,
1086
1086
  path: options.cookie?.path ?? "/",
1087
- maxAgeMs: options.cookie?.maxAgeMs ?? 24 * 60 * 60 * 1e3
1087
+ maxAgeMs: options.cookie?.maxAgeMs ?? 30 * 24 * 60 * 60 * 1e3
1088
1088
  };
1089
1089
  r.use(express.json());
1090
1090
  r.use(express.urlencoded({ extended: true }));
@@ -1141,6 +1141,7 @@ function createAuthRouter(options = {}) {
1141
1141
  projectId,
1142
1142
  metadata
1143
1143
  } = req.body || {};
1144
+ const COMPANY_NAME = process.env.COMPANY_NAME;
1144
1145
  try {
1145
1146
  const kcUser = await authAdmin.createUserInRealm({
1146
1147
  username: emailAddress,
@@ -1182,7 +1183,8 @@ function createAuthRouter(options = {}) {
1182
1183
  }
1183
1184
  )}`,
1184
1185
  expiresIn: "1 hour"
1185
- })
1186
+ }),
1187
+ from: COMPANY_NAME
1186
1188
  });
1187
1189
  if (emailResult.rateLimited) {
1188
1190
  return res.status(429).json({
@@ -1247,6 +1249,7 @@ function createAuthRouter(options = {}) {
1247
1249
  "/resend-verification-email",
1248
1250
  validateResendEmail,
1249
1251
  async (req, res) => {
1252
+ const COMPANY_NAME = process.env.COMPANY_NAME;
1250
1253
  const user = await OrgUser.findOne({ email: req.body.email });
1251
1254
  if (!user)
1252
1255
  return res.status(404).json({ ok: false, error: "User not found" });
@@ -1272,7 +1275,8 @@ function createAuthRouter(options = {}) {
1272
1275
  }
1273
1276
  )}`,
1274
1277
  expiresIn: "1 hour"
1275
- })
1278
+ }),
1279
+ from: COMPANY_NAME
1276
1280
  });
1277
1281
  if (resendResult.rateLimited) {
1278
1282
  return res.status(429).json({
@@ -1285,6 +1289,7 @@ function createAuthRouter(options = {}) {
1285
1289
  }
1286
1290
  );
1287
1291
  r.post("/forgot-password", validateResendEmail, async (req, res) => {
1292
+ const COMPANY_NAME = process.env.COMPANY_NAME;
1288
1293
  const user = await OrgUser.findOne({ email: req.body.email });
1289
1294
  if (!user)
1290
1295
  return res.status(404).json({ ok: false, error: "User not found" });
@@ -1311,7 +1316,8 @@ function createAuthRouter(options = {}) {
1311
1316
  }
1312
1317
  )}`,
1313
1318
  expiresIn: "1 hour"
1314
- })
1319
+ }),
1320
+ from: COMPANY_NAME
1315
1321
  });
1316
1322
  if (resetResult.rateLimited) {
1317
1323
  return res.status(429).json({
@@ -1735,13 +1741,14 @@ async function sendRateLimitedEmail({
1735
1741
  emailService,
1736
1742
  user,
1737
1743
  subject,
1738
- html
1744
+ html,
1745
+ from
1739
1746
  }) {
1740
1747
  const can = emailService.canSend(user?.lastEmailSent || []);
1741
1748
  if (!can.ok) {
1742
1749
  return { rateLimited: true, waitMs: can.waitMs };
1743
1750
  }
1744
- await emailService.send(user.email, subject, html);
1751
+ await emailService.send(user.email, subject, html, from);
1745
1752
  user.lastEmailSent = [...user.lastEmailSent || [], /* @__PURE__ */ new Date()];
1746
1753
  await user.save();
1747
1754
  return { rateLimited: false };
@@ -1762,7 +1769,7 @@ function generateTokens(user) {
1762
1769
  type: "user"
1763
1770
  };
1764
1771
  const accessToken = jwt4.sign(accessPayload, process.env.JWT_SECRET, {
1765
- expiresIn: "1h"
1772
+ expiresIn: "1d"
1766
1773
  });
1767
1774
  const refreshToken = jwt4.sign(
1768
1775
  { sub: user._id.toString() },
@@ -1798,13 +1805,61 @@ function createDashboardRouter(options) {
1798
1805
  }
1799
1806
 
1800
1807
  // src/express/email.routes.ts
1801
- import { Router as Router3 } from "express";
1808
+ import express3, { Router as Router3 } from "express";
1802
1809
  function createEmailRouter(options) {
1803
1810
  const r = Router3();
1811
+ const emailService = new EmailService();
1812
+ r.use(express3.json());
1813
+ r.use(express3.urlencoded({ extended: true }));
1804
1814
  r.get(
1805
1815
  "/verify",
1806
1816
  (req, res) => res.json({ ok: true, token: req.query.token })
1807
1817
  );
1818
+ r.post("/send", async (req, res) => {
1819
+ try {
1820
+ const { userId, to, subject, html, from } = req.body ?? {};
1821
+ if (!to || !subject || !html) {
1822
+ return res.status(400).json({
1823
+ ok: false,
1824
+ error: "BAD_REQUEST",
1825
+ message: "`to`, `subject`, and `html` are required."
1826
+ });
1827
+ }
1828
+ if (userId) {
1829
+ const user = await OrgUser.findOne({ id: userId }).lean();
1830
+ if (!user) {
1831
+ return res.status(404).json({
1832
+ ok: false,
1833
+ error: "NOT_FOUND",
1834
+ message: "User not found."
1835
+ });
1836
+ }
1837
+ const can = emailService.canSend(user?.lastEmailSent || []);
1838
+ if (!can.ok) {
1839
+ return res.status(429).json({
1840
+ ok: false,
1841
+ error: can.reason,
1842
+ waitMs: can.waitMs,
1843
+ message: "Too many emails sent recently. Please retry later."
1844
+ });
1845
+ }
1846
+ }
1847
+ await emailService.send(to, subject, html, from);
1848
+ if (userId) {
1849
+ await OrgUser.updateOne(
1850
+ { id: userId },
1851
+ { $push: { lastEmailSent: /* @__PURE__ */ new Date() } }
1852
+ );
1853
+ }
1854
+ return res.json({ ok: true });
1855
+ } catch (err) {
1856
+ return res.status(500).json({
1857
+ ok: false,
1858
+ error: "INTERNAL",
1859
+ message: err?.message ?? "Error"
1860
+ });
1861
+ }
1862
+ });
1808
1863
  return r;
1809
1864
  }
1810
1865
 
@@ -1911,7 +1966,7 @@ function createProjectsRouter(options) {
1911
1966
  // src/express/admin/admin.routes.ts
1912
1967
  import bcrypt3 from "bcryptjs";
1913
1968
  import { randomUUID as randomUUID3 } from "crypto";
1914
- import express3, { Router as Router5 } from "express";
1969
+ import express4, { Router as Router5 } from "express";
1915
1970
 
1916
1971
  // src/middlewares/requireRole.ts
1917
1972
  function requireRole(...roles) {
@@ -1974,8 +2029,8 @@ function resolveProjectId(req) {
1974
2029
  }
1975
2030
  function createAdminRouter(_options = {}) {
1976
2031
  const r = Router5();
1977
- r.use(express3.json());
1978
- r.use(express3.urlencoded({ extended: true }));
2032
+ r.use(express4.json());
2033
+ r.use(express4.urlencoded({ extended: true }));
1979
2034
  const adminGuards = [requireAuth(), requireRole("platform_admin")];
1980
2035
  r.post(
1981
2036
  "/users",