effortless-aws 0.28.0 → 0.30.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.
@@ -280,10 +280,7 @@ var createAuthRuntime = (secret, defaultExpiresIn, apiTokenVerify, apiTokenHeade
280
280
  return headerValue;
281
281
  };
282
282
  const buildHelpers = (sessionData) => ({
283
- createSession(...args) {
284
- const hasData = args.length > 0 && (typeof args[0] === "object" && args[0] !== null && !("expiresIn" in args[0]));
285
- const data = hasData ? args[0] : void 0;
286
- const options = hasData ? args[1] : args[0];
283
+ createSession(data, options) {
287
284
  const seconds = options?.expiresIn ? toSeconds(options.expiresIn) : defaultExpiresIn;
288
285
  const exp = Math.floor(Date.now() / 1e3) + seconds;
289
286
  const payload = Buffer.from(JSON.stringify({ exp, ...data }), "utf-8").toString("base64url");
@@ -308,7 +305,7 @@ var createAuthRuntime = (secret, defaultExpiresIn, apiTokenVerify, apiTokenHeade
308
305
  session: sessionData
309
306
  });
310
307
  return {
311
- async forRequest(cookieValue, authHeader, deps) {
308
+ async forRequest(cookieValue, authHeader) {
312
309
  if (authHeader && apiTokenVerify) {
313
310
  const tokenValue = extractTokenValue(authHeader);
314
311
  if (tokenCache) {
@@ -317,7 +314,7 @@ var createAuthRuntime = (secret, defaultExpiresIn, apiTokenVerify, apiTokenHeade
317
314
  return buildHelpers(cached.session);
318
315
  }
319
316
  }
320
- const session = await apiTokenVerify(tokenValue, { deps });
317
+ const session = await apiTokenVerify({ value: tokenValue });
321
318
  if (tokenCache && apiTokenCacheTtlSeconds) {
322
319
  tokenCache.set(tokenValue, { session, expiresAt: Date.now() + apiTokenCacheTtlSeconds * 1e3 });
323
320
  }
@@ -501,7 +498,6 @@ var getParameters = async (names) => {
501
498
  // src/runtime/handler-utils.ts
502
499
  var ENV_DEP_PREFIX = "EFF_DEP_";
503
500
  var ENV_PARAM_PREFIX = "EFF_PARAM_";
504
- var ENV_AUTH_SECRET = "EFF_AUTH_SECRET";
505
501
  var LOG_RANK = { error: 0, info: 1, debug: 2 };
506
502
  var truncate = (value, maxLength = 4096) => {
507
503
  if (value === void 0 || value === null) return value;
@@ -577,23 +573,25 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
577
573
  resolvedParams = await buildParams(handler.config);
578
574
  return resolvedParams;
579
575
  };
580
- const getAuthRuntime = async () => {
576
+ const getAuthRuntime = async (setupCtx) => {
581
577
  if (resolvedAuthRuntime !== null) return resolvedAuthRuntime;
582
- const ssmPath = process.env[ENV_AUTH_SECRET];
583
- if (!ssmPath || !handler.auth) {
578
+ const authOpts = setupCtx && typeof setupCtx === "object" && "auth" in setupCtx ? setupCtx.auth : void 0;
579
+ if (!authOpts?.secret) {
584
580
  resolvedAuthRuntime = void 0;
585
581
  return void 0;
586
582
  }
587
- const values = await getParameters([ssmPath]);
588
- const secret = values.get(ssmPath);
589
- if (!secret) throw new Error(`Auth secret not found at ${ssmPath}`);
590
- const defaultExpires = handler.auth.expiresIn ? toSeconds(handler.auth.expiresIn) : 604800;
591
- const cacheTtlSeconds = handler.apiToken?.cacheTtl ? toSeconds(handler.apiToken.cacheTtl) : void 0;
583
+ const secret = authOpts.secret;
584
+ resolvedAuthHeaderName = authOpts.apiToken?.header;
585
+ const defaultExpires = authOpts.expiresIn ? toSeconds(authOpts.expiresIn) : 604800;
586
+ const apiToken = authOpts.apiToken;
587
+ const cacheTtlSeconds = apiToken?.cacheTtl ? toSeconds(apiToken.cacheTtl) : void 0;
588
+ const rawVerify = apiToken?.verify;
589
+ const wrappedVerify = rawVerify ? (args) => rawVerify(args.value) : void 0;
592
590
  resolvedAuthRuntime = createAuthRuntime(
593
591
  secret,
594
592
  defaultExpires,
595
- handler.apiToken?.verify,
596
- handler.apiToken?.header,
593
+ wrappedVerify,
594
+ apiToken?.header,
597
595
  cacheTtlSeconds
598
596
  );
599
597
  return resolvedAuthRuntime;
@@ -603,7 +601,7 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
603
601
  if (handler.setup) {
604
602
  const params = await getParams();
605
603
  const deps = getDeps();
606
- const args = {};
604
+ const args = { enableAuth: (opts) => opts };
607
605
  if (params) args.config = params;
608
606
  if (deps) args.deps = deps;
609
607
  if (handler.static) args.files = staticFiles;
@@ -612,7 +610,8 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
612
610
  }
613
611
  return ctx;
614
612
  };
615
- const commonArgs = async (cookieValue, authHeader) => {
613
+ let resolvedAuthHeaderName;
614
+ const commonArgs = async (cookieValue, authHeader, headers) => {
616
615
  const args = {};
617
616
  if (handler.setup) args.ctx = await getSetup();
618
617
  const deps = getDeps();
@@ -620,9 +619,13 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
620
619
  const params = await getParams();
621
620
  if (params) args.config = params;
622
621
  if (handler.static) args.files = staticFiles;
623
- const authRuntime = await getAuthRuntime();
622
+ const authRuntime = await getAuthRuntime(args.ctx);
624
623
  if (authRuntime) {
625
- args.auth = await authRuntime.forRequest(cookieValue, authHeader, deps);
624
+ let finalAuthHeader = authHeader;
625
+ if (finalAuthHeader === void 0 && headers && resolvedAuthHeaderName) {
626
+ finalAuthHeader = headers[resolvedAuthHeaderName] ?? headers[resolvedAuthHeaderName.toLowerCase()] ?? void 0;
627
+ }
628
+ args.auth = await authRuntime.forRequest(cookieValue, finalAuthHeader);
626
629
  }
627
630
  return args;
628
631
  };