@tern-secure/backend 1.2.0-canary.v20250919134427 → 1.2.0-canary.v20250926170202

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 (56) hide show
  1. package/dist/admin/index.js +207 -180
  2. package/dist/admin/index.js.map +1 -1
  3. package/dist/admin/index.mjs +152 -105
  4. package/dist/admin/index.mjs.map +1 -1
  5. package/dist/admin/nextSessionTernSecure.d.ts.map +1 -1
  6. package/dist/admin/sessionTernSecure.d.ts +24 -5
  7. package/dist/admin/sessionTernSecure.d.ts.map +1 -1
  8. package/dist/{chunk-ZIO4EKS5.mjs → chunk-ZMDLKXUP.mjs} +6 -29
  9. package/dist/chunk-ZMDLKXUP.mjs.map +1 -0
  10. package/dist/constants.d.ts +4 -5
  11. package/dist/constants.d.ts.map +1 -1
  12. package/dist/fireRestApi/createFireApi.d.ts +12 -0
  13. package/dist/fireRestApi/createFireApi.d.ts.map +1 -0
  14. package/dist/fireRestApi/endpointUrl.d.ts +6 -0
  15. package/dist/fireRestApi/endpointUrl.d.ts.map +1 -0
  16. package/dist/fireRestApi/endpoints/AbstractApi.d.ts +7 -0
  17. package/dist/fireRestApi/endpoints/AbstractApi.d.ts.map +1 -0
  18. package/dist/fireRestApi/endpoints/EmailApi.d.ts +14 -0
  19. package/dist/fireRestApi/endpoints/EmailApi.d.ts.map +1 -0
  20. package/dist/fireRestApi/endpoints/PasswordApi.d.ts +20 -0
  21. package/dist/fireRestApi/endpoints/PasswordApi.d.ts.map +1 -0
  22. package/dist/fireRestApi/endpoints/SignInTokenApi.d.ts +11 -0
  23. package/dist/fireRestApi/endpoints/SignInTokenApi.d.ts.map +1 -0
  24. package/dist/fireRestApi/endpoints/SignUpApi.d.ts +11 -0
  25. package/dist/fireRestApi/endpoints/SignUpApi.d.ts.map +1 -0
  26. package/dist/fireRestApi/endpoints/TokenApi.d.ts +15 -0
  27. package/dist/fireRestApi/endpoints/TokenApi.d.ts.map +1 -0
  28. package/dist/fireRestApi/endpoints/index.d.ts +6 -0
  29. package/dist/fireRestApi/endpoints/index.d.ts.map +1 -0
  30. package/dist/fireRestApi/index.d.ts +2 -0
  31. package/dist/fireRestApi/index.d.ts.map +1 -0
  32. package/dist/fireRestApi/request.d.ts +37 -0
  33. package/dist/fireRestApi/request.d.ts.map +1 -0
  34. package/dist/fireRestApi/resources/JSON.d.ts +44 -0
  35. package/dist/fireRestApi/resources/JSON.d.ts.map +1 -0
  36. package/dist/fireRestApi/resources/Token.d.ts +7 -0
  37. package/dist/fireRestApi/resources/Token.d.ts.map +1 -0
  38. package/dist/index.js +178 -66
  39. package/dist/index.js.map +1 -1
  40. package/dist/index.mjs +161 -38
  41. package/dist/index.mjs.map +1 -1
  42. package/dist/instance/backendFireInstance.d.ts +4 -4
  43. package/dist/instance/backendFireInstance.d.ts.map +1 -1
  44. package/dist/instance/backendInstanceEdge.d.ts +2 -2
  45. package/dist/instance/backendInstanceEdge.d.ts.map +1 -1
  46. package/dist/tokens/authstate.d.ts +1 -1
  47. package/dist/tokens/authstate.d.ts.map +1 -1
  48. package/dist/tokens/request.d.ts +3 -3
  49. package/dist/tokens/request.d.ts.map +1 -1
  50. package/dist/tokens/requestFire.d.ts.map +1 -1
  51. package/dist/tokens/types.d.ts +4 -2
  52. package/dist/tokens/types.d.ts.map +1 -1
  53. package/package.json +3 -3
  54. package/dist/admin/gemini.sessionTernSecure.d.ts +0 -8
  55. package/dist/admin/gemini.sessionTernSecure.d.ts.map +0 -1
  56. package/dist/chunk-ZIO4EKS5.mjs.map +0 -1
package/dist/index.js CHANGED
@@ -54,11 +54,10 @@ var Attributes = {
54
54
  };
55
55
  var Cookies = {
56
56
  Session: "__session",
57
- IdToken: "_tern",
58
- CsrfToken: "_session_terncf",
59
- SessionCookie: "_session_cookie",
60
- SessionToken: "_session_token",
61
- Refresh: "__refresh",
57
+ CsrfToken: "__session_terncf",
58
+ IdToken: "FIREBASE_[DEFAULT]",
59
+ Refresh: "FIREBASEID_[DEFAULT]",
60
+ Custom: "__custom",
62
61
  Handshake: "__ternsecure_handshake",
63
62
  DevBrowser: "__ternsecure_db_jwt",
64
63
  RedirectCount: "__ternsecure_redirect_count",
@@ -243,10 +242,11 @@ function signedIn(sessionClaims, headers = new Headers(), token) {
243
242
  headers
244
243
  };
245
244
  }
246
- function signedOut(reason, headers = new Headers()) {
245
+ function signedOut(reason, message = "", headers = new Headers()) {
247
246
  return decorateHeaders({
248
247
  status: AuthStatus.SignedOut,
249
248
  reason,
249
+ message,
250
250
  isSignedIn: false,
251
251
  auth: () => signedOutAuthObject(),
252
252
  token: null,
@@ -271,17 +271,111 @@ var decorateHeaders = (requestState) => {
271
271
  return requestState;
272
272
  };
273
273
 
274
- // src/api/endpoints/SessionApi.ts
275
- var rootPath = "/sessions";
276
- var SessionApi = class {
274
+ // src/fireRestApi/endpoints/AbstractApi.ts
275
+ var AbstractAPI = class {
277
276
  constructor(request) {
278
277
  this.request = request;
279
278
  }
280
- async createSession(params) {
279
+ requireApiKey(apiKey) {
280
+ if (!apiKey) {
281
+ throw new Error("A valid API key is required.");
282
+ }
283
+ }
284
+ };
285
+
286
+ // src/fireRestApi/endpoints/EmailApi.ts
287
+ var rootPath = "/customTokens";
288
+ var EmailApi = class extends AbstractAPI {
289
+ async verifyEmailVerification(apiKey, params) {
290
+ this.requireApiKey(apiKey);
291
+ const { ...restParams } = params;
292
+ return this.request({
293
+ method: "POST",
294
+ path: `${rootPath}`,
295
+ bodyParams: restParams
296
+ });
297
+ }
298
+ async confirmEmailVerification(apiKey, params) {
299
+ this.requireApiKey(apiKey);
300
+ const { ...restParams } = params;
301
+ return this.request({
302
+ method: "POST",
303
+ path: `${rootPath}`,
304
+ bodyParams: restParams
305
+ });
306
+ }
307
+ };
308
+
309
+ // src/fireRestApi/endpoints/PasswordApi.ts
310
+ var rootPath2 = "/customTokens";
311
+ var PasswordApi = class extends AbstractAPI {
312
+ async verifyPasswordResetCode(apiKey, params) {
313
+ this.requireApiKey(apiKey);
314
+ const { ...restParams } = params;
281
315
  return this.request({
282
316
  method: "POST",
283
- path: rootPath,
284
- bodyParams: params
317
+ path: `${rootPath2}`,
318
+ bodyParams: restParams
319
+ });
320
+ }
321
+ async confirmPasswordReset(apiKey, params) {
322
+ this.requireApiKey(apiKey);
323
+ const { ...restParams } = params;
324
+ return this.request({
325
+ method: "POST",
326
+ path: `${rootPath2}`,
327
+ bodyParams: restParams
328
+ });
329
+ }
330
+ async changePassword(apiKey, params) {
331
+ this.requireApiKey(apiKey);
332
+ const { ...restParams } = params;
333
+ return this.request({
334
+ method: "POST",
335
+ path: `${rootPath2}`,
336
+ bodyParams: restParams
337
+ });
338
+ }
339
+ };
340
+
341
+ // src/fireRestApi/endpoints/SignInTokenApi.ts
342
+ var rootPath3 = "/customTokens";
343
+ var SignInTokenApi = class extends AbstractAPI {
344
+ async createCustomToken(apiKey, params) {
345
+ this.requireApiKey(apiKey);
346
+ const { ...restParams } = params;
347
+ return this.request({
348
+ method: "POST",
349
+ path: `${rootPath3}`,
350
+ bodyParams: restParams
351
+ });
352
+ }
353
+ };
354
+
355
+ // src/fireRestApi/endpoints/SignUpApi.ts
356
+ var rootPath4 = "/customTokens";
357
+ var SignUpApi = class extends AbstractAPI {
358
+ async createCustomToken(apiKey, params) {
359
+ this.requireApiKey(apiKey);
360
+ const { ...restParams } = params;
361
+ return this.request({
362
+ method: "POST",
363
+ path: `${rootPath4}`,
364
+ bodyParams: restParams
365
+ });
366
+ }
367
+ };
368
+
369
+ // src/fireRestApi/endpoints/TokenApi.ts
370
+ var rootPath5 = "/sessions";
371
+ var TokenApi = class extends AbstractAPI {
372
+ async refreshToken(apiKey, params) {
373
+ this.requireApiKey(apiKey);
374
+ const { ...restParams } = params;
375
+ return this.request({
376
+ method: "POST",
377
+ path: `${rootPath5}/refresh`,
378
+ bodyParams: restParams
285
379
  });
286
380
  }
287
381
  };
@@ -309,10 +403,10 @@ function joinPaths(...args) {
309
403
  return args.filter((p) => p).join(SEPARATOR).replace(MULTIPLE_SEPARATOR_REGEX, SEPARATOR);
310
404
  }
311
405
 
312
- // src/api/request.ts
406
+ // src/fireRestApi/request.ts
313
407
  function createRequest(options) {
314
408
  const requestFn = async (requestOptions) => {
315
- const { apiUrl, apiVersion } = options;
409
+ const { apiKey, apiUrl, apiVersion = "v1" } = options;
316
410
  const { path, method, queryParams, headerParams, bodyParams, formData } = requestOptions;
317
411
  const url = joinPaths(apiUrl, apiVersion, path);
318
412
  const finalUrl = new URL(url);
@@ -394,40 +488,18 @@ function parseError(error) {
394
488
  };
395
489
  }
396
490
 
397
- // src/api/createBackendApi.ts
398
- function createBackendApi(options) {
491
+ // src/fireRestApi/createFireApi.ts
492
+ function createFireApi(options) {
399
493
  const request = createRequest(options);
400
494
  return {
401
- sessions: new SessionApi(request)
495
+ email: new EmailApi(request),
496
+ password: new PasswordApi(request),
497
+ signIn: new SignInTokenApi(request),
498
+ signUp: new SignUpApi(request),
499
+ tokens: new TokenApi(request)
402
500
  };
403
501
  }
404
502
 
405
- // src/utils/options.ts
406
- var defaultOptions = {
407
- apiUrl: void 0,
408
- apiVersion: void 0
409
- };
410
- function mergePreDefinedOptions(userOptions = {}) {
411
- return {
412
- ...defaultOptions,
413
- ...userOptions
414
- };
415
- }
416
-
417
- // src/tokens/sessionConfig.ts
418
- var getSessionConfig = (options) => {
419
- const cookieConfig = options?.cookies?.session_cookie;
420
- return {
421
- COOKIE_NAME: cookieConfig?.name,
422
- DEFAULT_EXPIRES_IN_MS: cookieConfig?.attributes?.maxAge,
423
- DEFAULT_EXPIRES_IN_SECONDS: Math.floor((cookieConfig?.attributes?.maxAge || 0) / 1e3),
424
- REVOKE_REFRESH_TOKENS_ON_SIGNOUT: cookieConfig?.revokeRefreshTokensOnSignOut
425
- };
426
- };
427
-
428
- // src/jwt/verifyJwt.ts
429
- var import_jose2 = require("jose");
430
-
431
503
  // src/utils/errors.ts
432
504
  var TokenVerificationErrorReason = {
433
505
  TokenExpired: "token-expired",
@@ -463,6 +535,21 @@ var TokenVerificationError = class _TokenVerificationError extends Error {
463
535
  }
464
536
  };
465
537
 
538
+ // src/utils/options.ts
539
+ var defaultOptions = {
540
+ apiUrl: void 0,
541
+ apiVersion: void 0
542
+ };
543
+ function mergePreDefinedOptions(userOptions = {}) {
544
+ return {
545
+ ...defaultOptions,
546
+ ...userOptions
547
+ };
548
+ }
549
+
550
+ // src/jwt/verifyJwt.ts
551
+ var import_jose2 = require("jose");
552
+
466
553
  // src/utils/rfc4648.ts
467
554
  var base64url = {
468
555
  parse(string, opts) {
@@ -851,7 +938,6 @@ async function verifyToken(token, options) {
851
938
 
852
939
  // src/tokens/request.ts
853
940
  var BEARER_PREFIX = "Bearer ";
854
- var AUTH_COOKIE_NAME = "_session_cookie";
855
941
  function extractTokenFromHeader(request) {
856
942
  const authHeader = request.headers.get("Authorization");
857
943
  if (!authHeader || !authHeader.startsWith(BEARER_PREFIX)) {
@@ -859,9 +945,8 @@ function extractTokenFromHeader(request) {
859
945
  }
860
946
  return authHeader.slice(BEARER_PREFIX.length);
861
947
  }
862
- function extractTokenFromCookie(request, opts) {
948
+ function extractTokenFromCookie(request) {
863
949
  const cookieHeader = request.headers.get("Cookie") || void 0;
864
- const sessionName = getSessionConfig(opts).COOKIE_NAME;
865
950
  if (!cookieHeader) {
866
951
  return null;
867
952
  }
@@ -873,35 +958,63 @@ function extractTokenFromCookie(request, opts) {
873
958
  },
874
959
  {}
875
960
  );
876
- return cookies[AUTH_COOKIE_NAME] || null;
961
+ return cookies[constants.Cookies.Session] || null;
877
962
  }
878
963
  function hasAuthorizationHeader(request) {
879
964
  return request.headers.has("Authorization");
880
965
  }
881
966
  async function authenticateRequest(request, options) {
967
+ async function refreshToken() {
968
+ try {
969
+ const response = await options.apiClient?.tokens.refreshToken(options.firebaseConfig?.apiKey || "", {
970
+ format: "cookie",
971
+ refresh_token: "",
972
+ expired_token: "",
973
+ request_origin: options.apiUrl || ""
974
+ });
975
+ } catch (error) {
976
+ console.error("Error refreshing token:", error);
977
+ }
978
+ }
882
979
  async function authenticateRequestWithTokenInCookie() {
883
- const token = extractTokenFromCookie(request, options);
980
+ const token = extractTokenFromCookie(request);
884
981
  if (!token) {
885
982
  return signedOut(AuthErrorReason.SessionTokenMissing);
886
983
  }
887
- const { data, errors } = await verifyToken(token, options);
888
- if (errors) {
889
- throw errors[0];
984
+ try {
985
+ const { data, errors } = await verifyToken(token, options);
986
+ if (errors) {
987
+ throw errors[0];
988
+ }
989
+ const signedInRequestState = signedIn(data, void 0, token);
990
+ return signedInRequestState;
991
+ } catch (err) {
992
+ return handleError(err, "cookie");
890
993
  }
891
- const signedInRequestState = signedIn(data, void 0, token);
892
- return signedInRequestState;
893
994
  }
894
995
  async function authenticateRequestWithTokenInHeader() {
895
996
  const token = extractTokenFromHeader(request);
896
997
  if (!token) {
897
- return signedOut(AuthErrorReason.SessionTokenMissing);
998
+ return signedOut(AuthErrorReason.SessionTokenMissing, "");
898
999
  }
899
- const { data, errors } = await verifyToken(token, options);
900
- if (errors) {
901
- throw errors[0];
1000
+ try {
1001
+ const { data, errors } = await verifyToken(token, options);
1002
+ if (errors) {
1003
+ throw errors[0];
1004
+ }
1005
+ const signedInRequestState = signedIn(data, void 0, token);
1006
+ return signedInRequestState;
1007
+ } catch (err) {
1008
+ return handleError(err, "header");
902
1009
  }
903
- const signedInRequestState = signedIn(data, void 0, token);
904
- return signedInRequestState;
1010
+ }
1011
+ async function handleError(err, tokenCarrier) {
1012
+ if (!(err instanceof TokenVerificationError)) {
1013
+ return signedOut(AuthErrorReason.UnexpectedError);
1014
+ }
1015
+ let refreshError;
1016
+ err.tokenCarrier = tokenCarrier;
1017
+ return signedOut(err.reason, err.getFullMessage());
905
1018
  }
906
1019
  if (hasAuthorizationHeader(request)) {
907
1020
  return authenticateRequestWithTokenInHeader();
@@ -923,7 +1036,7 @@ function createAuthenticateRequest(params) {
923
1036
  // src/instance/backendInstanceEdge.ts
924
1037
  function createBackendInstanceClient(options) {
925
1038
  const opts = { ...options };
926
- const apiClient = createBackendApi(opts);
1039
+ const apiClient = createFireApi(opts);
927
1040
  const requestState = createAuthenticateRequest({ options: opts, apiClient });
928
1041
  return {
929
1042
  ...apiClient,
@@ -947,7 +1060,7 @@ function mergePreDefinedOptions2(preDefinedOptions, options) {
947
1060
  );
948
1061
  }
949
1062
  var BEARER_PREFIX2 = "Bearer ";
950
- var AUTH_COOKIE_NAME2 = "_session_cookie";
1063
+ var AUTH_COOKIE_NAME = "_session_cookie";
951
1064
  function extractTokenFromHeader2(request) {
952
1065
  const authHeader = request.headers.get("Authorization");
953
1066
  if (!authHeader || !authHeader.startsWith(BEARER_PREFIX2)) {
@@ -955,9 +1068,8 @@ function extractTokenFromHeader2(request) {
955
1068
  }
956
1069
  return authHeader.slice(BEARER_PREFIX2.length);
957
1070
  }
958
- function extractTokenFromCookie2(request, opts) {
1071
+ function extractTokenFromCookie2(request) {
959
1072
  const cookieHeader = request.headers.get("Cookie") || void 0;
960
- const sessionName = getSessionConfig(opts).COOKIE_NAME;
961
1073
  if (!cookieHeader) {
962
1074
  return null;
963
1075
  }
@@ -969,14 +1081,14 @@ function extractTokenFromCookie2(request, opts) {
969
1081
  },
970
1082
  {}
971
1083
  );
972
- return cookies[AUTH_COOKIE_NAME2] || null;
1084
+ return cookies[AUTH_COOKIE_NAME] || null;
973
1085
  }
974
1086
  function hasAuthorizationHeader2(request) {
975
1087
  return request.headers.has("Authorization");
976
1088
  }
977
1089
  async function authenticateRequest2(request, options) {
978
1090
  async function authenticateRequestWithTokenInCookie() {
979
- const token = extractTokenFromCookie2(request, options);
1091
+ const token = extractTokenFromCookie2(request);
980
1092
  if (!token) {
981
1093
  return signedOut(AuthErrorReason.SessionTokenMissing);
982
1094
  }
@@ -1018,7 +1130,7 @@ function createFireAuthenticateRequest(params) {
1018
1130
  // src/instance/backendFireInstance.ts
1019
1131
  function createFireClient(options) {
1020
1132
  const opts = { ...options };
1021
- const apiClient = createBackendApi(opts);
1133
+ const apiClient = createFireApi(opts);
1022
1134
  const requestState = createFireAuthenticateRequest({ options: opts });
1023
1135
  return {
1024
1136
  ...apiClient,