async function determineAuthFlow()

in packages/amplify-provider-awscloudformation/src/configuration-manager.ts [805:894]


async function determineAuthFlow(context: $TSContext, projectConfig?: ProjectConfig): Promise<AuthFlowConfig> {
  // Check for headless parameters
  // TODO fix how input parameters are handled
  let cfnParams = _.get(context, ['exeInfo', 'inputParams', 'awscloudformation'], undefined);
  if (cfnParams?.config) {
    cfnParams = cfnParams.config;
  }
  let {
    accessKeyId,
    profileName,
    region,
    secretAccessKey,
    useProfile,
  }: {
    accessKeyId: string;
    profileName: string;
    region: string;
    secretAccessKey: string;
    useProfile: boolean;
  } = cfnParams || {};

  // Check for local project config
  useProfile = useProfile ?? projectConfig?.config?.useProfile;
  profileName = profileName ?? projectConfig?.config?.profileName;

  const generalCreds = projectConfig?.configLevel === 'general';

  if (generalCreds) {
    return { type: 'general' };
  }

  if (useProfile && profileName) {
    return { type: 'profile', profileName };
  }

  if (accessKeyId && secretAccessKey && region) {
    return { type: 'accessKeys', accessKeyId, region, secretAccessKey };
  }

  if (projectConfig?.config?.awsConfigFilePath) {
    const awsConfigInfo = loadConfigFromPath(projectConfig.config.awsConfigFilePath);
    return { ...awsConfigInfo, type: 'accessKeys' };
  }

  let appId: string;
  let adminAppConfig: { isAdminApp?: boolean; region?: string };
  try {
    appId = resolveAppId(context);
    if (appId) {
      adminAppConfig = await isAmplifyAdminApp(appId);
      if (adminAppConfig.isAdminApp && adminAppConfig.region) {
        region = adminAppConfig.region;
        if (doAdminTokensExist(appId) && projectConfig?.configLevel === 'amplifyAdmin') {
          return { type: 'admin', appId, region };
        }
      }
    }
  } catch (e) {
    // do nothing, appId might not be defined for a new project
  }

  if (context?.exeInfo?.inputParams?.yes) {
    if (process.env.AWS_SDK_LOAD_CONFIG) {
      profileName = profileName || process.env.AWS_PROFILE || 'default';
      return { type: 'profile', profileName };
    } else {
      accessKeyId = accessKeyId || process.env.AWS_ACCESS_KEY_ID;
      secretAccessKey = secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY;
      region = region || resolveRegion();
      if (accessKeyId && secretAccessKey && region) {
        return { type: 'accessKeys', accessKeyId, region, secretAccessKey };
      }
    }
  }

  if (context?.exeInfo?.inputParams?.yes) {
    const errorMessage = 'Failed to resolve AWS credentials with --yes flag.';
    const docsUrl = 'https://docs.amplify.aws/cli/usage/headless';
    context.print.error(errorMessage);
    context.print.info(`Access keys for continuous integration can be configured with headless paramaters: ${chalk.green(docsUrl)}`);
    await context.usageData.emitError(errorMessage);
    exitOnNextTick(1);
  }

  const authType = await askAuthType(adminAppConfig?.isAdminApp);
  if (authType === 'admin') {
    return { type: authType, appId, region };
  }
  return { type: authType };
}