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

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

Instance Method Summary collapse

Methods included from Utils

#param_option, #read_param

Methods inherited from Base

#initialize, register

Constructor Details

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

Instance Method Details

#authenticateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/haveapi/cli/authentication/token.rb', line 58

def authenticate
  opts = {
    token: @token,
    lifetime: @lifetime || :renewable_auto,
    interval: @interval,
    valid_to: @valid_to,
    via: @via
  }

  opts.update(@credentials) if @credentials

  @communicator.authenticate(:token, opts) do |_action, params|
    ret = {}

    params.each do |name, desc|
      ret[name] = read_param(name, desc)
    end

    ret
  end
end

#options(opts) ⇒ Object



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
41
42
43
44
45
46
# File 'lib/haveapi/cli/authentication/token.rb', line 10

def options(opts)
  @credentials = {}

  request_credentials.each do |name, desc|
    opts.on(
      param_option(name, desc),
      desc[:label] || name.to_s
    ) { |v| @credentials[name] = v }
  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



80
81
82
# File 'lib/haveapi/cli/authentication/token.rb', line 80

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

#validateObject



48
49
50
51
52
53
54
55
56
# File 'lib/haveapi/cli/authentication/token.rb', line 48

def validate
  return if @token

  request_credentials.each do |name, desc|
    if !@credentials.has_key?(name) && desc[:required]
      @credentials[name] = read_param(name, desc)
    end
  end
end