@monocloud/auth-node-core 0.1.1 → 0.1.2

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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { MonoCloudAuthBaseError, MonoCloudHttpError, MonoCloudOPError, MonoCloudOidcClient, MonoCloudOidcClient as MonoCloudOidcClient$1, MonoCloudTokenError, MonoCloudValidationError, MonoCloudValidationError as MonoCloudValidationError$1 } from "@monocloud/auth-core";
1
+ import { MonoCloudAuthBaseError, MonoCloudHttpError, MonoCloudOPError, MonoCloudOPError as MonoCloudOPError$1, MonoCloudOidcClient, MonoCloudOidcClient as MonoCloudOidcClient$1, MonoCloudTokenError, MonoCloudValidationError, MonoCloudValidationError as MonoCloudValidationError$1 } from "@monocloud/auth-core";
2
2
  import { createRemoteJWKSet, jwtVerify } from "jose";
3
3
  import { ensureLeadingSlash, findToken, getBoolean, getNumber, isAbsoluteUrl, isPresent, isSameHost, now, parseSpaceSeparated, parseSpaceSeparatedSet, removeTrailingSlash, setsEqual } from "@monocloud/auth-core/internal";
4
4
  import { decrypt, decryptAuthState, encrypt, encryptAuthState, generateNonce, generatePKCE, generateState, isUserInGroup, mergeArrays, parseCallbackParams } from "@monocloud/auth-core/utils";
@@ -237,6 +237,7 @@ const DEFAULT_OPTIONS = {
237
237
  scopes: "openid profile email",
238
238
  responseType: "code"
239
239
  },
240
+ allowQueryParamOverrides: false,
240
241
  session: {
241
242
  cookie: {
242
243
  httpOnly: true,
@@ -371,6 +372,7 @@ const optionsSchema = Joi.object({
371
372
  federatedSignOut: boolRequired,
372
373
  userInfo: boolRequired,
373
374
  refetchUserInfo: boolRequired,
375
+ allowQueryParamOverrides: boolRequired,
374
376
  defaultAuthParams: authParamSchema,
375
377
  resources: Joi.array().items(indicatorOptionsSchema).optional(),
376
378
  session: sessionSchema,
@@ -437,6 +439,7 @@ const getOptions = (options, throwOnError = true) => {
437
439
  const MONOCLOUD_AUTH_FEDERATED_SIGNOUT = process.env.MONOCLOUD_AUTH_FEDERATED_SIGNOUT;
438
440
  const MONOCLOUD_AUTH_USER_INFO = process.env.MONOCLOUD_AUTH_USER_INFO;
439
441
  const MONOCLOUD_AUTH_REFETCH_USER_INFO = process.env.MONOCLOUD_AUTH_REFETCH_USER_INFO;
442
+ const MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES = process.env.MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES;
440
443
  const MONOCLOUD_AUTH_SESSION_COOKIE_NAME = process.env.MONOCLOUD_AUTH_SESSION_COOKIE_NAME;
441
444
  const MONOCLOUD_AUTH_SESSION_COOKIE_PATH = process.env.MONOCLOUD_AUTH_SESSION_COOKIE_PATH;
442
445
  const MONOCLOUD_AUTH_SESSION_COOKIE_DOMAIN = process.env.MONOCLOUD_AUTH_SESSION_COOKIE_DOMAIN;
@@ -485,6 +488,7 @@ const getOptions = (options, throwOnError = true) => {
485
488
  federatedSignOut: (options === null || options === void 0 ? void 0 : options.federatedSignOut) ?? getBoolean(MONOCLOUD_AUTH_FEDERATED_SIGNOUT) ?? DEFAULT_OPTIONS.federatedSignOut,
486
489
  userInfo: (options === null || options === void 0 ? void 0 : options.userInfo) ?? getBoolean(MONOCLOUD_AUTH_USER_INFO) ?? DEFAULT_OPTIONS.userInfo,
487
490
  refetchUserInfo: (options === null || options === void 0 ? void 0 : options.refetchUserInfo) ?? getBoolean(MONOCLOUD_AUTH_REFETCH_USER_INFO) ?? DEFAULT_OPTIONS.refetchUserInfo,
491
+ allowQueryParamOverrides: (options === null || options === void 0 ? void 0 : options.allowQueryParamOverrides) ?? getBoolean(MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES) ?? DEFAULT_OPTIONS.allowQueryParamOverrides,
488
492
  session: {
489
493
  cookie: {
490
494
  name: (options === null || options === void 0 || (_options$session = options.session) === null || _options$session === void 0 || (_options$session = _options$session.cookie) === null || _options$session === void 0 ? void 0 : _options$session.name) ?? MONOCLOUD_AUTH_SESSION_COOKIE_NAME ?? DEFAULT_OPTIONS.session.cookie.name,
@@ -563,7 +567,7 @@ var MonoCloudCoreClient = class {
563
567
  *
564
568
  * @param request - MonoCloud request object.
565
569
  * @param response - MonoCloud response object.
566
- * @param signInOptions - Optional configuration to customize the sign-in behavior.
570
+ * @param signInOptions - Configuration to customize the sign-in behavior.
567
571
  * @returns A promise that resolves when the callback processing and redirection are complete.
568
572
  *
569
573
  * @throws {@link MonoCloudValidationError} When validation of parameters or state fails.
@@ -597,18 +601,26 @@ var MonoCloudCoreClient = class {
597
601
  appState = await this.options.onSetApplicationState(request);
598
602
  if (appState === null || appState === void 0 || typeof appState !== "object" || Array.isArray(appState)) throw new MonoCloudValidationError$1("Invalid Application State. Expected state to be an object");
599
603
  }
600
- const retUrl = request.getQuery("return_url") ?? opt.returnUrl;
604
+ const query = this.options.allowQueryParamOverrides ? {
605
+ returnUrl: request.getQuery("return_url"),
606
+ authenticatorHint: request.getQuery("authenticator_hint"),
607
+ scope: request.getQuery("scope"),
608
+ resource: request.getQuery("resource"),
609
+ display: request.getQuery("display"),
610
+ uiLocales: request.getQuery("ui_locales"),
611
+ acrValues: request.getQuery("acr_values"),
612
+ loginHint: request.getQuery("login_hint"),
613
+ prompt: request.getQuery("prompt"),
614
+ maxAge: parseInt(request.getQuery("max_age"), 10)
615
+ } : {};
616
+ const retUrl = query.returnUrl ?? opt.returnUrl;
601
617
  if (typeof retUrl === "string" && retUrl && (!isAbsoluteUrl(retUrl) || isSameHost(this.options.appUrl, retUrl))) opt.returnUrl = retUrl;
602
618
  const { error } = signInOptionsSchema.validate(opt, { abortEarly: true });
603
619
  if (error) throw new MonoCloudValidationError$1(error.details[0].message);
604
620
  const state = generateState();
605
621
  const nonce = generateNonce();
606
622
  const { codeChallenge, codeVerifier } = await generatePKCE();
607
- const maxAgeQuery = request.getQuery("max_age");
608
- if (typeof maxAgeQuery === "string" && maxAgeQuery) {
609
- const parsedMaxAge = parseInt(maxAgeQuery, 10);
610
- if (!isNaN(parsedMaxAge)) opt.authParams.maxAge = parsedMaxAge;
611
- }
623
+ if (!isNaN(query.maxAge)) opt.authParams.maxAge = query.maxAge;
612
624
  const returnUrl = encodeURIComponent(opt.returnUrl ?? this.options.appUrl);
613
625
  let params = {
614
626
  redirectUri: `${this.options.appUrl}${ensureLeadingSlash(this.options.routes.callback)}`,
@@ -617,30 +629,28 @@ var MonoCloudCoreClient = class {
617
629
  state,
618
630
  codeChallenge
619
631
  };
620
- const authenticatorHint = request.getQuery("authenticator_hint") ?? opt.authParams.authenticatorHint;
632
+ const authenticatorHint = query.authenticatorHint ?? opt.authParams.authenticatorHint;
621
633
  if (typeof authenticatorHint === "string" && authenticatorHint) params.authenticatorHint = authenticatorHint;
622
- const reqScope = request.getQuery("scope");
623
- const scopes = (typeof reqScope === "string" ? reqScope : void 0) ?? opt.authParams.scopes;
634
+ const scopes = (typeof query.scope === "string" ? query.scope : void 0) ?? opt.authParams.scopes;
624
635
  if (scopes) {
625
636
  const { error: e } = scopesValidationSchema.validate(scopes, { abortEarly: true });
626
637
  if (!e) params.scopes = scopes;
627
638
  }
628
- const reqResource = request.getQuery("resource");
629
- const resource = (typeof reqResource === "string" ? reqResource : void 0) ?? opt.authParams.resource;
639
+ const resource = (typeof query.resource === "string" ? query.resource : void 0) ?? opt.authParams.resource;
630
640
  if (resource) {
631
641
  const { error: e } = resourceValidationSchema.validate(resource, { abortEarly: true });
632
642
  if (!e) params.resource = resource;
633
643
  }
634
- const display = request.getQuery("display") ?? opt.authParams.display;
644
+ const display = query.display ?? opt.authParams.display;
635
645
  if (typeof display === "string" && display) params.display = display;
636
- const uiLocales = request.getQuery("ui_locales") ?? opt.authParams.uiLocales;
646
+ const uiLocales = query.uiLocales ?? opt.authParams.uiLocales;
637
647
  if (typeof uiLocales === "string" && uiLocales) params.uiLocales = uiLocales;
638
- const acrValues = request.getQuery("acr_values") ?? opt.authParams.acrValues;
648
+ const acrValues = query.acrValues ?? opt.authParams.acrValues;
639
649
  if (typeof acrValues === "string" && acrValues) params.acrValues = acrValues.split(" ").map((x) => x.trim()).filter((x) => x !== "");
640
- const loginHint = request.getQuery("login_hint") ?? opt.authParams.loginHint;
650
+ const loginHint = query.loginHint ?? opt.authParams.loginHint;
641
651
  if (typeof loginHint === "string" && loginHint) params.loginHint = loginHint;
642
652
  let prompt;
643
- if (typeof request.getQuery("prompt") === "string") prompt = request.getQuery("prompt");
653
+ if (typeof query.prompt === "string") prompt = query.prompt;
644
654
  else prompt = opt.register ? "create" : opt.authParams.prompt;
645
655
  if (prompt) params.prompt = prompt;
646
656
  /* v8 ignore next -- @preserve */
@@ -700,6 +710,7 @@ var MonoCloudCoreClient = class {
700
710
  if (!isAbsoluteUrl(url)) fullUrl = `${this.options.appUrl}${ensureLeadingSlash(url)}`;
701
711
  const callbackParams = parseCallbackParams(method.toLowerCase() === "post" ? new URLSearchParams(body) : new URL(fullUrl).searchParams);
702
712
  if (callbackParams.state !== monoCloudState.state) throw new MonoCloudValidationError$1("Invalid state");
713
+ if (isPresent(callbackParams.error)) throw new MonoCloudOPError$1(callbackParams.error, callbackParams.errorDescription);
703
714
  const redirectUri = (callbackOptions === null || callbackOptions === void 0 ? void 0 : callbackOptions.redirectUri) ?? `${this.options.appUrl}${ensureLeadingSlash(this.options.routes.callback)}`;
704
715
  if (!callbackParams.code) throw new MonoCloudValidationError$1("Authorization code not found in callback params");
705
716
  const appState = JSON.parse(monoCloudState.appState);
@@ -733,7 +744,6 @@ var MonoCloudCoreClient = class {
733
744
  return response.done();
734
745
  }
735
746
  } catch {}
736
- /* c8 ignore stop */
737
747
  response.redirect(this.options.appUrl);
738
748
  } catch (error) {
739
749
  if (typeof (callbackOptions === null || callbackOptions === void 0 ? void 0 : callbackOptions.onError) === "function") return callbackOptions.onError(error);
@@ -766,7 +776,7 @@ var MonoCloudCoreClient = class {
766
776
  const { error } = userInfoOptionsSchema.validate(userinfoOptions, { abortEarly: true });
767
777
  if (error) throw new MonoCloudValidationError$1(error.details[0].message);
768
778
  }
769
- const refetchUserInfo = (userinfoOptions === null || userinfoOptions === void 0 ? void 0 : userinfoOptions.refresh) ?? this.options.refetchUserInfo;
779
+ const refetchUserInfo = (this.options.allowQueryParamOverrides ? { refresh: getBoolean(request.getQuery("refresh")) } : {}).refresh ?? (userinfoOptions === null || userinfoOptions === void 0 ? void 0 : userinfoOptions.refresh) ?? this.options.refetchUserInfo;
770
780
  const session = await this.sessionService.getSession(request, response, !refetchUserInfo);
771
781
  if (!session) {
772
782
  response.setNoCache();
@@ -813,11 +823,14 @@ var MonoCloudCoreClient = class {
813
823
  const { error } = signOutOptionsSchema.validate(signOutOptions, { abortEarly: true });
814
824
  if (error) throw new MonoCloudValidationError$1(error.details[0].message);
815
825
  }
826
+ const query = this.options.allowQueryParamOverrides ? {
827
+ postLogoutUrl: request.getQuery("post_logout_url"),
828
+ federated: getBoolean(request.getQuery("federated"))
829
+ } : {};
816
830
  let returnUrl = this.options.postLogoutRedirectUri ?? (signOutOptions === null || signOutOptions === void 0 ? void 0 : signOutOptions.postLogoutRedirectUri) ?? this.options.appUrl;
817
- const retUrl = request.getQuery("post_logout_url");
818
- if (typeof retUrl === "string" && retUrl) {
819
- const { error } = signOutOptionsSchema.validate({ postLogoutRedirectUri: retUrl });
820
- if (!error) returnUrl = retUrl;
831
+ if (query.postLogoutUrl) {
832
+ const { error } = signOutOptionsSchema.validate({ postLogoutRedirectUri: query.postLogoutUrl });
833
+ if (!error) returnUrl = query.postLogoutUrl;
821
834
  }
822
835
  if (!isAbsoluteUrl(returnUrl)) returnUrl = `${this.options.appUrl}${ensureLeadingSlash(returnUrl)}`;
823
836
  const session = await this.sessionService.getSession(request, response, false);
@@ -826,7 +839,7 @@ var MonoCloudCoreClient = class {
826
839
  return response.done();
827
840
  }
828
841
  await this.sessionService.removeSession(request, response);
829
- if (!((signOutOptions === null || signOutOptions === void 0 ? void 0 : signOutOptions.federatedSignOut) ?? this.options.federatedSignOut)) {
842
+ if (!(query.federated ?? (signOutOptions === null || signOutOptions === void 0 ? void 0 : signOutOptions.federatedSignOut) ?? this.options.federatedSignOut)) {
830
843
  response.redirect(returnUrl);
831
844
  return response.done();
832
845
  }