Class: HaveAPI::CLI::Authentication::Token

Inherits:
Base
  • Object
show all
Defined in:
lib/haveapi/cli/authentication/token.rb

Instance Attribute Summary

Attributes inherited from Base

#communicator

Instance Method Summary collapse

Methods inherited from Base

#initialize, register

Constructor Details

This class inherits a constructor from HaveAPI::CLI::Authentication::Base

Instance Method Details

#authenticateObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/haveapi/cli/authentication/token.rb', line 53

def authenticate
  @communicator.authenticate(:token, {
      user: @user,
      password: @password,
      token: @token,
      lifetime: @lifetime || :renewable_auto,
      interval: @interval,
      valid_to: @valid_to,
      via: @via
  })
end

#options(opts) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/haveapi/cli/authentication/token.rb', line 5

def options(opts)
  opts.on('--username USER', 'User name') do |u|
    @user = u
  end

  opts.on('--password PASSWORD', 'Password') do |p|
    @password = p
  end

  opts.on('--token TOKEN', 'Token') do |t|
    @token = t
  end

  opts.on('--token-lifetime LIFETIME',
          %i(fixed renewable_manual renewable_auto permanent),
          'Token lifetime, defaults to renewable_auto') do |l|
    @lifetime = l
  end

  opts.on('--token-interval SECONDS', Integer,
          'How long will token be valid in seconds') do |s|
    @interval = s
  end

  opts.on('--new-token', 'Request new token') do
    @token = nil
  end

  via = %i(query_param header)

  opts.on('--token-via VIA', via,
          'Send token as a query parameter or in HTTP header',
          "(#{via.join(', ')})") do |v|
    @via = v.to_sym
  end
end

#saveObject



65
66
67
68
69
70
# File 'lib/haveapi/cli/authentication/token.rb', line 65

def save
  super.update({
                   via: @via,
                   interval: @interval
               })
end

#validateObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/haveapi/cli/authentication/token.rb', line 42

def validate
  return if @token

  @user ||= ask('User name: ') { |q| q.default = nil }

  @password ||= ask('Password: ') do |q|
    q.default = nil
    q.echo = false
  end
end