Class: RogIQ::Commands::Auth

Inherits:
Thor
  • Object
show all
Defined in:
lib/rogiq/commands/auth.rb

Instance Method Summary collapse

Instance Method Details

#loginObject



28
29
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
# File 'lib/rogiq/commands/auth.rb', line 28

def 
  fmt = RogIQ::Formatters.new(format: options[:format])
  base = options[:api].to_s.strip
  base = ENV["ROGIQ_API_URL"].to_s.strip if base.empty?
  base = ask("API base URL:", default: "https://api.rogiq.ai") if base.empty?
  base = base.to_s.chomp("/")
  email = ask("Email:")
  password = ask("Password:", echo: false)

  headers = { "Content-Type" => "application/json", "Accept" => "application/json" }
  res, jwt = (base, headers, email, password)
  json = JSON.parse(res.body)

  if json["mfa_required"]
    mfa_token = json["mfa_token"]
    code = ask("MFA code:")
    remember = ask("Remember this device for 30 days? (y/N):").downcase.start_with?("y")
    res, jwt = post_mfa_verify(base, headers, mfa_token, code, remember)
    json = JSON.parse(res.body)
  end

  unless jwt
    fmt.error_msg(error_message(res))
    exit 1
  end

  accounts = json["accounts"] || []
   = (fmt, accounts)
  token_payload = mint_cli_token(base, jwt, , headers)
  plain = token_payload["token"]
  unless plain
    err = token_payload["error"] || token_payload["errors"]
    fmt.error_msg("Could not create API token: #{err || token_payload.inspect}")
    exit 1
  end

  RogIQ::ConfigStore.save(
    "api_base_url" => base,
    "token" => plain,
    "account_id" => ,
    "email" => email
  )

  fmt.success("Saved credentials to #{RogIQ::ConfigStore::PATH}")
  out = token_payload.reject { |k, _| k.to_s == "token" }
  out["token_saved"] = true
  fmt.output(out)
rescue RogIQ::HttpApi::Error => e
  fmt.error_msg(e.message)
  exit 1
end

#logoutObject



81
82
83
84
# File 'lib/rogiq/commands/auth.rb', line 81

def logout
  RogIQ::ConfigStore.clear!
  puts "Removed #{RogIQ::ConfigStore::PATH}"
end

#use(account_id) ⇒ Object



98
99
100
101
# File 'lib/rogiq/commands/auth.rb', line 98

def use()
  RogIQ::ConfigStore.merge!("account_id" => .to_s)
  puts "Default account_id=#{}"
end

#whoamiObject



87
88
89
90
91
92
93
94
95
# File 'lib/rogiq/commands/auth.rb', line 87

def whoami
  fmt = RogIQ::Formatters.new(format: options[:format])
  api = RogIQ::HttpApi.from_config!
  data = api.get("/api/v1/me")
  fmt.output(data)
rescue RogIQ::HttpApi::Error => e
  fmt.error_msg(e.message)
  exit 1
end