Class: ForestLiana::OidcClientManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/oidc_client_manager.rb

Class Method Summary collapse

Class Method Details

.get_clientObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/forest_liana/oidc_client_manager.rb', line 5

def self.get_client
  begin
    configuration = ForestLiana::OidcConfigurationRetriever.retrieve()
    if ForestLiana.forest_client_id.nil?
      client_data = Rails.cache.read("#{ForestLiana.env_secret}-client-data") || nil
      if client_data.nil?
        client_credentials = ForestLiana::OidcDynamicClientRegistrator.register({
          token_endpoint_auth_method: 'none',
          registration_endpoint: configuration['registration_endpoint']
        })
        client_data = { :client_id => client_credentials['client_id'], :issuer => configuration['issuer'], :redirect_uri => client_credentials['redirect_uris'][0] }
        Rails.cache.write("#{ForestLiana.env_secret}-client-data", client_data)
      end
    else
      client_data = { :client_id => ForestLiana.forest_client_id, :issuer => configuration['issuer'], :redirect_uri => File.join(ForestLiana.application_url, "/forest/authentication/callback").to_s }
    end

    OpenIDConnect::Client.new(
      identifier: client_data[:client_id],
      redirect_uri: client_data[:redirect_uri],
      host: "#{client_data[:issuer].sub(/^https?\:\/\/(www.)?/,'')}",
      authorization_endpoint: '/oidc/auth',
      token_endpoint: '/oidc/token',
    )
  rescue => error
    Rails.cache.delete("#{ForestLiana.env_secret}-client-data")
    raise error
  end
end