export const checkCustomAuthConfigFields = function()

in src/msha/auth/routes/auth-login-provider-custom.ts [15:56]


export const checkCustomAuthConfigFields = function (context: Context, providerName: string, customAuth?: SWAConfigFileAuth) {
  const generateResponse = function (msg: string) {
    return {
      context,
      status: 400,
      headers: { ["Content-Type"]: "text/plain" },
      body: msg,
    };
  };

  if (!CUSTOM_AUTH_REQUIRED_FIELDS[providerName]) {
    context.res = response(generateResponse(`Provider '${providerName}' not found`));
    return false;
  }

  const requiredFields = CUSTOM_AUTH_REQUIRED_FIELDS[providerName];
  const configFileProviderName = providerName === "aad" ? ENTRAID_FULL_NAME : providerName;
  const authConfigs: Record<string, string> = {};

  for (const field of requiredFields) {
    const settingName = customAuth?.identityProviders?.[configFileProviderName]?.registration?.[field];
    if (!settingName) {
      context.res = response(generateResponse(`${field} not found for '${providerName}' provider`));
      return false;
    }

    // Special case for aad where the openIdIssuer is in the config file itself rather than the env
    if (providerName === "aad" && field === "openIdIssuer") {
      authConfigs[field] = settingName;
    } else {
      const settingValue = process.env[settingName];
      if (!settingValue) {
        context.res = response(generateResponse(`${settingName} not found in env for '${providerName}' provider`));
        return false;
      }

      authConfigs[field] = settingValue;
    }
  }

  return authConfigs;
};