Class: Misty::AuthV2

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

Instance Attribute Summary

Attributes inherited from Auth

#catalog, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Auth

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

Methods included from HTTP::NetHTTP

net_http

Constructor Details

This class inherits a constructor from Misty::Auth

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

#catalog_endpoints(endpoints, region, interface) ⇒ Object



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

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



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

def credentials
  if @token
    identity = { "token": { "id": @token } }
  else
    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?
    identity = { "passwordCredentials": user_credentials }
  end

  if @tenant.id
    identity.merge!("tenantId": @tenant.id)
  elsif @tenant.name
    identity.merge!("tenantName": @tenant.name)
  else
    raise Misty::Auth::CredentialsError, "#{self.class}: No tenant available"
  end

  { "auth": identity }
end

#get_endpoint_url(endpoints, region, interface) ⇒ Object

Raises:



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

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

#set(response) ⇒ Object



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

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

#set_credentials(auth) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/misty/auth/auth_v2.rb', line 58

def set_credentials(auth)
  if auth[:token]
    @token = auth[:token]
  else
    @user = Misty::Auth::User.new(auth[:user_id], auth[:user])
    @user.password = auth[:password]
  end

  @tenant = Misty::Auth::Name.new(auth[:tenant_id], auth[:tenant])
  credentials
end

#user_credentialsObject



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

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