Class: RubyCoded::Auth::AuthManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_coded/auth/auth_manager.rb

Overview

This class is used to manage the authentication process for the different AI providers

Constant Summary collapse

PROVIDERS =
{
  openai: Providers::OpenAI,
  anthropic: Providers::Anthropic
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config_path: UserConfig::CONFIG_PATH, user_config: nil) ⇒ AuthManager

Returns a new instance of AuthManager.



24
25
26
27
# File 'lib/ruby_coded/auth/auth_manager.rb', line 24

def initialize(config_path: UserConfig::CONFIG_PATH, user_config: nil)
  @config_path = config_path
  @user_config = user_config
end

Instance Method Details

#authenticated_provider_namesObject



48
49
50
# File 'lib/ruby_coded/auth/auth_manager.rb', line 48

def authenticated_provider_names
  PROVIDERS.keys.select { |name| credential_store.retrieve(name) }
end

#check_authenticationObject



69
70
71
72
73
74
# File 'lib/ruby_coded/auth/auth_manager.rb', line 69

def check_authentication
  return if configured_providers.any? { |name| credential_store.retrieve(name) }

  provider_name = choose_provider
  (provider_name)
end

#configure_ruby_llm!Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_coded/auth/auth_manager.rb', line 81

def configure_ruby_llm!
  RubyLLM.configure do |config|
    config.max_retries = 1

    PROVIDERS.each do |name, provider|
      credentials = credential_store.retrieve(name)
      next unless credentials
      next if name == :openai && credentials["auth_method"] == "oauth"

      credentials = refresh_if_expired(name, provider, credentials)
      config.send("#{provider.ruby_llm_key}=", extract_api_key(credentials))
    end
  end
end

#configured_providersObject



44
45
46
# File 'lib/ruby_coded/auth/auth_manager.rb', line 44

def configured_providers
  PROVIDERS.keys
end

#login(provider_name) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/ruby_coded/auth/auth_manager.rb', line 29

def (provider_name)
  provider = PROVIDERS.fetch(provider_name)
  strategy = strategy_for(provider)
  credentials = strategy.authenticate
  credential_store.store(provider_name, credentials)
  configure_ruby_llm!
  print_api_credits_notice(provider)
  credentials
end

#login_promptObject



76
77
78
79
# File 'lib/ruby_coded/auth/auth_manager.rb', line 76

def 
  provider_name = choose_provider
  (provider_name)
end

#logout(provider_name) ⇒ Object



39
40
41
42
# File 'lib/ruby_coded/auth/auth_manager.rb', line 39

def logout(provider_name)
  credential_store.remove(provider_name)
  configure_ruby_llm!
end

#model_provider_authenticated?(model_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/ruby_coded/auth/auth_manager.rb', line 62

def model_provider_authenticated?(model_name)
  provider = provider_for_model(model_name)
  return false unless provider

  authenticated_provider_names.include?(provider)
end

#provider_for_model(model_name) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/ruby_coded/auth/auth_manager.rb', line 52

def provider_for_model(model_name)
  return nil if model_name.nil? || model_name.to_s.strip.empty?

  normalized = model_name.to_s.downcase
  return :openai if normalized.match?(/\A(gpt|o\d)/)
  return :anthropic if normalized.start_with?("claude")

  nil
end