@digilogiclabs/platform-core 1.20.1 → 1.21.0

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 (42) hide show
  1. package/dist/{ConsoleEmail-CO0OM4UT.d.ts → ConsoleEmail-BbdpINKx.d.ts} +4 -46
  2. package/dist/{ConsoleEmail-DDB28vyS.d.mts → ConsoleEmail-DpxhsG9C.d.mts} +4 -46
  3. package/dist/{IAI-D8wA_i8N.d.mts → IAI-CWd11dzL.d.mts} +1 -105
  4. package/dist/{IAI-D8wA_i8N.d.ts → IAI-CWd11dzL.d.ts} +1 -105
  5. package/dist/IEmail-CsOcaQcb.d.mts +45 -0
  6. package/dist/IEmail-CsOcaQcb.d.ts +45 -0
  7. package/dist/ILogger-DLDBZaH4.d.mts +105 -0
  8. package/dist/ILogger-DLDBZaH4.d.ts +105 -0
  9. package/dist/{agents-Cc65YUoW.d.ts → agents-C42rGwRl.d.ts} +2 -1
  10. package/dist/{agents-DGciJI27.d.mts → agents-CES-Aq1K.d.mts} +2 -1
  11. package/dist/agents.d.mts +3 -2
  12. package/dist/agents.d.ts +3 -2
  13. package/dist/auth.d.mts +238 -1
  14. package/dist/auth.d.ts +238 -1
  15. package/dist/auth.js +198 -0
  16. package/dist/auth.js.map +1 -1
  17. package/dist/auth.mjs +183 -0
  18. package/dist/auth.mjs.map +1 -1
  19. package/dist/circuit-breaker-CP6kw0P5.d.ts +351 -0
  20. package/dist/circuit-breaker-CsQivd57.d.mts +351 -0
  21. package/dist/email-templates.d.mts +106 -1
  22. package/dist/email-templates.d.ts +106 -1
  23. package/dist/email-templates.js +608 -0
  24. package/dist/email-templates.js.map +1 -1
  25. package/dist/email-templates.mjs +596 -0
  26. package/dist/email-templates.mjs.map +1 -1
  27. package/dist/index.d.mts +12 -355
  28. package/dist/index.d.ts +12 -355
  29. package/dist/index.js +10 -7
  30. package/dist/index.js.map +1 -1
  31. package/dist/index.mjs +10 -7
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/migrate.js +0 -0
  34. package/dist/pdf-exports/index.d.mts +63 -3
  35. package/dist/pdf-exports/index.d.ts +63 -3
  36. package/dist/pdf-exports/index.js +21 -4
  37. package/dist/pdf-exports/index.js.map +1 -1
  38. package/dist/pdf-exports/index.mjs +20 -4
  39. package/dist/pdf-exports/index.mjs.map +1 -1
  40. package/dist/testing.d.mts +5 -3
  41. package/dist/testing.d.ts +5 -3
  42. package/package.json +11 -11
package/dist/auth.mjs CHANGED
@@ -1826,6 +1826,184 @@ function computeClaimDelta(current, newEmail, newName) {
1826
1826
  return changed ? delta : null;
1827
1827
  }
1828
1828
 
1829
+ // src/auth/tokens.ts
1830
+ import crypto2 from "crypto";
1831
+ var HMAC_HEX_CHARS = 16;
1832
+ function validateSecret(secret) {
1833
+ if (typeof secret !== "string" || secret.length < 16) {
1834
+ throw new Error(
1835
+ "signToken/verifyToken require a secret of at least 16 characters"
1836
+ );
1837
+ }
1838
+ }
1839
+ function computeHmac(secret, signedPart) {
1840
+ return crypto2.createHmac("sha256", secret).update(signedPart).digest("hex").slice(0, HMAC_HEX_CHARS);
1841
+ }
1842
+ function signToken(opts) {
1843
+ validateSecret(opts.secret);
1844
+ if (typeof opts.payload !== "string" || opts.payload.length === 0) {
1845
+ throw new Error("signToken requires a non-empty payload string");
1846
+ }
1847
+ const signedPart = opts.ttlSeconds !== void 0 ? `${opts.payload}.${Math.floor(Date.now() / 1e3)}` : opts.payload;
1848
+ const encoded = Buffer.from(signedPart, "utf-8").toString("base64url");
1849
+ const hmac = computeHmac(opts.secret, signedPart);
1850
+ return `${encoded}.${hmac}`;
1851
+ }
1852
+ function verifyToken(token, opts) {
1853
+ validateSecret(opts.secret);
1854
+ if (typeof token !== "string" || token.length === 0) {
1855
+ return { valid: false, reason: "malformed" };
1856
+ }
1857
+ const lastDot = token.lastIndexOf(".");
1858
+ if (lastDot <= 0 || lastDot === token.length - 1) {
1859
+ return { valid: false, reason: "malformed" };
1860
+ }
1861
+ const encoded = token.slice(0, lastDot);
1862
+ const providedHmac = token.slice(lastDot + 1);
1863
+ let signedPart;
1864
+ try {
1865
+ signedPart = Buffer.from(encoded, "base64url").toString("utf-8");
1866
+ } catch {
1867
+ return { valid: false, reason: "malformed" };
1868
+ }
1869
+ if (signedPart.length === 0) {
1870
+ return { valid: false, reason: "malformed" };
1871
+ }
1872
+ const expectedHmac = computeHmac(opts.secret, signedPart);
1873
+ if (providedHmac.length !== expectedHmac.length || !crypto2.timingSafeEqual(
1874
+ Buffer.from(providedHmac, "utf-8"),
1875
+ Buffer.from(expectedHmac, "utf-8")
1876
+ )) {
1877
+ return { valid: false, reason: "bad_signature" };
1878
+ }
1879
+ const tsDot = signedPart.lastIndexOf(".");
1880
+ let payload = signedPart;
1881
+ let issuedAt;
1882
+ if (tsDot > 0) {
1883
+ const tsCandidate = signedPart.slice(tsDot + 1);
1884
+ const tsNum = Number(tsCandidate);
1885
+ if (Number.isFinite(tsNum) && /^\d+$/.test(tsCandidate)) {
1886
+ payload = signedPart.slice(0, tsDot);
1887
+ issuedAt = tsNum;
1888
+ }
1889
+ }
1890
+ if (opts.maxAgeSeconds !== void 0 && issuedAt !== void 0) {
1891
+ const ageSeconds = Math.floor(Date.now() / 1e3) - issuedAt;
1892
+ if (ageSeconds > opts.maxAgeSeconds) {
1893
+ return { valid: false, reason: "expired" };
1894
+ }
1895
+ }
1896
+ return issuedAt !== void 0 ? { valid: true, payload, issuedAt } : { valid: true, payload };
1897
+ }
1898
+
1899
+ // src/auth/redirect.ts
1900
+ var DANGEROUS_PROTOCOLS = /* @__PURE__ */ new Set([
1901
+ "javascript:",
1902
+ "data:",
1903
+ "vbscript:",
1904
+ "file:"
1905
+ ]);
1906
+ function safeRedirect(candidate, opts) {
1907
+ if (typeof candidate !== "string" || candidate.length === 0) {
1908
+ return opts.fallback;
1909
+ }
1910
+ const trimmed = candidate.trim();
1911
+ if (trimmed.length === 0) {
1912
+ return opts.fallback;
1913
+ }
1914
+ if (trimmed.startsWith("//")) {
1915
+ return opts.fallback;
1916
+ }
1917
+ if (trimmed.startsWith("/")) {
1918
+ return trimmed;
1919
+ }
1920
+ let url;
1921
+ try {
1922
+ url = new URL(trimmed);
1923
+ } catch {
1924
+ return opts.fallback;
1925
+ }
1926
+ if (DANGEROUS_PROTOCOLS.has(url.protocol.toLowerCase())) {
1927
+ return opts.fallback;
1928
+ }
1929
+ const candidateOrigin = url.origin.toLowerCase();
1930
+ const allowed = opts.allowedOrigins.some(
1931
+ (o) => o.toLowerCase() === candidateOrigin
1932
+ );
1933
+ if (!allowed) {
1934
+ return opts.fallback;
1935
+ }
1936
+ return url.toString();
1937
+ }
1938
+
1939
+ // src/auth/honeypot.ts
1940
+ var DEFAULT_FIELD_NAME = "_hp";
1941
+ function isHoneypotFilled(body, fieldName = DEFAULT_FIELD_NAME) {
1942
+ if (body == null) return false;
1943
+ if (typeof FormData !== "undefined" && body instanceof FormData) {
1944
+ const value2 = body.get(fieldName);
1945
+ return typeof value2 === "string" && value2.trim().length > 0;
1946
+ }
1947
+ if (typeof body !== "object") return false;
1948
+ const value = body[fieldName];
1949
+ return typeof value === "string" && value.trim().length > 0;
1950
+ }
1951
+
1952
+ // src/auth/health-route.ts
1953
+ var DEFAULT_TIMEOUT_MS = 2e3;
1954
+ async function withTimeoutOrError(probe, timeoutMs) {
1955
+ try {
1956
+ const result = await Promise.race([
1957
+ probe(),
1958
+ new Promise(
1959
+ (_, reject) => setTimeout(() => reject(new Error("probe timed out")), timeoutMs)
1960
+ )
1961
+ ]);
1962
+ return result;
1963
+ } catch {
1964
+ return { status: "error" };
1965
+ }
1966
+ }
1967
+ function createHealthRoute(opts) {
1968
+ const names = Object.keys(opts.checks);
1969
+ return async function GET() {
1970
+ const probeResults = await Promise.all(
1971
+ names.map(async (name) => {
1972
+ const check = opts.checks[name];
1973
+ const result = await withTimeoutOrError(
1974
+ check.probe,
1975
+ check.timeoutMs ?? DEFAULT_TIMEOUT_MS
1976
+ );
1977
+ return [name, result];
1978
+ })
1979
+ );
1980
+ const services = {};
1981
+ let degraded = false;
1982
+ for (const [name, result] of probeResults) {
1983
+ services[name] = result;
1984
+ const check = opts.checks[name];
1985
+ if (check.gating && result.status === "error") {
1986
+ degraded = true;
1987
+ }
1988
+ }
1989
+ const body = {
1990
+ status: degraded ? "degraded" : "healthy",
1991
+ services
1992
+ };
1993
+ if (opts.extraBody) {
1994
+ try {
1995
+ const extra = await opts.extraBody();
1996
+ Object.assign(body, extra);
1997
+ } catch {
1998
+ }
1999
+ }
2000
+ return new Response(JSON.stringify(body), {
2001
+ status: degraded ? 503 : 200,
2002
+ headers: { "content-type": "application/json" }
2003
+ });
2004
+ };
2005
+ }
2006
+
1829
2007
  // src/env.ts
1830
2008
  function getRequiredEnv(key) {
1831
2009
  const value = process.env[key];
@@ -1958,6 +2136,7 @@ export {
1958
2136
  createBetaClient,
1959
2137
  createEnsureUser,
1960
2138
  createFeatureFlags,
2139
+ createHealthRoute,
1961
2140
  createLazyRateLimitStore,
1962
2141
  createMemoryRateLimitStore,
1963
2142
  createRedisRateLimitStore,
@@ -1989,6 +2168,7 @@ export {
1989
2168
  hasRole,
1990
2169
  isAllowlisted,
1991
2170
  isApiError,
2171
+ isHoneypotFilled,
1992
2172
  isTokenExpired,
1993
2173
  isValidBearerToken,
1994
2174
  parseKeycloakRoles,
@@ -1997,8 +2177,10 @@ export {
1997
2177
  resetRateLimitForKey,
1998
2178
  resolveIdentifier,
1999
2179
  resolveRateLimitIdentifier,
2180
+ safeRedirect,
2000
2181
  safeValidate,
2001
2182
  sanitizeApiError,
2183
+ signToken,
2002
2184
  storeBetaCode,
2003
2185
  stripHtml,
2004
2186
  stripNullBytes,
@@ -2006,6 +2188,7 @@ export {
2006
2188
  validateBetaCode,
2007
2189
  validateEnvVars,
2008
2190
  verifyCronAuth,
2191
+ verifyToken,
2009
2192
  zodErrorResponse
2010
2193
  };
2011
2194
  //# sourceMappingURL=auth.mjs.map