decidim-user_fields 0.0.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +308 -49
  3. data/Rakefile +253 -0
  4. data/app/forms/decidim/custom_user_fields/admin/customizations_config_form.rb +67 -0
  5. data/app/forms/decidim/custom_user_fields/verifications/verification_form.rb +85 -0
  6. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields_form.html.erb.deface +2 -2
  7. data/app/overrides/decidim/devise/omniauth_registrations/new/extra_user_fields.html.erb.deface +3 -0
  8. data/app/overrides/invitation_override.rb +31 -0
  9. data/app/packs/entrypoints/decidim_custom_user_fields.js +0 -0
  10. data/app/packs/stylesheets/decidim/custom_user_fields/_decidim_custom_user_fields.scss +48 -0
  11. data/app/views/decidim/custom_user_fields/_profile_form.html.erb +2 -2
  12. data/app/views/decidim/custom_user_fields/_registration_form.html.erb +2 -2
  13. data/app/views/decidim/custom_user_fields/_verification_form.erb +4 -0
  14. data/app/views/decidim/custom_user_fields/system/_customizations_tab.html.erb +15 -0
  15. data/config/assets.rb +9 -0
  16. data/config/i18n-tasks.yml +22 -0
  17. data/config/locales/decidim_custom_user_fields_scenarios.en.yml +50 -0
  18. data/config/locales/decidim_custom_user_fields_scenarios.fr.yml +48 -0
  19. data/config/locales/decidim_toggle.en.yml +13 -0
  20. data/config/locales/decidim_toggle.fr.yml +13 -0
  21. data/config/locales/decidim_user_fields.en.yml +25 -0
  22. data/config/locales/decidim_user_fields.fr.yml +25 -0
  23. data/lib/decidim/custom_user_fields/custom_user_fields.rb +10 -14
  24. data/lib/decidim/custom_user_fields/customization_field_naming.rb +17 -0
  25. data/lib/decidim/custom_user_fields/customizations/builder.rb +48 -0
  26. data/lib/decidim/custom_user_fields/customizations/customization.rb +29 -0
  27. data/lib/decidim/custom_user_fields/customizations.rb +42 -0
  28. data/lib/decidim/custom_user_fields/decidim_integrations.rb +52 -0
  29. data/lib/decidim/custom_user_fields/definition_guidance.rb +109 -0
  30. data/lib/decidim/custom_user_fields/dev/bootstrap.rb +102 -0
  31. data/lib/decidim/custom_user_fields/dev/local_oidc_seeder.rb +150 -0
  32. data/lib/decidim/custom_user_fields/dev/omniauth_providers_patcher.rb +68 -0
  33. data/lib/decidim/custom_user_fields/dev/openid_connect_setup.rb +110 -0
  34. data/lib/decidim/custom_user_fields/dev/scenario_customizations.rb +72 -0
  35. data/lib/decidim/custom_user_fields/engine.rb +59 -26
  36. data/lib/decidim/custom_user_fields/extended_data.rb +52 -0
  37. data/lib/decidim/custom_user_fields/field_definition.rb +28 -6
  38. data/lib/decidim/custom_user_fields/fields/boolean_field.rb +51 -0
  39. data/lib/decidim/custom_user_fields/fields/date_field.rb +80 -10
  40. data/lib/decidim/custom_user_fields/fields/dummy_field.rb +40 -0
  41. data/lib/decidim/custom_user_fields/fields/extra_field_ref_field.rb +118 -0
  42. data/lib/decidim/custom_user_fields/fields/generic_field.rb +99 -6
  43. data/lib/decidim/custom_user_fields/fields/text_area_field.rb +34 -9
  44. data/lib/decidim/custom_user_fields/fields/text_field.rb +47 -6
  45. data/lib/decidim/custom_user_fields/i18n/customization_keys.rb +55 -0
  46. data/lib/decidim/custom_user_fields/i18n/customization_keys_scanner.rb +80 -0
  47. data/lib/decidim/custom_user_fields/i18n/tasks.rb +7 -0
  48. data/lib/decidim/custom_user_fields/overrides/authorizations_controller.rb +18 -0
  49. data/lib/decidim/custom_user_fields/overrides/command.rb +23 -21
  50. data/lib/decidim/custom_user_fields/overrides/confirmations_controller.rb +15 -0
  51. data/lib/decidim/custom_user_fields/overrides/devise_authentication_methods.rb +68 -0
  52. data/lib/decidim/custom_user_fields/overrides/form_definition.rb +25 -8
  53. data/lib/decidim/custom_user_fields/overrides/invitations_controller.rb +109 -0
  54. data/lib/decidim/custom_user_fields/overrides/omniauth_command.rb +40 -0
  55. data/lib/decidim/custom_user_fields/overrides/omniauth_registrations_controller.rb +93 -0
  56. data/lib/decidim/custom_user_fields/overrides/user.rb +16 -0
  57. data/lib/decidim/custom_user_fields/registration_fields.rb +99 -0
  58. data/lib/decidim/custom_user_fields/update_customizations_config_command.rb +39 -0
  59. data/lib/decidim/custom_user_fields/upgrade/migrate_toggle_config.rb +74 -0
  60. data/lib/decidim/custom_user_fields/verifications/builder.rb +100 -0
  61. data/lib/decidim/custom_user_fields/verifications/verifications.rb +82 -0
  62. data/lib/decidim/custom_user_fields/version.rb +4 -2
  63. data/lib/decidim/user_fields.rb +46 -13
  64. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_upgrade.rake +16 -0
  65. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_webpacker.rake +59 -0
  66. data/lib/tasks/decidim/custom_user_fields/decidim_custom_user_fields_upgrade_tasks.rake +16 -0
  67. data/lib/tasks/decidim/custom_user_fields/dev/local_setup.rake +16 -0
  68. metadata +80 -15
  69. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/custom_user_fields/customizations/customization"
4
+ require "decidim/custom_user_fields/customizations/builder"
5
+
6
+ module Decidim
7
+ module CustomUserFields
8
+ module Customizations
9
+ class << self
10
+ def register(name, &)
11
+ customization = Customization.new(name)
12
+ registry[name.to_sym] = customization
13
+ builder = Builder.new(customization)
14
+ yield builder
15
+ customization
16
+ end
17
+
18
+ def find(name)
19
+ return if name.blank?
20
+
21
+ registry[name.to_sym]
22
+ end
23
+
24
+ def all
25
+ registry.values
26
+ end
27
+
28
+ delegate :any?, to: :registry
29
+
30
+ def clear!
31
+ registry.clear
32
+ end
33
+
34
+ private
35
+
36
+ def registry
37
+ @registry ||= {}
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module DecidimIntegrations
6
+ module_function
7
+
8
+ def apply!
9
+ include_form(Decidim::RegistrationForm) do |klass|
10
+ next if klass.respond_to?(:require_password_on_accepting)
11
+
12
+ klass.define_singleton_method(:require_password_on_accepting) do
13
+ Decidim::User.require_password_on_accepting
14
+ end
15
+ end
16
+
17
+ include_form(Decidim::OmniauthRegistrationForm)
18
+ include_form(Decidim::AccountForm)
19
+ prepend_to(Decidim::CreateRegistration, Command)
20
+ prepend_to(Decidim::CreateOmniauthRegistration, OmniauthCommand)
21
+ prepend_to(Decidim::Devise::OmniauthRegistrationsController, OmniauthRegistrationsController)
22
+ prepend_to(Decidim::UpdateAccount, Command)
23
+ prepend_to(Decidim::Devise::InvitationsController, InvitationAcceptExtendedData)
24
+ prepend_to(Decidim::Devise::SessionsController, FirstSignInPrompt)
25
+ prepend_to(Decidim::Devise::OmniauthRegistrationsController, FirstSignInPrompt)
26
+ prepend_to(Decidim::Devise::RegistrationsController, FirstSignInPrompt)
27
+ prepend_to(Decidim::Devise::InvitationsController, FirstSignInPrompt)
28
+ prepend_to(Decidim::Devise::ConfirmationsController, ConfirmationsController)
29
+ prepend_to(Decidim::Devise::ConfirmationsController, FirstSignInPrompt)
30
+ prepend_to(Decidim::Verifications::AuthorizationsController, AuthorizationsController)
31
+ prepend_to(Decidim::User, UserExtendedDataGuard)
32
+ end
33
+
34
+ def include_form(klass)
35
+ include_module(klass, FormDefinition)
36
+ yield klass if block_given?
37
+ end
38
+
39
+ def prepend_to(klass, mod)
40
+ return if klass.included_modules.include?(mod)
41
+
42
+ klass.prepend(mod)
43
+ end
44
+
45
+ def include_module(klass, mod)
46
+ return if klass.included_modules.include?(mod)
47
+
48
+ klass.include(mod)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "did_you_mean"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ # Builds Decidim::CustomUserFields::Error messages for unknown or removed DSL calls.
8
+ module DefinitionGuidance
9
+ LEGACY = {
10
+ custom_fields:
11
+ "Use Customizations.all / register_customization instead of custom_fields.",
12
+ register_field_set:
13
+ "Use register_customization instead of register_field_set.",
14
+ add_field:
15
+ "Use register_customization { registration_fields { |set| set.add_field ... } } " \
16
+ "instead of configure { add_field }."
17
+ }.freeze
18
+
19
+ SUPPORTED_FIELD_TYPES = [:dummy, :text, :textarea, :date, :extra_field_ref, :boolean].freeze
20
+
21
+ module_function
22
+
23
+ def raise_unknown!(receiver, method_name, suggestions: [])
24
+ raise Error, unknown_message(receiver, method_name, suggestions)
25
+ end
26
+
27
+ def unknown_message(receiver, method_name, suggestions)
28
+ [
29
+ "#{label(receiver)}##{method_name} is not a valid CustomUserFields DSL call.",
30
+ caller_hint,
31
+ hint_for(receiver, method_name, suggestions)
32
+ ].compact.join(" ")
33
+ end
34
+
35
+ def unsupported_type_message(type)
36
+ hint = type_suggestion(type)
37
+ base = "field type #{type} is not supported"
38
+ return "#{base} (supported: #{SUPPORTED_FIELD_TYPES.join(", ")})" if hint.blank?
39
+
40
+ "#{base}. Did you mean? #{hint}"
41
+ end
42
+
43
+ def hint_for(receiver, method_name, suggestions)
44
+ legacy = legacy_hint(receiver, method_name)
45
+ return legacy if legacy
46
+
47
+ match = spell(method_name.to_s, suggestions.map(&:to_s))
48
+ match ? "Did you mean? #{match}" : nil
49
+ end
50
+
51
+ def legacy_hint(receiver, method_name)
52
+ key = method_name.to_sym
53
+ return LEGACY[key] if key != :add_field
54
+ return LEGACY[key] if module_receiver?(receiver)
55
+
56
+ nil
57
+ end
58
+
59
+ def type_suggestion(type)
60
+ spell(type.to_s, SUPPORTED_FIELD_TYPES.map(&:to_s))
61
+ end
62
+
63
+ def spell(input, dictionary)
64
+ DidYouMean::SpellChecker.new(dictionary:).correct(input).first
65
+ end
66
+
67
+ def module_receiver?(receiver)
68
+ receiver.equal?(Decidim::CustomUserFields)
69
+ end
70
+
71
+ def label(receiver)
72
+ receiver.is_a?(Module) ? receiver.name : receiver.class.name
73
+ end
74
+
75
+ def caller_hint
76
+ loc = caller_locations.find { |entry| !gem_path?(entry.path) }
77
+ return unless loc
78
+
79
+ "(called from #{loc.path}:#{loc.lineno})"
80
+ end
81
+
82
+ def gem_path?(path)
83
+ path.include?("custom_user_fields") || path.include?("definition_guidance")
84
+ end
85
+ end
86
+
87
+ # Raises DefinitionGuidance errors from method_missing on DSL receivers.
88
+ module DefinitionMissing
89
+ def method_missing(method_name, *_args, &)
90
+ DefinitionGuidance.raise_unknown!(
91
+ self,
92
+ method_name,
93
+ suggestions: dsl_method_names
94
+ )
95
+ end
96
+
97
+ def respond_to_missing?(_method_name, _include_private = false)
98
+ false
99
+ end
100
+
101
+ def dsl_method_names
102
+ owner = is_a?(Module) ? self : self.class
103
+ return [] unless owner.const_defined?(:DSL_METHODS, false)
104
+
105
+ owner::DSL_METHODS
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Dev
6
+ # Minimal Decidim org + admin for local OIDC (no full Decidim.seed! demo data).
7
+ class Bootstrap
8
+ ADMIN_EMAIL = "admin@example.org"
9
+ ADMIN_PASSWORD = "decidim123456789"
10
+
11
+ def self.call(**)
12
+ new(**).call
13
+ end
14
+
15
+ def initialize(organization_host: ENV.fetch("DECIDIM_HOST", "localhost"))
16
+ @organization_host = organization_host
17
+ end
18
+
19
+ def call
20
+ organization = ensure_organization!
21
+ ensure_terms_of_service!(organization)
22
+ ensure_admin!(organization)
23
+ ensure_system_admin!
24
+ organization
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :organization_host
30
+
31
+ def ensure_organization!
32
+ Decidim::Organization.find_by(host: organization_host) || Decidim::Organization.create!(
33
+ name: localized("Custom User Fields Dev"),
34
+ host: organization_host,
35
+ secondary_hosts: %w(0.0.0.0 127.0.0.1),
36
+ default_locale: Decidim.default_locale,
37
+ available_locales: Decidim.available_locales,
38
+ reference_prefix: "dev",
39
+ description: localized("Local development organization"),
40
+ smtp_settings: {
41
+ from: "decidim-local <#{ADMIN_EMAIL}>",
42
+ from_email: ADMIN_EMAIL,
43
+ from_label: "decidim-local",
44
+ address: ENV.fetch("SMTP_ADDRESS", "mailcatcher"),
45
+ port: ENV.fetch("SMTP_PORT", "1025"),
46
+ user_name: ENV.fetch("SMTP_USERNAME", ""),
47
+ encrypted_password: Decidim::AttributeEncryptor.encrypt(ENV.fetch("SMTP_PASSWORD", ""))
48
+ },
49
+ colors: { primary: "#bf4044", secondary: "#09780e", tertiary: "#3584e4" },
50
+ available_authorizations: []
51
+ )
52
+ end
53
+
54
+ def ensure_terms_of_service!(organization)
55
+ return if Decidim::StaticPage.exists?(organization:, slug: "terms-of-service")
56
+
57
+ tos_page = Decidim::StaticPage.create!(
58
+ organization:,
59
+ slug: "terms-of-service",
60
+ title: localized("Terms of service"),
61
+ content: localized("Terms of service for local development."),
62
+ allow_public_access: true
63
+ )
64
+ organization.update!(tos_version: tos_page.updated_at)
65
+ end
66
+
67
+ def ensure_admin!(organization)
68
+ user = Decidim::User.find_or_initialize_by(email: ADMIN_EMAIL, organization:)
69
+ user.assign_attributes(
70
+ name: "Admin",
71
+ nickname: "admin",
72
+ password: ADMIN_PASSWORD,
73
+ password_confirmation: ADMIN_PASSWORD,
74
+ confirmed_at: Time.current,
75
+ admin: true,
76
+ admin_terms_accepted_at: Time.current,
77
+ tos_agreement: true,
78
+ accepted_tos_version: organization.tos_version,
79
+ newsletter_notifications_at: Time.current,
80
+ locale: organization.default_locale
81
+ )
82
+ user.save!
83
+ user
84
+ end
85
+
86
+ def ensure_system_admin!
87
+ user = Decidim::System::Admin.find_or_initialize_by(email: "system@example.org")
88
+ user.assign_attributes(
89
+ password: "decidim123456789",
90
+ password_confirmation: "decidim123456789"
91
+ )
92
+ user.save!
93
+ user
94
+ end
95
+
96
+ def localized(text)
97
+ Decidim.available_locales.index_with { |_locale| text }
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ module Dev
8
+ # Configures the demo organization for local OIDC + scenario customizations.
9
+ class LocalOidcSeeder
10
+ DEFAULT_CUSTOMIZATION = :association
11
+
12
+ def self.call(**)
13
+ new(**).call
14
+ end
15
+
16
+ def initialize(
17
+ oidc_env_path: default_oidc_env_path,
18
+ customization: ENV.fetch("DEV_CUSTOMIZATION", DEFAULT_CUSTOMIZATION).to_sym,
19
+ organization_host: ENV.fetch("DECIDIM_HOST", "localhost")
20
+ )
21
+ @oidc_env_path = Pathname.new(oidc_env_path)
22
+ @customization = customization
23
+ @organization_host = organization_host
24
+ end
25
+
26
+ def call
27
+ register_scenarios!
28
+ organization = ensure_organization!
29
+ apply_oidc_settings!(organization)
30
+ enable_customization!(organization)
31
+ enable_authorizations!(organization)
32
+ print_summary(organization)
33
+ organization
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :oidc_env_path, :customization, :organization_host
39
+
40
+ def default_oidc_env_path
41
+ module_root = File.expand_path("../../../..", __dir__)
42
+ Pathname.new(module_root).join("docker/zitadel/oidc.env")
43
+ end
44
+
45
+ def register_scenarios!
46
+ return unless defined?(Decidim::CustomUserFields::ScenarioCustomizations)
47
+
48
+ Decidim::CustomUserFields::ScenarioCustomizations.register!
49
+ end
50
+
51
+ def ensure_organization!
52
+ Decidim::Organization.find_by(host: organization_host) || Bootstrap.call(organization_host:)
53
+ end
54
+
55
+ def apply_oidc_settings!(organization)
56
+ env = load_oidc_env
57
+ settings = organization.omniauth_settings || {}
58
+ public_issuer = env.fetch("OIDC_ISSUER")
59
+ internal_issuer = env.fetch("OIDC_ISSUER_INTERNAL", public_issuer)
60
+ redirect_uri = env.fetch("OIDC_REDIRECT_URI")
61
+ client_id = env.fetch("OIDC_CLIENT_ID")
62
+ client_secret = env.fetch("OIDC_CLIENT_SECRET")
63
+ URI.parse(internal_issuer)
64
+ public_uri = URI.parse(public_issuer)
65
+
66
+ settings.merge!(
67
+ "omniauth_settings_openid_connect_enabled" => true,
68
+ "omniauth_settings_openid_connect_name" => "Zitadel",
69
+ "omniauth_settings_openid_connect_scope" => "openid email profile",
70
+ "omniauth_settings_openid_connect_issuer" => public_issuer,
71
+ "omniauth_settings_openid_connect_uid_field" => "sub",
72
+ "omniauth_settings_openid_connect_response_type" => "code",
73
+ "omniauth_settings_openid_connect_discovery" => false,
74
+ "omniauth_settings_openid_connect_icon" => "phone-line",
75
+ "omniauth_settings_openid_connect_client_options__host" => public_uri.host,
76
+ "omniauth_settings_openid_connect_client_options__port" => public_uri.port.to_s,
77
+ "omniauth_settings_openid_connect_client_options__scheme" => public_uri.scheme,
78
+ "omniauth_settings_openid_connect_client_options__identifier" => client_id,
79
+ "omniauth_settings_openid_connect_client_options__secret" => client_secret,
80
+ "omniauth_settings_openid_connect_client_options__redirect_uri" => redirect_uri,
81
+ "omniauth_settings_openid_connect_client_options__authorization_endpoint" =>
82
+ "#{public_issuer}/oauth/v2/authorize",
83
+ "omniauth_settings_openid_connect_client_options__token_endpoint" =>
84
+ "#{internal_issuer}/oauth/v2/token",
85
+ "omniauth_settings_openid_connect_client_options__userinfo_endpoint" =>
86
+ "#{internal_issuer}/oidc/v1/userinfo",
87
+ "omniauth_settings_openid_connect_client_options__jwks_uri" =>
88
+ "#{internal_issuer}/oauth/v2/keys",
89
+ "omniauth_settings_openid_connect_client_options__end_session_endpoint" =>
90
+ "#{public_issuer}/oidc/v1/end_session"
91
+ )
92
+
93
+ organization.update!(omniauth_settings: encrypt_omniauth_settings(settings))
94
+ end
95
+
96
+ def encrypt_omniauth_settings(settings)
97
+ settings.transform_values do |value|
98
+ Decidim::OmniauthProvider.value_defined?(value) ? Decidim::AttributeEncryptor.encrypt(value) : value
99
+ end
100
+ end
101
+
102
+ def enable_customization!(organization)
103
+ Decidim::Toggle.save_config!(
104
+ organization,
105
+ :custom_user_fields,
106
+ { "enabled_customization" => customization.to_s },
107
+ merge: false
108
+ )
109
+ end
110
+
111
+ def enable_authorizations!(organization)
112
+ handlers = Decidim::CustomUserFields::Verifications.workflow_handlers_for(customization)
113
+ return if handlers.empty?
114
+
115
+ current = Array(organization.available_authorizations)
116
+ organization.update!(
117
+ available_authorizations: (current + handlers).uniq
118
+ )
119
+ end
120
+
121
+ def load_oidc_env
122
+ raise "Missing #{oidc_env_path}. Run: ./bin/dev-oidc-up" unless oidc_env_path.file?
123
+
124
+ env = oidc_env_path.each_line.with_object({}) do |line, memo|
125
+ next if line.strip.empty? || line.start_with?("#")
126
+
127
+ key, value = line.split("=", 2)
128
+ memo[key] = value.to_s.strip
129
+ end
130
+ %w(OIDC_ISSUER OIDC_REDIRECT_URI OIDC_CLIENT_ID OIDC_CLIENT_SECRET).each do |key|
131
+ raise "Missing #{key} in #{oidc_env_path}" if env[key].blank?
132
+ end
133
+ env
134
+ end
135
+
136
+ def print_summary(organization)
137
+ Rails.logger.debug { <<~MSG }
138
+
139
+ decidim_custom_user_fields:dev:seed complete
140
+ Organization: #{organization.name} (#{organization.host})
141
+ Customization: #{customization} enabled
142
+ OIDC provider: Zitadel (openid_connect)
143
+ Login: http://localhost:3000/users/sign_in
144
+
145
+ MSG
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ module Dev
8
+ # Idempotently writes the dummy app initializer that registers the
9
+ # openid_connect OmniAuth provider in Decidim.omniauth_providers.
10
+ class OmniauthProvidersPatcher
11
+ SNIPPET = <<~RUBY
12
+ # frozen_string_literal: true
13
+
14
+ # Local Zitadel OIDC testing: register the openid_connect provider
15
+ # catalogue entry. Enabled in development only, honouring
16
+ # OMNIAUTH_OPENID_CONNECT_ENABLED; credentials come from the
17
+ # organization record (see Dev::OpenidConnectSetup.provider_setup).
18
+ if Rails.env.development?
19
+ Decidim.omniauth_providers[:openid_connect] = {
20
+ enabled: Decidim::Env.new("OMNIAUTH_OPENID_CONNECT_ENABLED", "true").present?,
21
+ icon: "shield-line"
22
+ }
23
+ end
24
+ RUBY
25
+
26
+ def self.call(initializer_path: default_initializer_path)
27
+ new(initializer_path:).call
28
+ end
29
+
30
+ def self.default_initializer_path
31
+ module_root = File.expand_path("../../../..", __dir__)
32
+ Pathname.new(module_root).join(
33
+ "spec/decidim_dummy_app/config/initializers/decidim_openid_connect.rb"
34
+ )
35
+ end
36
+
37
+ def initialize(initializer_path:)
38
+ @initializer_path = Pathname.new(initializer_path)
39
+ end
40
+
41
+ def call
42
+ raise_missing_dummy_app unless dummy_app_installed?
43
+
44
+ content = initializer_path.file? ? initializer_path.read : nil
45
+ status = content == SNIPPET ? :skipped : :patched
46
+ initializer_path.write(SNIPPET) if status == :patched
47
+ status
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :initializer_path
53
+
54
+ def dummy_app_installed?
55
+ dummy_app_root.join("config/application.rb").file?
56
+ end
57
+
58
+ def dummy_app_root
59
+ initializer_path.dirname.dirname.dirname
60
+ end
61
+
62
+ def raise_missing_dummy_app
63
+ raise "Missing dummy app (#{dummy_app_root}). Run: bundle exec rake test_app"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Dev
6
+ # Registers the openid_connect OmniAuth strategy for local Zitadel testing.
7
+ module OpenidConnectSetup
8
+ CONFIG_MAPPING = {
9
+ name: :name,
10
+ response_type: :response_type,
11
+ response_mode: :response_mode,
12
+ discovery: :discovery,
13
+ scope: :scope,
14
+ uid_field: :uid_field,
15
+ issuer: :issuer,
16
+ post_logout_redirect_uri: :post_logout_redirect_uri,
17
+ client_options__port: :client_options__port,
18
+ client_options__scheme: :client_options__scheme,
19
+ client_options__host: :client_options__host,
20
+ client_options__authorization_endpoint: :client_options__authorization_endpoint,
21
+ client_options__token_endpoint: :client_options__token_endpoint,
22
+ client_options__userinfo_endpoint: :client_options__userinfo_endpoint,
23
+ client_options__jwks_uri: :client_options__jwks_uri,
24
+ client_options__end_session_endpoint: :client_options__end_session_endpoint,
25
+ client_options__identifier: :client_options__identifier,
26
+ client_options__secret: :client_options__secret,
27
+ client_options__redirect_uri: :client_options__redirect_uri
28
+ }.freeze
29
+
30
+ module_function
31
+
32
+ def enabled?
33
+ return false unless defined?(OmniAuth::Strategies::OpenIDConnect)
34
+ # Compose sets ZITADEL_OIDC_ENABLED=1 for local OIDC; never load that
35
+ # middleware in the test suite (setup decrypts org omniauth settings).
36
+ return false if Rails.env.test?
37
+
38
+ ActiveModel::Type::Boolean.new.cast(
39
+ ENV.fetch("ZITADEL_OIDC_ENABLED", Rails.env.development?)
40
+ )
41
+ end
42
+
43
+ def register_middleware!
44
+ return unless enabled?
45
+
46
+ configure_zitadel_http_clients!
47
+
48
+ Rails.application.config.middleware.use OmniAuth::Builder do
49
+ provider(
50
+ :openid_connect,
51
+ setup: Decidim::CustomUserFields::Dev::OpenidConnectSetup.provider_setup
52
+ )
53
+ end
54
+ end
55
+
56
+ def provider_setup
57
+ lambda do |env|
58
+ request = Rack::Request.new(env)
59
+ organization = Decidim::Organization.find_by(host: request.host)
60
+ provider_config = Decidim::CustomUserFields::Dev::OpenidConnectSetup.openid_connect_provider_config(organization)
61
+
62
+ CONFIG_MAPPING.each do |option_key, config_key|
63
+ value = provider_config[config_key]
64
+ next if value.nil?
65
+
66
+ if option_key.to_s.include?("__")
67
+ key_parent, key = option_key.to_s.split("__", 2)
68
+ env["omniauth.strategy"].options[key_parent.to_sym] ||= {}
69
+ env["omniauth.strategy"].options[key_parent.to_sym][key.to_sym] = value
70
+ else
71
+ env["omniauth.strategy"].options[option_key] = value
72
+ end
73
+ end
74
+
75
+ env["omniauth.strategy"]
76
+ end
77
+ end
78
+
79
+ def openid_connect_provider_config(organization)
80
+ return {} if organization.blank?
81
+
82
+ organization.enabled_omniauth_providers.fetch(:openid_connect, {})
83
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage
84
+ {}
85
+ end
86
+
87
+ # Zitadel resolves the instance from the HTTP Host header (ExternalDomain=localhost).
88
+ # Server-side calls use the Docker service name in the URL but must send Host: localhost:8080.
89
+ def configure_zitadel_http_clients!
90
+ host_header = { "Host" => zitadel_public_host }
91
+
92
+ Rack::OAuth2.http_config do |faraday|
93
+ faraday.headers.update(host_header)
94
+ end
95
+
96
+ OpenIDConnect.http_config do |faraday|
97
+ faraday.headers.update(host_header)
98
+ end
99
+ end
100
+
101
+ def zitadel_public_host
102
+ ENV.fetch("ZITADEL_PUBLIC_HOST") do
103
+ uri = URI.parse(ENV.fetch("OIDC_ISSUER", "http://localhost:8080"))
104
+ uri.port == uri.default_port ? uri.host : "#{uri.host}:#{uri.port}"
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end