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 login
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)
= { "Content-Type" => "application/json", "Accept" => "application/json" }
res, jwt = post_login(base, , 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, , mfa_token, code, remember)
json = JSON.parse(res.body)
end
unless jwt
fmt.error_msg(error_message(res))
exit 1
end
accounts = json["accounts"] || []
account_id = pick_account_id(fmt, accounts)
token_payload = mint_cli_token(base, jwt, account_id, )
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" => 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
|