getAuthTokenSource function

AuthTokenSourceResult getAuthTokenSource()

Determine where the auth token is sourced from.

Implementation

AuthTokenSourceResult getAuthTokenSource() {
  if (isBareMode()) {
    if (getConfiguredApiKeyHelper() != null) {
      return const AuthTokenSourceResult(
        source: AuthTokenSourceKind.apiKeyHelper,
        hasToken: true,
      );
    }
    return const AuthTokenSourceResult(
      source: AuthTokenSourceKind.none,
      hasToken: false,
    );
  }

  if (Platform.environment['ANTHROPIC_AUTH_TOKEN'] != null &&
      !isManagedOAuthContext()) {
    return const AuthTokenSourceResult(
      source: AuthTokenSourceKind.anthropicAuthToken,
      hasToken: true,
    );
  }

  if (Platform.environment['MAGE_OAUTH_TOKEN'] != null) {
    return const AuthTokenSourceResult(
      source: AuthTokenSourceKind.neomageOauthToken,
      hasToken: true,
    );
  }

  final oauthTokenFromFd = getOAuthTokenFromFileDescriptor();
  if (oauthTokenFromFd != null) {
    if (Platform.environment['MAGE_OAUTH_TOKEN_FILE_DESCRIPTOR'] != null) {
      return const AuthTokenSourceResult(
        source: AuthTokenSourceKind.neomageOauthTokenFileDescriptor,
        hasToken: true,
      );
    }
    return const AuthTokenSourceResult(
      source: AuthTokenSourceKind.ccrOauthTokenFile,
      hasToken: true,
    );
  }

  final apiKeyHelper = getConfiguredApiKeyHelper();
  if (apiKeyHelper != null && !isManagedOAuthContext()) {
    return const AuthTokenSourceResult(
      source: AuthTokenSourceKind.apiKeyHelper,
      hasToken: true,
    );
  }

  final oauthTokens = getNeomageAIOAuthTokens();
  if (oauthTokens != null &&
      _shouldUseNeomageAIAuth(oauthTokens.scopes) &&
      oauthTokens.accessToken.isNotEmpty) {
    return const AuthTokenSourceResult(
      source: AuthTokenSourceKind.neomageAi,
      hasToken: true,
    );
  }

  return const AuthTokenSourceResult(
    source: AuthTokenSourceKind.none,
    hasToken: false,
  );
}