Sha256: a1d34cdf569612e175764997db639f41ae9ee144df72fc2dc73c0377910b96ea

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module IronCore
  class KeystoneTokenProvider
    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

    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

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
iron_core-1.0.12 lib/iron_core/keystone_token_provider.rb
iron_core-1.0.11 lib/iron_core/keystone_token_provider.rb
iron_core-1.0.9 lib/iron_core/keystone_token_provider.rb
iron_core-1.0.8 lib/iron_core/keystone_token_provider.rb
iron_core-1.0.7 lib/iron_core/keystone_token_provider.rb