Sha256: 56bbc5e9b56fc7533e2aebdc5344e84df3caf2b13629d5efc23204de5729b9b0

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

require 'json'
require 'async'
require 'async/http/internet'

module VAAS
  class ClientCredentialsGrantAuthenticator

    attr_accessor :client_id, :client_secret, :token_endpoint, :token

    def initialize(client_id, client_secret, token_endpoint = 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token')
      @client_id = client_id
      @client_secret = client_secret
      @token_endpoint = token_endpoint
    end

    def get_token
      Async do
        client = Async::HTTP::Internet.new

        header = [['content-type', 'application/x-www-form-urlencoded']]
        body = ["grant_type=client_credentials&client_id=#{client_id}&client_secret=#{client_secret}"]

        response = client.post(token_endpoint, header, body)
        self.token = JSON.parse(response.read)['access_token']
      rescue => e
        raise VaasAuthenticationError, e
      ensure
        client&.close
      end

      raise VaasAuthenticationError if token.nil?
      token
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vaas-4.1.0 lib/vaas/client_credentials_grant_authenticator.rb
vaas-4.0.0 lib/vaas/client_credentials_grant_authenticator.rb
vaas-3.1.0 lib/vaas/client_credentials_grant_authenticator.rb