Module: Fog::Proxmox::Auth::Token

Included in:
AccessTicket, UserToken
Defined in:
lib/fog/proxmox/auth/token.rb,
lib/fog/proxmox/auth/token/user_token.rb,
lib/fog/proxmox/auth/token/access_ticket.rb

Defined Under Namespace

Classes: AccessTicket, ExpiryError, URLError, UserToken

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



33
34
35
# File 'lib/fog/proxmox/auth/token.rb', line 33

def data
  @data
end

#expiresObject (readonly)

Returns the value of attribute expires.



33
34
35
# File 'lib/fog/proxmox/auth/token.rb', line 33

def expires
  @expires
end

#tokenObject (readonly)

Returns the value of attribute token.



33
34
35
# File 'lib/fog/proxmox/auth/token.rb', line 33

def token
  @token
end

#useridObject (readonly)

Returns the value of attribute userid.



33
34
35
# File 'lib/fog/proxmox/auth/token.rb', line 33

def userid
  @userid
end

Class Method Details

.build(proxmox_options, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/proxmox/auth/token.rb', line 51

def self.build(proxmox_options, options)
  unless proxmox_options.key? :proxmox_auth_method
    raise ArgumentError,
          'Missing required proxmox_auth_method in options.'
  end

  auth_method = proxmox_options[:proxmox_auth_method]
  if auth_method == Fog::Proxmox::Auth::Token::AccessTicket::NAME
    Fog::Proxmox::Auth::Token::AccessTicket.new(proxmox_options, options)
  elsif auth_method == Fog::Proxmox::Auth::Token::UserToken::NAME
    Fog::Proxmox::Auth::Token::UserToken.new(proxmox_options, options)
  else
    raise ArgumentError,
          "Unkown authentication method: #{auth_method}. Only #{Fog::Proxmox::Auth::Token::AccessTicket::NAME} or #{Fog::Proxmox::Auth::Token::UserToken::NAME} are accepted."
  end
end

Instance Method Details

#authenticate(proxmox_options, connection_options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/proxmox/auth/token.rb', line 68

def authenticate(proxmox_options, connection_options = {})
  uri = URI.parse(proxmox_options[:proxmox_url])
  request = {
    expects: [200, 201],
    headers: headers(auth_method, proxmox_options, { Accept: 'application/json' }),
    body: auth_body(proxmox_options),
    method: auth_method,
    path: uri.path + auth_path(proxmox_options)
  }
  connection = Fog::Core::Connection.new(
    uri.to_s,
    false,
    connection_options
  )
  response = connection.request(request)
  Json.get_data(response)
end

#expired?Boolean

Returns:

  • (Boolean)

Raises:



86
87
88
89
90
# File 'lib/fog/proxmox/auth/token.rb', line 86

def expired?
  raise ExpiryError, 'Missing token expiration data' if @expires.nil? || @expires.empty?

  Time.at(@expires) < Time.now.utc
end

#initialize(proxmox_options, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/proxmox/auth/token.rb', line 38

def initialize(proxmox_options, options = {})
  if proxmox_options[:proxmox_url].nil? || proxmox_options[:proxmox_url].empty?
    raise URLError,
          'No proxmox_url provided'
  end

  @token ||= ''
  @token_id ||= ''
  @userid ||= ''
  @data = authenticate(proxmox_options, options)
  build_credentials(proxmox_options, data)
end