Class: Openstack::Client::Authenticator

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Helpers::CacheReaderable
Defined in:
lib/openstack/client/authenticator.rb,
lib/openstack/client/authenticator/request.rb

Overview

:reek:IrresponsibleModule

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CacheReaderable

#cache_key, #null_cache_placeholder, #read_from_cache

Constructor Details

#initialize(username:, password:, uri: Rails.application.config.x.openstack.fetch(:authentication_url), cache: Rails.cache) ⇒ Authenticator

Returns a new instance of Authenticator.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/openstack/client/authenticator.rb', line 19

def initialize(
  username:,
  password:,
  uri: Rails.application.config.x.openstack.fetch(:authentication_url),
  cache: Rails.cache
)
  @username = username
  @password = password
  @uri = URI(uri)
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/openstack/client/authenticator.rb', line 10

def cache
  @cache
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/openstack/client/authenticator.rb', line 10

def password
  @password
end

#uriObject (readonly)

Returns the value of attribute uri.



10
11
12
# File 'lib/openstack/client/authenticator.rb', line 10

def uri
  @uri
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/openstack/client/authenticator.rb', line 10

def username
  @username
end

Instance Method Details

#authenticateObject



31
32
33
34
# File 'lib/openstack/client/authenticator.rb', line 31

def authenticate
  cache_response if token_expired?
  authentication_succeed?
end

#authenticate_requestObject



36
37
38
39
40
41
42
43
# File 'lib/openstack/client/authenticator.rb', line 36

def authenticate_request
  return unless block_given?

  authenticate
  yield.tap do |request|
    request.add_field('x-auth-token', token)
  end
end

#tokenObject



45
46
47
# File 'lib/openstack/client/authenticator.rb', line 45

def token
  read_from_cache.fetch('token')
end