Class: CognitiveFaculty::Configuration::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/cognitive_faculty/configuration/auth.rb

Overview

Authorization configuration for Cognitive Faculty Platform API.

Attributes:

+client_id+: Client ID used to request an acces token.
+client_secret+: Client secret used to request an access token.
+tenant_id+: AppID tenant id
+oauth_server_url+: AppID oauth server URL
+token+: (read-only) OIDC access token.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuth

Returns a new instance of Auth.



19
20
21
# File 'lib/cognitive_faculty/configuration/auth.rb', line 19

def initialize
  @realm = "master"
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



16
17
18
# File 'lib/cognitive_faculty/configuration/auth.rb', line 16

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



16
17
18
# File 'lib/cognitive_faculty/configuration/auth.rb', line 16

def client_secret
  @client_secret
end

#oauth_server_urlObject

Returns the value of attribute oauth_server_url.



16
17
18
# File 'lib/cognitive_faculty/configuration/auth.rb', line 16

def oauth_server_url
  @oauth_server_url
end

#tenant_idObject

Returns the value of attribute tenant_id.



16
17
18
# File 'lib/cognitive_faculty/configuration/auth.rb', line 16

def tenant_id
  @tenant_id
end

#tokenObject (readonly)

Returns the value of attribute token.



17
18
19
# File 'lib/cognitive_faculty/configuration/auth.rb', line 17

def token
  @token
end

Instance Method Details

#access_tokenObject



23
24
25
26
# File 'lib/cognitive_faculty/configuration/auth.rb', line 23

def access_token
  update_token if token.nil? || token.expired?
  token.token
end

#update_tokenObject



28
29
30
31
32
# File 'lib/cognitive_faculty/configuration/auth.rb', line 28

def update_token
  opts = { token_url: "#{oauth_server_url}/token" }
  oauth2 = OAuth2::Client.new(client_id, client_secret, opts)
  @token = oauth2.client_credentials.get_token(:username => client_id, :password => client_secret)
end