|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module ActiveAdmin |
| 4 | + module Oidc |
| 5 | + # Finds-or-creates an AdminUser for an OIDC callback. Runs the host's |
| 6 | + # `on_login` hook (which owns all authorization decisions), then saves. |
| 7 | + # |
| 8 | + # provisioner = UserProvisioner.new(config, claims: merged_claims, provider: "oidc") |
| 9 | + # admin_user = provisioner.call # raises ProvisioningError on denial |
| 10 | + # |
| 11 | + # Strategy: |
| 12 | + # |
| 13 | + # 1. Look up by (provider, uid). If found → update. |
| 14 | + # 2. Otherwise look up by the configured identity_attribute. If that row |
| 15 | + # is already locked to a different (provider, uid) → refuse |
| 16 | + # (account-takeover guard). Otherwise adopt it. |
| 17 | + # 3. Otherwise build a new record. |
| 18 | + # 4. Assign the identity attribute and oidc_raw_info. |
| 19 | + # 5. Call config.on_login(admin_user, claims). Falsy → deny. Truthy → |
| 20 | + # save and return. |
| 21 | + # |
| 22 | + # The claims hash is passed through untouched except that `access_token` |
| 23 | + # and `refresh_token` (if present) are never persisted. |
| 24 | + class UserProvisioner |
| 25 | + # Claim keys that must never land in oidc_raw_info. |
| 26 | + BLOCKED_RAW_INFO_KEYS = %w[access_token refresh_token id_token].freeze |
| 27 | + |
| 28 | + def initialize(config, claims:, provider:) |
| 29 | + @config = config |
| 30 | + @claims = claims.transform_keys(&:to_s) |
| 31 | + @provider = provider |
| 32 | + end |
| 33 | + |
| 34 | + def call |
| 35 | + validate_claims! |
| 36 | + |
| 37 | + admin_user = find_or_adopt_or_build |
| 38 | + assign_base_attributes(admin_user) |
| 39 | + |
| 40 | + allowed = @config.on_login.call(admin_user, @claims) |
| 41 | + raise ProvisioningError, denial_message unless allowed |
| 42 | + |
| 43 | + save!(admin_user) |
| 44 | + admin_user |
| 45 | + end |
| 46 | + |
| 47 | + private |
| 48 | + |
| 49 | + def model |
| 50 | + @model ||= admin_user_class |
| 51 | + end |
| 52 | + |
| 53 | + def admin_user_class |
| 54 | + klass_name = @config.respond_to?(:admin_user_class) && @config.admin_user_class |
| 55 | + return klass_name if klass_name.is_a?(Class) |
| 56 | + |
| 57 | + Object.const_get(klass_name || "AdminUser") |
| 58 | + end |
| 59 | + |
| 60 | + def validate_claims! |
| 61 | + if blank?(@claims["sub"]) |
| 62 | + raise ProvisioningError, "OIDC id_token is missing a sub claim" |
| 63 | + end |
| 64 | + |
| 65 | + claim_key = @config.identity_claim.to_s |
| 66 | + if blank?(@claims[claim_key]) |
| 67 | + raise ProvisioningError, |
| 68 | + "OIDC id_token is missing identity claim #{claim_key.inspect}" |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + def find_or_adopt_or_build |
| 73 | + uid = @claims["sub"].to_s |
| 74 | + existing = model.find_by(provider: @provider, uid: uid) |
| 75 | + return existing if existing |
| 76 | + |
| 77 | + identity_value = @claims[@config.identity_claim.to_s] |
| 78 | + identity_match = model.find_by(@config.identity_attribute => identity_value) |
| 79 | + |
| 80 | + if identity_match |
| 81 | + if identity_match.provider.present? || identity_match.uid.present? |
| 82 | + raise ProvisioningError, |
| 83 | + "Identity #{identity_value.inspect} is already linked to a different account (takeover guard)" |
| 84 | + end |
| 85 | + |
| 86 | + identity_match.provider = @provider |
| 87 | + identity_match.uid = uid |
| 88 | + return identity_match |
| 89 | + end |
| 90 | + |
| 91 | + model.new(provider: @provider, uid: uid) |
| 92 | + end |
| 93 | + |
| 94 | + def assign_base_attributes(admin_user) |
| 95 | + identity_value = @claims[@config.identity_claim.to_s] |
| 96 | + admin_user.public_send("#{@config.identity_attribute}=", identity_value) |
| 97 | + admin_user.oidc_raw_info = sanitized_raw_info if admin_user.respond_to?(:oidc_raw_info=) |
| 98 | + end |
| 99 | + |
| 100 | + def sanitized_raw_info |
| 101 | + @claims.reject { |k, _v| BLOCKED_RAW_INFO_KEYS.include?(k) } |
| 102 | + end |
| 103 | + |
| 104 | + def save!(admin_user) |
| 105 | + admin_user.save! |
| 106 | + rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique => e |
| 107 | + raise ProvisioningError, e.message |
| 108 | + end |
| 109 | + |
| 110 | + def denial_message |
| 111 | + @config.access_denied_message |
| 112 | + end |
| 113 | + |
| 114 | + def blank?(value) |
| 115 | + value.nil? || value.to_s.strip.empty? |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments