Module: Misty::Auth

Includes:
HTTP::NetHTTP
Included in:
AuthV2, AuthV3
Defined in:
lib/misty/auth.rb,
lib/misty/auth/name.rb,
lib/misty/auth/errors.rb

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

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



9
10
11
# File 'lib/misty/auth.rb', line 9

def catalog
  @catalog
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/misty/auth.rb', line 9

def token
  @token
end

Class Method Details

.factory(auth, config) ⇒ Object



11
12
13
14
15
# File 'lib/misty/auth.rb', line 11

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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/misty/auth.rb', line 32

def authenticate
  Misty::HTTP::NetHTTP.http_request(
    @uri, ssl_verify_mode: @config.ssl_verify_mode, log: @config.log
  ) do |connection|
    response = connection.post(path, @credentials.to_json,
      { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
    unless response.code =~ /200|201/
      raise AuthenticationError, "Response code=#{response.code}, Msg=#{response.msg}"
    end
    response
  end
end

#expired?Boolean

Returns:

  • (Boolean)


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

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

#find_url(service, region_id, interface) ⇒ Object

Raises:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/misty/auth.rb', line 68

def find_url(service, region_id, interface)
  if service['endpoints']
    service['endpoints'].each do |endpoint|
      if (url = endpoint_url(endpoint, region_id, interface))
        return url
      end
    end
  end
  message = "No endpoint found: service '#{service['type']}', region '#{region_id}', interface '#{interface}'"
  raise CatalogError, message
end

#get_service(service_names) ⇒ Object

Raises:



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

def get_service(service_names)
  @catalog.each do |service|
    return service if service_names.include?(service['type'])
  end
  raise CatalogError, "No service '#{service_names}' found."
end

#get_tokenObject



63
64
65
66
# File 'lib/misty/auth.rb', line 63

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

#get_url(service_names, region_id, interface) ⇒ Object



52
53
54
# File 'lib/misty/auth.rb', line 52

def get_url(service_names, region_id, interface)
  find_url(get_service(service_names), region_id, interface)
end

#initialize(auth, config) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/misty/auth.rb', line 17

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
    @credentials = set_credentials(auth)
    @token, @catalog, @expires = set(authenticate)
  end
end