Skip to content
Kward Search API index

Module: Kward::CLI::AuthCommands

Defined in:
lib/kward/cli/auth_commands.rb

Overview

Login, logout, and credential-status commands mixed into the CLI frontend.

Instance Method Summary collapse

Instance Method Details

#auth_status_line(label, configured, location) ⇒ Object



37
38
39
40
41
# File 'lib/kward/cli/auth_commands.rb', line 37

def auth_status_line(label, configured, location)
  status = configured ? :ok : :warning
  message = configured ? "configured" : "not configured"
  "#{doctor_mark(status)} #{label}: #{message} (#{location})"
end

#handle_auth_command(arguments) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kward/cli/auth_commands.rb', line 7

def handle_auth_command(arguments)
  if help_option_arguments?(arguments)
    print_command_help("auth")
    return
  end

  case arguments
  when ["status"]
    print_auth_status
  when ["logout"]
    logout_auth
  else
    raise ArgumentError, command_usage("auth")
  end
end

#login(provider: nil, oauth: nil, auth_method: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kward/cli/auth_commands.rb', line 64

def (provider: nil, oauth: nil, auth_method: nil)
  provider = (provider)
  if api_key_login?(provider, auth_method)
    (provider)
    return
  end

  oauth ||= case provider
            when "github", "copilot" then GithubOAuth.new
            when "anthropic" then AnthropicOAuth.new
            else OpenAIOAuth.new
            end
  path = oauth.(prompt: @prompt)
  name = case provider
         when "github", "copilot" then "GitHub"
         when "anthropic" then "Anthropic"
         else "OpenAI"
         end
  @prompt.say("#{colored("Saved", :green, :bold)} #{name} OAuth login to #{path}")
end

#logout_authObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kward/cli/auth_commands.rb', line 43

def logout_auth
  removed = []
  [OpenAIOAuth.default_auth_path, AnthropicOAuth.default_auth_path, GithubOAuth.default_auth_path].each do |path|
    next unless File.exist?(path)

    File.delete(path)
    removed << path
  end
  store = api_key_store
  store.migrate_openrouter_config_key!
  ProviderCatalog.api_key_providers.each do |provider|
    removed << "#{provider.name} API key" if store.delete(provider.id)
  end

  if removed.empty?
    @prompt.say "No saved credentials found."
  else
    @prompt.say "Removed #{removed.length} saved credential#{removed.length == 1 ? "" : "s"}."
  end
end

Writes the auth status output for the terminal CLI flow.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kward/cli/auth_commands.rb', line 24

def print_auth_status
  store = api_key_store
  store.migrate_openrouter_config_key!
  lines = ["#{colored("Auth Status", :green, :bold)}", ""]
  lines << auth_status_line("OpenAI OAuth", File.exist?(OpenAIOAuth.default_auth_path), OpenAIOAuth.default_auth_path)
  lines << auth_status_line("Anthropic OAuth", File.exist?(AnthropicOAuth.default_auth_path), AnthropicOAuth.default_auth_path)
  lines << auth_status_line("GitHub OAuth", File.exist?(GithubOAuth.default_auth_path), GithubOAuth.default_auth_path)
  ProviderCatalog.api_key_providers.each do |provider|
    lines << auth_status_line("#{provider.name} API key", store.configured?(provider.id), store.path)
  end
  @prompt.say lines.join("\n")
end