Class: ForestAdminAgent::Auth::OidcClientManager

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_agent/auth/oidc_client_manager.rb,
sig/forest_admin_agent/auth/oidc_client_manager.rbs

Constant Summary collapse

TTL =
60 * 60 * 24
TTL_CONFIG =

Returns:

  • (Integer)

Instance Method Summary collapse

Instance Method Details

#make_forest_provider(rendering_id) ⇒ Object

Returns:

  • (Object)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/forest_admin_agent/auth/oidc_client_manager.rb', line 11

def make_forest_provider(rendering_id)
  if Facades::Container.cache(:debug)
    OpenIDConnect.http_config do |options|
      options.ssl.verify = false
    end
  end

  config_agent = Facades::Container.config_from_cache
  cache_key = "#{config_agent[:env_secret]}-client-data"
  cache = setup_cache(cache_key, config_agent)

  render_provider(cache, rendering_id, config_agent[:env_secret])
end

#register(env_secret, registration_endpoint, data) ⇒ Array[String]

Returns:

  • (Array[String])


49
50
51
52
53
54
55
56
57
# File 'lib/forest_admin_agent/auth/oidc_client_manager.rb', line 49

def register(env_secret, registration_endpoint, data)
  response = OpenIDConnect.http_client.post(
    registration_endpoint,
    data,
    { 'Authorization' => "Bearer #{env_secret}" }
  )

  response.body
end

#render_provider(cache, rendering_id, secret) ⇒ Object

Returns:

  • (Object)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/forest_admin_agent/auth/oidc_client_manager.rb', line 59

def render_provider(cache, rendering_id, secret)
  OAuth2::ForestProvider.new(
    rendering_id,
    {
      identifier: cache[:client_id],
      redirect_uri: cache[:redirect_uri],
      host: cache[:issuer].to_s.sub(%r{^https?://(www.)?}, ''),
      secret: secret
    }
  )
end

#setup_cache(env_secret, config_agent) ⇒ { client_id: String, issuer: String, redirect_uri: String }

Returns:

  • ({ client_id: String, issuer: String, redirect_uri: String })


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/forest_admin_agent/auth/oidc_client_manager.rb', line 27

def setup_cache(env_secret, config_agent)
  cache = FileCache.new('auth_issuer', (config_agent[:cache_dir]).to_s, TTL)
  cache.get_or_set env_secret do
    oidc_config = retrieve_config(config_agent[:forest_server_url])
    credentials = register(
      config_agent[:env_secret],
      oidc_config.raw['registration_endpoint'],
      {
        token_endpoint_auth_method: 'none',
        registration_endpoint: oidc_config.raw['registration_endpoint'],
        application_type: 'web'
      }
    )

    {
      client_id: credentials['client_id'],
      issuer: oidc_config.raw['issuer'],
      redirect_uri: credentials['redirect_uris'].first
    }
  end
end