Class: Fizzy::CLI::AuthCommands
- Inherits:
-
Thor
- Object
- Thor
- Fizzy::CLI::AuthCommands
- Includes:
- Base
- Defined in:
- lib/fizzy/cli/auth.rb
Instance Method Summary collapse
- #accounts ⇒ Object
- #identity ⇒ Object
- #login ⇒ Object
- #status ⇒ Object
- #switch(account_slug) ⇒ Object
Methods included from Base
Instance Method Details
#accounts ⇒ Object
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 |
#identity ⇒ Object
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 |
#login ⇒ Object
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 login token = [:token] custom_url = [: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 |
#status ⇒ Object
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["account_name"]} (#{account["account_slug"]})" puts "User: #{account.dig("user", "name")} <#{account.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.}" end |
#switch(account_slug) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fizzy/cli/auth.rb', line 102 def switch(account_slug) data = YAML.safe_load_file(Auth::TOKEN_FILE, permitted_classes: [Time]) normalized = Auth.normalize_slug(account_slug) account = data["accounts"].find { |a| Auth.normalize_slug(a["account_slug"]) == normalized } raise AuthError, "No account found for #{account_slug}" unless account data["default_account"] = normalized File.write(Auth::TOKEN_FILE, YAML.dump(data)) puts "Switched to #{account["account_name"]} (#{normalized})" end |