Class: VAAS::ClientCredentialsGrantAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/vaas/client_credentials_grant_authenticator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ClientCredentialsGrantAuthenticator.



10
11
12
13
14
# File 'lib/vaas/client_credentials_grant_authenticator.rb', line 10

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

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



8
9
10
# File 'lib/vaas/client_credentials_grant_authenticator.rb', line 8

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



8
9
10
# File 'lib/vaas/client_credentials_grant_authenticator.rb', line 8

def client_secret
  @client_secret
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/vaas/client_credentials_grant_authenticator.rb', line 8

def token
  @token
end

#token_endpointObject

Returns the value of attribute token_endpoint.



8
9
10
# File 'lib/vaas/client_credentials_grant_authenticator.rb', line 8

def token_endpoint
  @token_endpoint
end

Instance Method Details

#get_tokenObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vaas/client_credentials_grant_authenticator.rb', line 16

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