Class: Pcli::Services::Commands::User::Totp

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/pcli/services/commands/user/totp.rb

Instance Method Summary collapse

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
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
# File 'lib/pcli/services/commands/user/totp.rb', line 20

def call(*)
  spinner = nil
  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Retrieving user...', output)
    r = api.me
    if r.failure?
      spinner.failure
    else
      spinner.success
    end
    r
  end

  if response.failure?
    output.puts
    Output::ServerError.show(response, output, screen)
    return CommandOutput.continue
  end

  output.puts
  output.puts "This action will regenerate #{response.json['name']}'s TOTP secret and display the corresponding QR code once."
  output.puts

  if prompt.no?(Pl.red('Are you sure you want to continue?'))
    output.puts 'Command aborted.'
    return CommandOutput.continue
  end

  output.puts
  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Regenerating TOTP...', output)
    r = api.rotate_totp
    if r.failure?
      spinner.failure
    end
    r
  end

  if response.success?
    spinner.success("TOTP #{Pl.green('regenerated')}")
    output.puts
    output.puts Pl.yellow('New TOTP code:')
    qr.show(response.json['totpUrl'])
    output.puts
    output.print 'Scan the QR with your authenticator app and press any key when finished...'
    input.gets
    output.clear_screen
  else
    output.puts
    Output::ServerError.show(response, output, screen)
  end

  CommandOutput.continue
end