Class: Legion::Extensions::Identity::Entra::Application::Actor::TokenRefresher

Inherits:
Actors::Every
  • Object
show all
Defined in:
lib/legion/extensions/identity/entra/application/actors/token_refresher.rb

Constant Summary collapse

DEFAULT_REFRESH_INTERVAL =
2700

Instance Method Summary collapse

Instance Method Details

#check_subtask?Boolean

Returns:

  • (Boolean)


15
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 15

def check_subtask?  = false

#enabled?Boolean

rubocop:disable Legion/Extension/ActorEnabledSideEffects

Returns:

  • (Boolean)


24
25
26
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 24

def enabled? # rubocop:disable Legion/Extension/ActorEnabledSideEffects
  true
end

#generate_task?Boolean

Returns:

  • (Boolean)


16
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 16

def generate_task?  = false

#manualObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 28

def manual
  log.debug('Application TokenRefresher tick')
  data = Legion::Extensions::Identity::Entra::Helpers::TokenManager.token_data(:application, refresh: false)

  if data && !Legion::Extensions::Identity::Entra::Helpers::TokenManager.expired?(data)
    log.debug('Application token still valid')
    return
  end

  log.info('Application token nearing expiry, re-acquiring')
  auth_settings = Legion::Extensions::Identity::Entra::Helpers::TokenManager.settings_auth
  runner = Object.new.extend(Legion::Extensions::Identity::Entra::Application::Runners::Credential)
  result = runner.acquire_token(
    tenant_id:     auth_settings[:tenant_id],
    client_id:     auth_settings[:client_id],
    client_secret: auth_settings[:client_secret]
  )

  body = result&.dig(:result)
  unless body&.dig(:access_token)
    log.warn('Application token re-acquisition failed')
    return
  end

  Legion::Extensions::Identity::Entra::Helpers::TokenManager.save_token(
    :application,
    access_token: body[:access_token],
    expires_in:   body[:expires_in],
    scopes:       body[:scope] || 'https://graph.microsoft.com/.default',
    tenant_id:    auth_settings[:tenant_id],
    client_id:    auth_settings[:client_id]
  )
  Legion::Extensions::Identity::Entra::Client.reset!(pattern: :application)
  log.info('Application token refreshed successfully')
rescue StandardError => e
  log.error("Application TokenRefresher: #{e.message}")
end

#run_now?Boolean

Returns:

  • (Boolean)


17
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 17

def run_now?        = false

#runner_classObject



12
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 12

def runner_class    = self.class

#runner_functionObject



13
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 13

def runner_function = 'manual'

#timeObject



19
20
21
22
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 19

def time
  Legion::Settings.dig(:identity, :entra, :application, :token, :refresh_interval) ||
    DEFAULT_REFRESH_INTERVAL
end

#use_runner?Boolean

Returns:

  • (Boolean)


14
# File 'lib/legion/extensions/identity/entra/application/actors/token_refresher.rb', line 14

def use_runner?     = false