Class: Wiq::Commands::Auth

Inherits:
Base
  • Object
show all
Defined in:
lib/wiq/commands/auth.rb

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?

Instance Method Details

#listObject



182
183
184
185
186
187
188
189
190
191
# File 'lib/wiq/commands/auth.rb', line 182

def list
  entries = Wiq::Credentials.all_entries
  render_index(
    entries,
    summary: "Stored credentials: #{entries.size}.",
    breadcrumbs: [
      { "cmd" => "wiq auth status --as <alias>", "description" => "Inspect a specific slot" }
    ]
  )
end

#loginObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wiq/commands/auth.rb', line 30

def 
  default_host = Wiq::Config::PRODUCTION_HOST
  host = options[:host] || prompt("WIQ host URL [#{default_host}]: ")
  host = host.to_s.strip
  host = default_host if host.empty?
  unless host.start_with?("http")
    raise Wiq::ConfigError.new("Host must be an absolute URL, got #{host.inspect}.",
                               code: "invalid_host")
  end

  token = options[:token] || prompt_secret("Personal access token: ")
  token = token.to_s.strip
  unless token.start_with?("wiq_pat_")
    raise Wiq::ConfigError.new("Token does not start with `wiq_pat_`.",
                               code: "invalid_token",
                               hint: "Mint a PAT at <host>/settings/personal_access_tokens.")
  end

  alias_name = (host, options[:as])

  if Wiq::Credentials.for_host(host, alias_name) && !options[:force]
    raise Wiq::AliasConflictError.new(host, alias_name)
  end

  cfg = Wiq::Config.new
  cfg.instance_variable_set(:@host, host)
  cfg.instance_variable_set(:@token, token)
  client = Wiq::Client.new(cfg)

   = (client, token)

  Wiq::Credentials.store(
    host: host,
    alias_name: alias_name,
    token: token,
    token_prefix: [:token_prefix],
    name: [:name],
    profile: [:profile]
  )

  render(
    {
      "host" => host,
      "alias" => alias_name,
      "token_prefix" => [:token_prefix],
      "name" => [:name],
      "profile" => [:profile],
      "stored_at" => Wiq::Credentials.for_host(host, alias_name)["stored_at"]
    }.compact,
    summary: profile_summary("Stored PAT for #{host} as #{alias_name.inspect}",
                             [:profile]),
    breadcrumbs: [
      { "cmd" => "wiq auth status --as #{alias_name}",
        "description" => "Verify the stored token works" },
      { "cmd" => "wiq auth list", "description" => "See all stored credentials" }
    ]
  )
end

#logoutObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/wiq/commands/auth.rb', line 154

def logout
  cfg = Wiq::Config.load(symbolized_options)
  raise Wiq::HostUnsetError unless cfg.host
  unless cfg.alias_name
    aliases = Wiq::Credentials.aliases_for(cfg.host)
    raise Wiq::AmbiguousAliasError.new(cfg.host, aliases) unless aliases.empty?

    render({ "host" => cfg.host, "removed" => false },
           summary: "No credentials stored for #{cfg.host}.")
    return
  end

  removed = Wiq::Credentials.remove(cfg.host, cfg.alias_name)
  render(
    { "host" => cfg.host, "alias" => cfg.alias_name, "removed" => removed },
    summary: removed ? "Cleared #{cfg.alias_name.inspect} for #{cfg.host}." \
                    : "No credential stored at #{cfg.host} / #{cfg.alias_name.inspect}."
  )
end

#statusObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wiq/commands/auth.rb', line 103

def status
  cfg = Wiq::Config.load(symbolized_options)
  raise Wiq::HostUnsetError unless cfg.host

  store_entry = cfg.alias_name ? Wiq::Credentials.for_host(cfg.host, cfg.alias_name) || {} : {}
   = nil
  live_error = nil
  if cfg.token
    begin
      client = Wiq::Client.new(cfg)
       = (client, cfg.token)
    rescue Wiq::APIError, Faraday::Error => e
      live_error = e.message
    end
  end

  data = {
    "host" => cfg.host,
    "host_source" => cfg.sources[:host],
    "alias" => cfg.alias_name,
    "alias_source" => cfg.sources[:alias],
    "token_present" => !cfg.token.nil?,
    "token_source" => cfg.sources[:token],
    "token_prefix" => store_entry["token_prefix"],
    "stored_name" => store_entry["name"],
    "stored_profile" => store_entry["profile"],
    "live_name" => &.dig(:name),
    "live_last_used_at" => &.dig(:last_used_at),
    "live_profile" => &.dig(:profile),
    "live_error" => live_error
  }.compact

  summary =
    if !cfg.token
      cfg.alias_name ? "No token stored for alias #{cfg.alias_name.inspect}." \
                    : "No alias resolvable for this host."
    else
      profile_summary("Token reachable (alias=#{cfg.alias_name})",
                      &.dig(:profile) || store_entry["profile"])
    end

  render(data, summary: summary)
end