Module: Legion::Extensions::Identity::Entra::Delegated::Runners::OnBehalfOf

Includes:
Helpers::Lex, Logging::Helper, Settings::Helper
Defined in:
lib/legion/extensions/identity/entra/delegated/runners/on_behalf_of.rb

Instance Method Summary collapse

Instance Method Details

#exchange_on_behalf_of(tenant_id:, client_id:, client_secret:, assertion:, scope:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/identity/entra/delegated/runners/on_behalf_of.rb', line 15

def exchange_on_behalf_of(tenant_id:, client_id:, client_secret:,
                          assertion:, scope:, **)
  log.debug("OnBehalfOf.exchange: tenant=#{tenant_id} scope=#{scope}")
  result = obo_post(tenant_id,
                    grant_type:          'urn:ietf:params:oauth:grant-type:jwt-bearer',
                    client_id:           client_id,
                    client_secret:       client_secret,
                    assertion:           assertion,
                    scope:               scope,
                    requested_token_use: 'on_behalf_of')
  log.info('OnBehalfOf.exchange: token exchanged successfully') if result[:access_token]
  { result: result }
rescue StandardError => e
  handle_exception(e, level: :error, operation: 'delegated.on_behalf_of.exchange')
  { error: 'request_failed', description: e.message }
end

#obo_post(tenant_id, form) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/identity/entra/delegated/runners/on_behalf_of.rb', line 32

def obo_post(tenant_id, form)
  log.debug("OnBehalfOf.obo_post: tenant=#{tenant_id}")
  response = obo_connection(tenant_id).post('oauth2/v2.0/token',
                                            URI.encode_www_form(form.transform_keys(&:to_s)))
  body = response.body.to_s.empty? ? {} : json_load(response.body)
  unless response.success?
    body[:error] ||= "http_#{response.status}"
    body[:error_description] ||= response.reason_phrase
    log.debug("OnBehalfOf.obo_post: error=#{body[:error]} status=#{response.status}")
  end
  body
end