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_client_for_callback_url(callback_url) ⇒ Object


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
34
# File 'app/services/forest_liana/oidc_client_manager.rb', line 5

def self.get_client_for_callback_url(callback_url)
  begin
    configuration = ForestLiana::OidcConfigurationRetriever.retrieve()
    if ForestLiana.forest_client_id.nil?
      client_data = Rails.cache.read("#{callback_url}-#{ForestLiana.env_secret}-client-data") || nil
      if client_data.nil?
        client_credentials = ForestLiana::OidcDynamicClientRegistrator.register({
          token_endpoint_auth_method: 'none',
          redirect_uris: [callback_url],
          registration_endpoint: configuration['registration_endpoint']
        })
        client_data = { :client_id => client_credentials['client_id'], :issuer => configuration['issuer'] }
        Rails.cache.write("#{callback_url}-#{ForestLiana.env_secret}-client-data", client_data)
      end
    else
      client_data = { :client_id => ForestLiana.forest_client_id, :issuer => configuration['issuer'] }
    end

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