Class: Misty::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/misty/auth.rb

Direct Known Subclasses

AuthV2, AuthV3

Defined Under Namespace

Classes: AuthenticationError, CatalogError, CredentialsError, ExpiryError, InitError, TokenError, URLError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Auth

Returns a new instance of Auth.

Raises:



24
25
26
27
28
29
30
31
32
# File 'lib/misty/auth.rb', line 24

def initialize(options)
  raise CredentialsError unless credentials_valid?(options)
  @credentials = scoped_credentials(options)
  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.



22
23
24
# File 'lib/misty/auth.rb', line 22

def catalog
  @catalog
end

Class Method Details

.factory(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/misty/auth.rb', line 12

def self.factory(options = {})
  if options[:project]
    return Misty::AuthV3.new(options)
  elsif options[:tenant]
    return Misty::AuthV2.new(options)
  else
    raise CredentialsError, "Cannot identify version from credentials"
  end
end

Instance Method Details

#authenticateObject



34
35
36
37
38
39
# File 'lib/misty/auth.rb', line 34

def authenticate
  http = Net::HTTP.new(@uri.host, @uri.port)
  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:



41
42
43
44
# File 'lib/misty/auth.rb', line 41

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:



46
47
48
49
50
51
52
53
# File 'lib/misty/auth.rb', line 46

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



55
56
57
58
# File 'lib/misty/auth.rb', line 55

def get_token
  authenticate if expired?
  @token
end