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_credential_lines(credentials, status:, empty_message:) ⇒ Object



58
59
60
61
62
# File 'lib/kward/cli/auth_commands.rb', line 58

def auth_credential_lines(credentials, status:, empty_message:)
  return ["  #{empty_message}"] if credentials.empty?

  credentials.map { |credential| "  #{doctor_mark(status)} #{credential.fetch(:label)}" }
end

#auth_credentialsObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/kward/cli/auth_commands.rb', line 47

def auth_credentials
  store = api_key_store
  store.migrate_openrouter_config_key!
  credentials = [
    { label: "OpenAI OAuth", configured: File.exist?(OpenAIOAuth.default_auth_path) },
    { label: "Anthropic OAuth", configured: File.exist?(AnthropicOAuth.default_auth_path) },
    { label: "GitHub OAuth", configured: File.exist?(GithubOAuth.default_auth_path) }
  ]
  credentials.concat ProviderCatalog.api_key_providers.map { |provider| { label: "#{provider.name} API key", configured: store.configured?(provider.id) } }
end

#handle_auth_command(arguments) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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 ["status", "--all"]
    print_auth_status(show_all: true)
  when ["logout"]
    logout_auth
  else
    raise ArgumentError, command_usage("auth")
  end
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kward/cli/auth_commands.rb', line 85

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



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 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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kward/cli/auth_commands.rb', line 26

def print_auth_status(show_all: false)
  credentials = auth_credentials
  configured, missing = credentials.partition { |credential| credential.fetch(:configured) }
  lines = [colored("Authentication", :green, :bold), "", colored("Configured", :blue, :bold)]
  lines.concat(auth_credential_lines(configured, status: :ok, empty_message: "None"))

  if show_all
    lines << ""
    lines << colored("Not configured", :blue, :bold)
    lines.concat(auth_credential_lines(missing, status: :optional, empty_message: "None"))
  elsif missing.any?
    lines << ""
    lines << "#{missing.length} other provider#{missing.length == 1 ? " is" : "s are"} not configured. Run `kward auth status --all` for details."
  end

  lines << ""
  lines << colored("Credential directory", :blue, :bold)
  lines << "  #{ConfigFiles.config_dir}"
  @prompt.say lines.join("\n")
end