Class: Misty::Auth

Inherits:
Object
  • Object
show all
Includes:
HTTP::NetHTTP
Defined in:
lib/misty/auth.rb,
lib/misty/auth/name.rb

Direct Known Subclasses

AuthV2, AuthV3

Defined Under Namespace

Modules: Domain Classes: AuthenticationError, CatalogError, CredentialsError, DomainScope, ExpiryError, InitError, Name, ProjectScope, TokenError, URLError, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTTP::NetHTTP

#net_http

Constructor Details

#initialize(options, ssl_verify_mode, log) ⇒ Auth

Returns a new instance of Auth.

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/misty/auth.rb', line 27

def initialize(options, ssl_verify_mode, log)
  @ssl_verify_mode, @log =  ssl_verify_mode, log
  @credentials = scoped_authentication

  raise URLError, "No URL provided" unless options[:url] && !options[:url].empty?
  @uri = URI.parse(options[:url])
  @token = nil
  setup(authenticate)
  raise CatalogError, "No catalog provided during authentication" if @catalog.empty?
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



25
26
27
# File 'lib/misty/auth.rb', line 25

def catalog
  @catalog
end

Class Method Details

.factory(options, *args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/misty/auth.rb', line 17

def self.factory(options, *args)
  if options[:tenant_id] || options[:tenant]
    return Misty::AuthV2.new(options, *args)
  else
    return Misty::AuthV3.new(options, *args)
  end
end

Instance Method Details

#authenticateObject



38
39
40
41
42
43
# File 'lib/misty/auth.rb', line 38

def authenticate
  http = net_http(@uri, @ssl_verify_mode, @log)
  response = http.post(self.class.path, @credentials.to_json, Misty::HEADER_JSON)
  raise AuthenticationError, "Response code=#{response.code}, Msg=#{response.msg}" unless response.code =~ /200|201/
  response
end

#expired?Boolean

Returns:

  • (Boolean)

Raises:



45
46
47
48
# File 'lib/misty/auth.rb', line 45

def expired?
  raise ExpiryError, "Missing token expiration data" if @expires.nil? || @expires.empty?
  Time.parse(@expires) < Time.now.utc
end

#get_endpoint(service_names, region, interface) ⇒ Object

Raises:



50
51
52
53
54
55
56
57
# File 'lib/misty/auth.rb', line 50

def get_endpoint(service_names, region, interface)
  @catalog.each do |catalog|
    if service_names.include? catalog["type"]
      return catalog_endpoints(catalog["endpoints"], region, interface)
    end
  end
  raise CatalogError, "No service found with either #{service_names} name, region #{region}, interface #{interface}"
end

#get_tokenObject



59
60
61
62
# File 'lib/misty/auth.rb', line 59

def get_token
  authenticate if expired?
  @token
end