Class: Misty::Auth::Token::V2

Inherits:
Object
  • Object
show all
Includes:
Misty::Auth::Token
Defined in:
lib/misty/auth/token/v2.rb

Instance Attribute Summary collapse

Attributes included from Misty::Auth::Token

#catalog, #data, #expires, #token, #user

Instance Method Summary collapse

Methods included from Misty::Auth::Token

build, #get, #initialize

Methods included from HTTP::NetHTTP

http_request

Instance Attribute Details

#tenantObject (readonly)

Returns the value of attribute tenant.



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

def tenant
  @tenant
end

Instance Method Details

#credentialsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/misty/auth/token/v2.rb', line 11

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

#pathObject



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

def path
  '/v2.0/tokens'
end

#set(response) ⇒ Object



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

def set(response)
  @data = JSON.load(response.body)
  @token   = @data['access']['token']['id']
  @expires = @data['access']['token']['expires']
  catalog = @data['access']['serviceCatalog']
  @catalog = Misty::Auth::Catalog::V2.new(catalog)
end

#set_credentials(auth) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/misty/auth/token/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/token/v2.rb', line 35

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