Class: ActiveAdmin::Oidc::Devise::OmniauthCallbacksController

Inherits:
Devise::OmniauthCallbacksController
  • Object
show all
Defined in:
app/controllers/active_admin/oidc/devise/omniauth_callbacks_controller.rb

Overview

Handles the OAuth callback from the IdP. Wiring:

# config/routes.rb (or inside ActiveAdmin::Devise.config)
devise_for :admin_users, ActiveAdmin::Devise.config.merge(
  controllers: {
    omniauth_callbacks: "active_admin/oidc/devise/omniauth_callbacks"
  }
)

The action name matches the provider name registered with Devise (‘:oidc`, from ActiveAdmin::Oidc::Engine::PROVIDER_NAME).

Instance Method Summary collapse

Instance Method Details

#failureObject



60
61
62
63
64
# File 'app/controllers/active_admin/oidc/devise/omniauth_callbacks_controller.rb', line 60

def failure
  Rails.logger.warn("[activeadmin-oidc] omniauth failure: #{failure_message}")
  flash[:alert] = ActiveAdmin::Oidc.config.access_denied_message
  redirect_to after_omniauth_failure_path_for(resource_name)
end

#oidcObject



20
21
22
23
24
25
26
27
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
# File 'app/controllers/active_admin/oidc/devise/omniauth_callbacks_controller.rb', line 20

def oidc
  auth  = request.env['omniauth.auth'] || {}
  info  = auth['info'] || {}
  # Defensive: an OIDC strategy is supposed to put a Hash at
  # extra.raw_info, but a misbehaving/custom strategy could
  # set something else (String, nil, Array). We only trust a
  # Hash-shaped value; anything else collapses to {} and we
  # rebuild `sub`/`email` from the top-level auth hash below.
  extra = auth.dig('extra', 'raw_info')
  extra = {} unless extra.is_a?(Hash)

  claims = extra.to_h.transform_keys(&:to_s)
  claims['sub']   = auth['uid'] if claims['sub'].blank? && auth['uid'].present?
  claims['email'] = info['email'] if claims['email'].blank? && info['email'].present?

  admin_user = UserProvisioner.new(
    ActiveAdmin::Oidc.config,
    claims: claims,
    provider: ActiveAdmin::Oidc::Engine::PROVIDER_NAME.to_s
  ).call

   admin_user, event: :authentication
  set_flash_message(:notice, :success, kind: 'OIDC') if is_navigational_format?
rescue ActiveAdmin::Oidc::InactiveError => e
  Rails.logger.warn("[activeadmin-oidc] inactive: #{e.inactive_message_key}")
  # Fall back to the standard `inactive` translation rather
  # than the raw symbol — custom keys like :locked_by_admin
  # would otherwise leak host-internal state into a flash
  # visible to unauthenticated visitors.
  flash[:alert] = I18n.t(
    "devise.failure.#{e.inactive_message_key}",
    default: I18n.t("devise.failure.inactive")
  )
  redirect_to after_omniauth_failure_path_for(resource_name)
rescue ActiveAdmin::Oidc::ProvisioningError => e
  Rails.logger.warn("[activeadmin-oidc] denial: #{e.message}")
  flash[:alert] = ActiveAdmin::Oidc.config.access_denied_message
  redirect_to after_omniauth_failure_path_for(resource_name)
end