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

http_request

Constructor Details

#initialize(auth, config) ⇒ Auth

Returns a new instance of Auth.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/misty/auth.rb', line 25

def initialize(auth, config)
  if auth[:context]
    # bypass the authentication by given token catalog and expire date
    @token   = auth[:context][:token]
    @catalog = auth[:context][:catalog]
    @expires = auth[:context][:expires]
  else
    raise URLError, 'No URL provided' if auth[:url].nil? || auth[:url].empty?
    @uri = URI.parse(auth[:url])
    @config = config
    # autheticate
    @credentials = set_credentials(auth)
    @token, @catalog, @expires = set(authenticate)
  end
  #byebug unless config.is_a?(Misty::Cloud::Config)
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



17
18
19
# File 'lib/misty/auth.rb', line 17

def catalog
  @catalog
end

#tokenObject (readonly)

Returns the value of attribute token.



17
18
19
# File 'lib/misty/auth.rb', line 17

def token
  @token
end

Class Method Details

.factory(auth, config) ⇒ Object



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

def self.factory(auth, config)
  version = auth[:tenant_id] || auth[:tenant] ? 'V2' : 'V3'
  klass = Object.const_get("Misty::Auth#{version}")
  klass.new(auth, config)
end

Instance Method Details

#authenticateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/misty/auth.rb', line 42

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

#expired?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/misty/auth.rb', line 56

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

#get_endpoint(service_names, region, interface) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
# File 'lib/misty/auth.rb', line 63

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



72
73
74
75
# File 'lib/misty/auth.rb', line 72

def get_token
  @token, @catalog, @expires = set(authenticate) if expired?
  @token
end