Class: Fizzy::CLI::AuthCommands

Inherits:
Thor
  • Object
show all
Includes:
Base
Defined in:
lib/fizzy/cli/auth.rb

Instance Method Summary collapse

Methods included from Base

included

Instance Method Details

#accountsObject



92
93
94
95
96
97
98
99
# File 'lib/fizzy/cli/auth.rb', line 92

def accounts
  data = YAML.safe_load_file(Auth::TOKEN_FILE, permitted_classes: [Time])
  data["accounts"].each do |a|
    marker = a["account_slug"] == data["default_account"] ? " *" : ""
    puts "#{a["account_name"]} (#{a["account_slug"]})#{marker}"
    puts "  #{a.dig("user", "name")} <#{a.dig("user", "email_address")}>"
  end
end

#identityObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fizzy/cli/auth.rb', line 13

def identity
  resp = client.get("/my/identity")
  if json?
    Formatter.json(resp.body)
  else
    resp.body["accounts"].each do |a|
      puts "#{a["name"]} (#{a["slug"]})"
      puts "  #{a["user"]["name"]} <#{a["user"]["email_address"]}> — #{a["user"]["role"]}"
    end
  end
end

#loginObject

Raises:



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
# File 'lib/fizzy/cli/auth.rb', line 28

def 
  token = options[:token]
  custom_url = options[:url]

  # Verify token by fetching identity
  client_opts = { token: token, account_slug: "" }
  client_opts[:base_url] = custom_url if custom_url
  c = Client.new(**client_opts)
  resp = c.get("/my/identity")
  accounts = resp.body["accounts"]

  raise AuthError, "No accounts found for this token" if accounts.empty?

  # Build tokens data
  token_accounts = accounts.map do |a|
    acct = {
      "account_slug" => Auth.normalize_slug(a["slug"]),
      "account_name" => a["name"],
      "account_id" => a["id"],
      "access_token" => token,
      "user" => {
        "id" => a["user"]["id"],
        "name" => a["user"]["name"],
        "email_address" => a["user"]["email_address"],
        "role" => a["user"]["role"]
      },
      "created_at" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ")
    }
    acct["url"] = custom_url if custom_url
    acct
  end

  data = {
    "accounts" => token_accounts,
    "default_account" => Auth.normalize_slug(accounts.first["slug"])
  }

  FileUtils.mkdir_p(Auth::CONFIG_DIR)
  File.write(Auth::TOKEN_FILE, YAML.dump(data))
  File.chmod(0o600, Auth::TOKEN_FILE)

  puts "Authenticated as #{token_accounts.first.dig("user", "name")}"
  token_accounts.each do |a|
    marker = a["account_slug"] == data["default_account"] ? " (default)" : ""
    puts "  #{a["account_name"]} (#{a["account_slug"]})#{marker}"
  end
end

#statusObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fizzy/cli/auth.rb', line 77

def status
  resp = client.get("/my/identity")

  puts "URL: #{client.base_url}" unless base_url == Client::DEFAULT_BASE_URL
  puts "Token: #{Auth::TOKEN_FILE}"
  puts "Account: #{["account_name"]} (#{["account_slug"]})"
  puts "User: #{.dig("user", "name")} <#{.dig("user", "email_address")}>"

  accounts_count = resp.body["accounts"].size
  puts "Accounts: #{accounts_count}" if accounts_count > 1
rescue Fizzy::AuthError => e
  puts "Not authenticated: #{e.message}"
end

#switch(account_slug) ⇒ Object

Raises:



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fizzy/cli/auth.rb', line 102

def switch()
  data = YAML.safe_load_file(Auth::TOKEN_FILE, permitted_classes: [Time])
  normalized = Auth.normalize_slug()
   = data["accounts"].find { |a| Auth.normalize_slug(a["account_slug"]) == normalized }
  raise AuthError, "No account found for #{}" unless 

  data["default_account"] = normalized
  File.write(Auth::TOKEN_FILE, YAML.dump(data))

  puts "Switched to #{["account_name"]} (#{normalized})"
end