Class: Misty::AuthV2

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

Instance Attribute Summary

Attributes included from Auth

#catalog, #token

Instance Method Summary collapse

Methods included from Auth

#authenticate, #expired?, factory, #find_url, #get_service, #get_token, #get_url, #initialize

Methods included from HTTP::NetHTTP

http_request

Instance Method Details

#credentialsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/misty/auth/auth_v2.rb', line 7

def credentials
  if @token
    identity = { 'token': { 'id': @token } }
  else
    raise CredentialsError, "#{self.class}: User name is required" if @user.name.nil?
    raise 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 CredentialsError, "#{self.class}: No tenant available"
  end

  { 'auth': identity }
end

#endpoint_url(endpoint, region_id, interface) ⇒ Object



27
28
29
# File 'lib/misty/auth/auth_v2.rb', line 27

def endpoint_url(endpoint, region_id, interface)
  return endpoint["#{interface}URL"] if endpoint['region'] == region_id && endpoint["#{interface}URL"]
end

#pathObject



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

def path
  '/v2.0/tokens'
end

#set(response) ⇒ Object



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

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



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

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



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

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