@tern-secure/backend 1.2.0-canary.v20251002185025 → 1.2.0-canary.v20251002193408

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.js CHANGED
@@ -571,9 +571,6 @@ function createFireApi(options) {
571
571
  };
572
572
  }
573
573
 
574
- // src/tokens/request.ts
575
- var import_cookie2 = require("@tern-secure/shared/cookie");
576
-
577
574
  // src/utils/errors.ts
578
575
  var TokenVerificationErrorReason = {
579
576
  TokenExpired: "token-expired",
@@ -622,6 +619,72 @@ function mergePreDefinedOptions(userOptions = {}) {
622
619
  };
623
620
  }
624
621
 
622
+ // src/tokens/c-authenticateRequestProcessor.ts
623
+ var RequestProcessorContext = class {
624
+ constructor(ternSecureRequest, options) {
625
+ this.ternSecureRequest = ternSecureRequest;
626
+ this.options = options;
627
+ this.initHeaderValues();
628
+ this.initCookieValues();
629
+ this.initUrlValues();
630
+ Object.assign(this, options);
631
+ this.ternUrl = this.ternSecureRequest.ternUrl;
632
+ }
633
+ get request() {
634
+ return this.ternSecureRequest;
635
+ }
636
+ initHeaderValues() {
637
+ this.sessionTokenInHeader = this.parseAuthorizationHeader(
638
+ this.getHeader(constants.Headers.Authorization)
639
+ );
640
+ this.origin = this.getHeader(constants.Headers.Origin);
641
+ this.host = this.getHeader(constants.Headers.Host);
642
+ this.forwardedHost = this.getHeader(constants.Headers.ForwardedHost);
643
+ this.forwardedProto = this.getHeader(constants.Headers.CloudFrontForwardedProto) || this.getHeader(constants.Headers.ForwardedProto);
644
+ this.referrer = this.getHeader(constants.Headers.Referrer);
645
+ this.userAgent = this.getHeader(constants.Headers.UserAgent);
646
+ this.secFetchDest = this.getHeader(constants.Headers.SecFetchDest);
647
+ this.accept = this.getHeader(constants.Headers.Accept);
648
+ }
649
+ initCookieValues() {
650
+ const isProduction = process.env.NODE_ENV === "production";
651
+ const defaultPrefix = isProduction ? "__HOST-" : "__dev_";
652
+ this.sessionTokenInCookie = this.getCookie(constants.Cookies.Session);
653
+ this.idTokenInCookie = this.getCookie(`${defaultPrefix}${constants.Cookies.IdToken}`);
654
+ this.refreshTokenInCookie = this.getCookie(`${defaultPrefix}${constants.Cookies.Refresh}`);
655
+ this.csrfTokenInCookie = this.getCookie(constants.Cookies.CsrfToken);
656
+ this.customTokenInCookie = this.getCookie(constants.Cookies.Custom);
657
+ }
658
+ initUrlValues() {
659
+ this.method = this.ternSecureRequest.method;
660
+ this.pathSegments = this.ternSecureRequest.ternUrl.pathname.split("/").filter(Boolean);
661
+ this.endpoint = this.pathSegments[2];
662
+ this.subEndpoint = this.pathSegments[3];
663
+ }
664
+ getHeader(name) {
665
+ return this.ternSecureRequest.headers.get(name) || void 0;
666
+ }
667
+ getCookie(name) {
668
+ return this.ternSecureRequest.cookies.get(name) || void 0;
669
+ }
670
+ parseAuthorizationHeader(authorizationHeader) {
671
+ if (!authorizationHeader) {
672
+ return void 0;
673
+ }
674
+ const [scheme, token] = authorizationHeader.split(" ", 2);
675
+ if (!token) {
676
+ return scheme;
677
+ }
678
+ if (scheme === "Bearer") {
679
+ return token;
680
+ }
681
+ return void 0;
682
+ }
683
+ };
684
+ var createRequestProcessor = (ternSecureRequest, options) => {
685
+ return new RequestProcessorContext(ternSecureRequest, options);
686
+ };
687
+
625
688
  // src/jwt/verifyJwt.ts
626
689
  var import_jose2 = require("jose");
627
690
 
@@ -1012,66 +1075,31 @@ async function verifyToken(token, options) {
1012
1075
  }
1013
1076
 
1014
1077
  // src/tokens/request.ts
1015
- var BEARER_PREFIX = "Bearer ";
1016
- function extractTokenFromHeader(request) {
1017
- const authHeader = request.headers.get("Authorization");
1018
- if (!authHeader || !authHeader.startsWith(BEARER_PREFIX)) {
1019
- return null;
1020
- }
1021
- return authHeader.slice(BEARER_PREFIX.length);
1022
- }
1023
- function extractTokenFromCookie(request) {
1024
- const cookieHeader = request.headers.get("Cookie") || void 0;
1025
- if (!cookieHeader) {
1026
- return null;
1027
- }
1028
- const cookiePrefix = (0, import_cookie2.getCookiePrefix)();
1029
- const idTokenCookieName = (0, import_cookie2.getCookieName)(
1030
- constants.Cookies.IdToken,
1031
- cookiePrefix
1032
- );
1033
- const cookies = cookieHeader.split(";").reduce(
1034
- (acc, cookie) => {
1035
- const [name, value] = cookie.trim().split("=");
1036
- acc[name] = value;
1037
- return acc;
1038
- },
1039
- {}
1040
- );
1041
- console.log("Extracted cookies:", cookies[idTokenCookieName]);
1042
- return cookies[idTokenCookieName] || null;
1043
- }
1044
1078
  function hasAuthorizationHeader(request) {
1045
1079
  return request.headers.has("Authorization");
1046
1080
  }
1047
1081
  async function authenticateRequest(request, options) {
1082
+ const context = createRequestProcessor(createTernSecureRequest(request), options);
1048
1083
  async function authenticateRequestWithTokenInCookie() {
1049
- const token = extractTokenFromCookie(request);
1050
- if (!token) {
1051
- return signedOut(AuthErrorReason.SessionTokenMissing);
1052
- }
1053
1084
  try {
1054
- const { data, errors } = await verifyToken(token, options);
1085
+ const { data, errors } = await verifyToken(context.idTokenInCookie, options);
1055
1086
  if (errors) {
1056
1087
  throw errors[0];
1057
1088
  }
1058
- const signedInRequestState = signedIn(data, void 0, token);
1089
+ const signedInRequestState = signedIn(data, void 0, context.idTokenInCookie);
1059
1090
  return signedInRequestState;
1060
1091
  } catch (err) {
1061
1092
  return handleError(err, "cookie");
1062
1093
  }
1063
1094
  }
1064
1095
  async function authenticateRequestWithTokenInHeader() {
1065
- const token = extractTokenFromHeader(request);
1066
- if (!token) {
1067
- return signedOut(AuthErrorReason.SessionTokenMissing, "");
1068
- }
1096
+ const { sessionTokenInHeader } = context;
1069
1097
  try {
1070
- const { data, errors } = await verifyToken(token, options);
1098
+ const { data, errors } = await verifyToken(sessionTokenInHeader, options);
1071
1099
  if (errors) {
1072
1100
  throw errors[0];
1073
1101
  }
1074
- const signedInRequestState = signedIn(data, void 0, token);
1102
+ const signedInRequestState = signedIn(data, void 0, sessionTokenInHeader);
1075
1103
  return signedInRequestState;
1076
1104
  } catch (err) {
1077
1105
  return handleError(err, "header");
@@ -1128,16 +1156,16 @@ function mergePreDefinedOptions2(preDefinedOptions, options) {
1128
1156
  { ...preDefinedOptions }
1129
1157
  );
1130
1158
  }
1131
- var BEARER_PREFIX2 = "Bearer ";
1159
+ var BEARER_PREFIX = "Bearer ";
1132
1160
  var AUTH_COOKIE_NAME = "_session_cookie";
1133
- function extractTokenFromHeader2(request) {
1161
+ function extractTokenFromHeader(request) {
1134
1162
  const authHeader = request.headers.get("Authorization");
1135
- if (!authHeader || !authHeader.startsWith(BEARER_PREFIX2)) {
1163
+ if (!authHeader || !authHeader.startsWith(BEARER_PREFIX)) {
1136
1164
  return null;
1137
1165
  }
1138
- return authHeader.slice(BEARER_PREFIX2.length);
1166
+ return authHeader.slice(BEARER_PREFIX.length);
1139
1167
  }
1140
- function extractTokenFromCookie2(request) {
1168
+ function extractTokenFromCookie(request) {
1141
1169
  const cookieHeader = request.headers.get("Cookie") || void 0;
1142
1170
  if (!cookieHeader) {
1143
1171
  return null;
@@ -1157,7 +1185,7 @@ function hasAuthorizationHeader2(request) {
1157
1185
  }
1158
1186
  async function authenticateRequest2(request, options) {
1159
1187
  async function authenticateRequestWithTokenInCookie() {
1160
- const token = extractTokenFromCookie2(request);
1188
+ const token = extractTokenFromCookie(request);
1161
1189
  if (!token) {
1162
1190
  return signedOut(AuthErrorReason.SessionTokenMissing);
1163
1191
  }
@@ -1169,7 +1197,7 @@ async function authenticateRequest2(request, options) {
1169
1197
  return signedInRequestState;
1170
1198
  }
1171
1199
  async function authenticateRequestWithTokenInHeader() {
1172
- const token = extractTokenFromHeader2(request);
1200
+ const token = extractTokenFromHeader(request);
1173
1201
  if (!token) {
1174
1202
  return signedOut(AuthErrorReason.SessionTokenMissing);
1175
1203
  }