Class: IronCore::KeystoneTokenProvider
- Inherits:
-
Object
- Object
- IronCore::KeystoneTokenProvider
- Defined in:
- lib/iron_core/keystone_token_provider.rb
Instance Method Summary collapse
-
#initialize(client, options) ⇒ KeystoneTokenProvider
constructor
A new instance of KeystoneTokenProvider.
- #post(path, params = {}) ⇒ Object
- #token ⇒ Object
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, ) @rest_client = client.dup @token = [:tenant_token] # Way to bypass fetching a token from keystone api @server = [:server] @tenant = [:tenant] @username = [:username] @password = [:password] @user_token = [: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 |
#token ⇒ Object
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 |