Class: IronCore::KeystoneTokenProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_core/keystone_token_provider.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ KeystoneTokenProvider

Returns a new instance of KeystoneTokenProvider.



3
4
5
6
7
8
9
10
11
# File 'lib/iron_core/keystone_token_provider.rb', line 3

def initialize(client, options)
  @rest_client = client.dup
  @token = options[:tenant_token] # Way to bypass fetching a token from keystone api
  @server = options[:server]
  @tenant = options[:tenant]
  @username = options[:username]
  @password = options[:password]
  @user_token = options[:token]
end

Instance Method Details

#post(path, params = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/iron_core/keystone_token_provider.rb', line 44

def post(path, params = {})
  request_hash = {}
  request_hash[:headers] = {'Content-Type' => 'application/json', 'Accept' => 'application/json'}
  request_hash[:body] = params.to_json
  @rest_client.post(path, request_hash)
end

#tokenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/iron_core/keystone_token_provider.rb', line 13

def token
  if @token.nil? || (@local_expires_at && (Time.now - @local_expires_at > -10))
    payload = {
      auth: {
        tenantId: @tenant,
      }
    }
    if @username.to_s != ''
      payload[:auth][:passwordCredentials] = {
        username: @username,
        password: @password
      }
    elsif @user_token.to_s != ''
      payload[:auth][:token] = {id: @user_token}
    end

    response = post(@server + 'tokens', payload)
    result = JSON.parse(response.body)
    token_data = result['access']['token']

    issued_at = Time.parse(token_data['issued_at'] + " UTC")
    expires = Time.parse(token_data['expires'] + " UTC")
    duration = (expires - issued_at).to_i

    @local_expires_at = Time.now + duration
    @token = token_data['id']
  end

  @token
end