Class: Misty::AuthV2

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

Instance Attribute Summary

Attributes inherited from Auth

#catalog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Auth

#authenticate, #expired?, factory, #get_endpoint, #get_token

Methods included from HTTP::NetHTTP

#net_http

Constructor Details

#initialize(options, *args) ⇒ AuthV2

Returns a new instance of AuthV2.



9
10
11
12
13
14
# File 'lib/misty/auth/auth_v2.rb', line 9

def initialize(options, *args)
  @user = Misty::Auth::User.new(options[:user_id], options[:user])
  @user.password = options[:password]
  @tenant = Misty::Auth::Name.new(options[:tenant_id], options[:tenant])
  super(options, *args)
end

Class Method Details

.pathObject



5
6
7
# File 'lib/misty/auth/auth_v2.rb', line 5

def self.path
  "/v2.0/tokens"
end

Instance Method Details

#auth_by_idObject



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

def auth_by_id
  {
    "auth": {
      "passwordCredentials": credentials,
      "tenantId": @tenant.id
    }
  }
end

#auth_by_nameObject



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

def auth_by_name
  {
    "auth": {
      "passwordCredentials": credentials,
      "tenantName": @tenant.name
    }
  }
end

#catalog_endpoints(endpoints, region, interface) ⇒ Object



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

def catalog_endpoints(endpoints, region, interface)
  endpoints.each do |endpoint|
    if endpoint["region"] == region && endpoint["#{interface}URL"]
      return endpoint["#{interface}URL"]
    end
  end
end

#credentialsObject



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

def credentials
  {
    "username": @user.name,
    "password": @user.password
  }
end

#get_endpoint_url(endpoints, region, interface) ⇒ Object

Raises:



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

def get_endpoint_url(endpoints, region, interface)
  endpoint = endpoints.select { |ep| !ep[interface].empty? }
  raise CatalogError, "No endpoint available for region '#{region}' and interface '#{interface}'" unless endpoint
  endpoint[0][interface]
end

#scoped_authenticationObject



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

def scoped_authentication
  raise Misty::Auth::CredentialsError, "#{self.class}: User name is required" if @user.name.nil?
  raise Misty::Auth::CredentialsError, "#{self.class}: User password is required" if @user.password.nil?
  return auth_by_id if @tenant.id
  return auth_by_name if @tenant.name
  raise Misty::Auth::CredentialsError, "#{self.class}: No tenant available"
end

#setup(response) ⇒ Object



30
31
32
33
34
35
# File 'lib/misty/auth/auth_v2.rb', line 30

def setup(response)
  payload = JSON.load(response.body)
  @token   = payload["access"]["token"]["id"]
  @catalog = payload["access"]["serviceCatalog"]
  @expires = payload["access"]["token"]["expires"]
end