decidim-zitadel 0.1.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 (50) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +661 -0
  3. data/README.md +115 -0
  4. data/Rakefile +434 -0
  5. data/app/forms/decidim/zitadel/admin/config_form.rb +91 -0
  6. data/app/helpers/decidim/zitadel/omniauth_button_helper.rb +18 -0
  7. data/app/overrides/decidim_zitadel_account_profile.rb +51 -0
  8. data/app/overrides/decidim_zitadel_login.rb +35 -0
  9. data/app/overrides/decidim_zitadel_omniauth_buttons.rb +74 -0
  10. data/app/views/decidim/zitadel/deface_partials/_account_profile.html.erb +12 -0
  11. data/app/views/decidim/zitadel/deface_partials/_header_login.html.erb +7 -0
  12. data/app/views/decidim/zitadel/deface_partials/_omniauth_button_label.html.erb +8 -0
  13. data/config/locales/de.yml +50 -0
  14. data/config/locales/en.yml +50 -0
  15. data/config/locales/fr.yml +50 -0
  16. data/config/locales/it.yml +50 -0
  17. data/db/migrate/20260101000001_create_decidim_zitadel_sessions.rb +17 -0
  18. data/db/seeds.rb +3 -0
  19. data/lib/decidim/zitadel/account_form_freeze.rb +35 -0
  20. data/lib/decidim/zitadel/auth_attributes.rb +50 -0
  21. data/lib/decidim/zitadel/availability.rb +35 -0
  22. data/lib/decidim/zitadel/complete_session.rb +28 -0
  23. data/lib/decidim/zitadel/discovery.rb +58 -0
  24. data/lib/decidim/zitadel/email_linker.rb +43 -0
  25. data/lib/decidim/zitadel/engine/overrides.rb +23 -0
  26. data/lib/decidim/zitadel/engine.rb +64 -0
  27. data/lib/decidim/zitadel/http_clients.rb +18 -0
  28. data/lib/decidim/zitadel/identity_resolver.rb +44 -0
  29. data/lib/decidim/zitadel/invite_csv.rb +60 -0
  30. data/lib/decidim/zitadel/oidc_end_session.rb +79 -0
  31. data/lib/decidim/zitadel/omniauth_registrations_override.rb +114 -0
  32. data/lib/decidim/zitadel/omniauth_setup.rb +137 -0
  33. data/lib/decidim/zitadel/organization_omniauth.rb +14 -0
  34. data/lib/decidim/zitadel/profile_completion.rb +65 -0
  35. data/lib/decidim/zitadel/profile_completion_guard.rb +60 -0
  36. data/lib/decidim/zitadel/profile_updater.rb +49 -0
  37. data/lib/decidim/zitadel/registrations_override.rb +9 -0
  38. data/lib/decidim/zitadel/seed_hook.rb +12 -0
  39. data/lib/decidim/zitadel/seeds.rb +42 -0
  40. data/lib/decidim/zitadel/sessions_override.rb +53 -0
  41. data/lib/decidim/zitadel/settings_tab.rb +18 -0
  42. data/lib/decidim/zitadel/sign_in_button_cache.rb +23 -0
  43. data/lib/decidim/zitadel/upgrade_hooks.rb +63 -0
  44. data/lib/decidim/zitadel/user_factory.rb +54 -0
  45. data/lib/decidim/zitadel/version.rb +13 -0
  46. data/lib/decidim/zitadel.rb +42 -0
  47. data/lib/decidim-zitadel.rb +3 -0
  48. data/lib/tasks/decidim_zitadel_tasks.rake +21 -0
  49. data/lib/tasks/decidim_zitadel_upgrade.rake +47 -0
  50. metadata +183 -0
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # decidim-zitadel
2
+
3
+ Org-scoped [Zitadel](https://zitadel.com) OpenID Connect for Decidim via [decidim-toggle](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle). Credentials live in Toggle only — never `secrets.yml`.
4
+
5
+ ## Install
6
+
7
+ ```ruby
8
+ gem "decidim-zitadel"
9
+ gem "decidim-toggle", git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle", branch: "main"
10
+ ```
11
+
12
+ ```ruby
13
+ # config/application.rb or an initializer
14
+ require "decidim/zitadel"
15
+ ```
16
+
17
+ ```bash
18
+ bundle exec rails decidim:update
19
+ # or
20
+ bundle exec rails decidim:upgrade
21
+ bundle exec rails db:migrate
22
+ ```
23
+
24
+ `decidim:update` / `decidim:upgrade` copy this gem’s (and `decidim-toggle`’s) migrations into the host app via `FROM`, and install `package.json` dependencies **only if** this gem ships any. You can also run:
25
+
26
+ ```bash
27
+ bundle exec rails railties:install:migrations
28
+ bundle exec rails db:migrate
29
+ ```
30
+
31
+ This gem switches the host to **ActiveRecord sessions** (`activerecord-session_store`) so OIDC nonce/state fit. Ensure the `sessions` table is migrated.
32
+
33
+ ## Toggle fields (System → Organizations → Zitadel)
34
+
35
+ | Field | Notes |
36
+ |-------|--------|
37
+ | Enable | Turns on Zitadel OmniAuth for that org |
38
+ | Issuer URL | e.g. `http://localhost:8080` — discovery GET `{issuer}/.well-known/openid-configuration` on save |
39
+ | Client ID | OIDC app id |
40
+ | Client secret | Leave blank on later saves to keep the stored secret |
41
+ | Scopes | Space-separated OIDC scopes, default `openid email profile` |
42
+ | OmniAuth button label | Translatable per locale. Shown on the Zitadel sign-in button. Blank uses `normalize_provider_name("zitadel").titleize` (`Zitadel`) |
43
+
44
+ Failed discovery does **not** enable the module. Endpoints are persisted with the credentials. No organization OmniAuth System tab / `omniauth_settings` write.
45
+
46
+ When enabled, header Log in keeps the stock main-bar look and POSTs Zitadel OmniAuth. `/users/sign_in` and the login popup (`loginModal`) keep stock Devise markup; the OmniAuth button label uses Toggle `omniauth_button_label` via Deface on `_omniauth_buttons` (the provider span, including `t("devise.shared.omniauth_\#{provider}_button")`). Saving that label updates the organization timestamp so those pages, which cache the buttons on the organization, show the new text. The same selector matches Decidim 0.29 (visible provider span) and 0.30–0.32 (`span.sr-only`); for Zitadel the partial prints the Toggle label in a visible span, and the link `title` is that same string. Password sign-in stays on those stock Decidim surfaces when they still render.
47
+
48
+ Log out still uses Devise `DELETE /users/sign_out`. When the org Toggle is on and discovery stored an `end_session_endpoint`, the gem always 302s to that endpoint with `client_id` and `post_logout_redirect_uri` (the login URL plus `signed_out=1`), plus `id_token_hint` when the OIDC callback stored one. Register that exact post-logout URI on the Zitadel OIDC app (locale prefix included if your host uses one). Local Docker init writes `postLogoutRedirectUris` in `docker/zitadel/init-oidc.sh`. If there is no `end_session_endpoint`, only the local Devise session is cleared.
49
+
50
+ ## HTML overrides
51
+
52
+ | Virtual path | Name | Purpose | File |
53
+ |--------------|------|---------|------|
54
+ | `layouts/decidim/header/_main_links_desktop` | `decidim_zitadel_header_login_desktop` | Header Log in look, Zitadel OmniAuth POST | `app/overrides/decidim_zitadel_login.rb` |
55
+ | `layouts/decidim/header/_main_links_mobile_item_account` | `decidim_zitadel_header_login_mobile` | Mobile Log in look, Zitadel OmniAuth POST | `app/overrides/decidim_zitadel_login.rb` |
56
+ | `decidim/devise/shared/_omniauth_buttons` | `decidim_zitadel_omniauth_button_label` | Provider span label → `zitadel_omniauth_button_label` | `app/overrides/decidim_zitadel_omniauth_buttons.rb` |
57
+ | `decidim/devise/shared/_omniauth_buttons` | `decidim_zitadel_omniauth_button_title` | Zitadel link `title` is the Toggle label; other providers keep the I18n string | `app/overrides/decidim_zitadel_omniauth_buttons.rb` |
58
+ | `decidim/shared/_login_modal` | _(no separate override)_ | Renders `_omniauth_buttons` twice; label comes from shared partial Deface | `app/overrides/decidim_zitadel_omniauth_buttons.rb` |
59
+ | `decidim/account/show` | `decidim_zitadel_hide_account_name` | Locked users: name field → Zitadel `{issuer}/profile` link | `app/overrides/decidim_zitadel_account_profile.rb` |
60
+ | `decidim/account/show` | `decidim_zitadel_hide_account_email` | Hide email when linked | `app/overrides/decidim_zitadel_account_profile.rb` |
61
+ | `decidim/account/show` | `decidim_zitadel_hide_account_locale` | Hide language/locale when linked | `app/overrides/decidim_zitadel_account_profile.rb` |
62
+ | `decidim/account/show` | `decidim_zitadel_hide_password_accordion` | Hide password accordion when linked | `app/overrides/decidim_zitadel_account_profile.rb` |
63
+
64
+ Every `/users/auth/zitadel` authorize start sends OIDC `ui_locales` from `session[:user_locale]`, then `I18n.locale`, then the request-host organization's `default_locale`. OmniAuth middleware handles this, not each login link.
65
+
66
+ ## Local Docker
67
+
68
+ ```bash
69
+ # Module shell (Decidim 0.29)
70
+ docker compose up -d
71
+ docker compose exec zitadel-module bash
72
+
73
+ # With local Zitadel (+ OIDC app init → docker/zitadel/oidc.env)
74
+ docker compose -f docker-compose.yml -f docker-compose.zitadel.yml up -d
75
+ ```
76
+
77
+ Paste `ISSUER_URL` / `CLIENT_ID` / `CLIENT_SECRET` from `docker/zitadel/oidc.env` into Toggle. Prefer issuer `http://localhost:8080` (compose sets `extra_hosts: localhost:host-gateway`).
78
+
79
+ `LOGINV2_REQUIRED` is **false** in the overlay. Production custom Login V2 is a **Zitadel app Login V2 base URI** pointing at [zitadel-voca-login](https://git.octree.ch) (or stock Login V2) — **not** a gem dependency of this module.
80
+
81
+ ## Seeds
82
+
83
+ After `Decidim.seed!`, confirmed users without identities:
84
+
85
+ - `zitadel.seed.alice@example.org`
86
+ - `zitadel.seed.bob@example.org`
87
+
88
+ Toggle is not enabled by seeds.
89
+
90
+ ## Export users for voca-login bulk invite
91
+
92
+ ```bash
93
+ docker compose exec zitadel-module bundle exec rake "decidim_zitadel:export_invite_csv[localhost]"
94
+ # or
95
+ ORGANIZATION_HOST=localhost FILE=/tmp/users.csv bundle exec rake decidim_zitadel:export_invite_csv
96
+ ```
97
+
98
+ CSV: `email;firstname;lastname;locale` (semicolon, header included). Paste into voca-login org invite admin. This gem does not call Zitadel User/invite APIs.
99
+
100
+ ## Specs / coverage
101
+
102
+ ```bash
103
+ # Default (no decidim-user_fields) — mirrors GitLab ruby::rspec
104
+ docker compose -f docker-compose.ci.yml run --rm rspec
105
+ # With decidim-user_fields — mirrors GitLab ruby::rspec-user_fields
106
+ docker compose -f docker-compose.ci.yml run --rm -e BUNDLE_GEMFILE=/app/gemfiles/with_user_fields.gemfile rspec
107
+ # RuboCop
108
+ docker compose -f docker-compose.ci.yml run --rm rspec bundle exec rubocop .
109
+ ```
110
+
111
+ CI compose has **no** Zitadel service. Line coverage must be **> 90%** (`SIMPLECOV=1`).
112
+
113
+ ## Decidim versions
114
+
115
+ Appraisals: `decidim-0.29` (default image `octree/decidim-dev:0.29`), `decidim-0.32` (`docker-compose.0.32.yml`), and `with_user_fields` (0.29 + optional `decidim-user_fields`).
data/Rakefile ADDED
@@ -0,0 +1,434 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "rake"
5
+ require "rake/file_utils"
6
+ require "yaml"
7
+
8
+ require "decidim/dev/common_rake"
9
+
10
+ def base_app_name
11
+ "decidim_zitadel"
12
+ end
13
+
14
+ # Appraisal/CI often set a relative BUNDLE_GEMFILE; dummy chdir breaks that path.
15
+ def expand_bundle_gemfile!
16
+ gemfile = ENV.fetch("BUNDLE_GEMFILE", nil)
17
+ return if gemfile.blank? || File.absolute_path?(gemfile)
18
+
19
+ ENV["BUNDLE_GEMFILE"] = File.expand_path(gemfile, __dir__)
20
+ end
21
+
22
+ def legacy_decidim?
23
+ Gem.loaded_specs.fetch("decidim-core").version < Gem::Version.new("0.32")
24
+ end
25
+
26
+ # Decidim 0.29 expects Rails 7.0 defaults. Some generators leave Rails 8-shaped
27
+ # application.rb (load_defaults 8.1 + autoload_lib) which boots fail on 7.0.10.
28
+ def patch_dummy_app_rails70!(dummy_root)
29
+ return unless legacy_decidim?
30
+
31
+ application_rb = File.join(dummy_root, "config/application.rb")
32
+ return unless File.exist?(application_rb)
33
+
34
+ contents = File.read(application_rb)
35
+ patched = contents
36
+ .gsub(/config\.load_defaults\s+[\d.]+/, "config.load_defaults 7.0")
37
+ .gsub(/^[ \t]*config\.autoload_lib\(.*\n/, "")
38
+ File.write(application_rb, patched) if patched != contents
39
+ end
40
+
41
+ def run!(cwd, command)
42
+ FileUtils.chdir(cwd) { sh(command) }
43
+ end
44
+
45
+ # Keep the file: stub aligned with the Shakapacker gem used by each appraisal.
46
+ def ensure_shakapacker_stub!(dummy_root)
47
+ stub_dir = File.join(dummy_root, "packages/shakapacker-stub")
48
+ FileUtils.mkdir_p(stub_dir)
49
+ File.write(File.join(stub_dir, "package.json"), <<~JSON)
50
+ {"name": "shakapacker", "version": "#{legacy_decidim? ? "7.1.0" : "9.7.0"}"}
51
+ JSON
52
+ end
53
+
54
+ def copy_env_storage_config!(dummy_root)
55
+ generators = Gem.loaded_specs.fetch("decidim-generators").full_gem_path
56
+ source = File.join(generators, "lib/decidim/generators/app_templates/storage.yml")
57
+ FileUtils.cp(source, File.join(dummy_root, "config/storage.yml"))
58
+ end
59
+
60
+ def prepare_dummy_decidim_boot_files!(dummy_root)
61
+ return ensure_dummy_decidim_boot_files!(dummy_root) if legacy_decidim?
62
+
63
+ FileUtils.rm_f(File.join(dummy_root, "config/secrets.yml"))
64
+ FileUtils.rm_f(File.join(dummy_root, "config/initializers/decidim.rb"))
65
+ FileUtils.rm_f(File.join(dummy_root, "package-lock.json"))
66
+ copy_env_storage_config!(dummy_root)
67
+ end
68
+
69
+ def normalize_variant_migrations!(dummy_root)
70
+ pattern = File.join(dummy_root, "db/migrate/*_create_active_storage_variant_records.decidim.rb")
71
+ migrations = Dir.glob(pattern)
72
+ FileUtils.rm_f(legacy_decidim? ? migrations : migrations[0...-1])
73
+ end
74
+
75
+ # Drop migrations not shipped by the installed Decidim 0.29.* / toggle gems
76
+ # (vendor volumes or mixed installs can leave newer Migration[7.2]/[8.x] files).
77
+ def purge_non_legacy_dummy_migrations!(dummy_root)
78
+ migrate_dir = File.join(dummy_root, "db/migrate")
79
+ return unless Dir.exist?(migrate_dir)
80
+
81
+ allowed = allowed_migration_origins
82
+ Dir.glob(File.join(migrate_dir, "*.rb")).each do |path|
83
+ purge_dummy_migration!(path, allowed)
84
+ end
85
+ end
86
+
87
+ def allowed_migration_origins
88
+ origins = {}
89
+ Gem.loaded_specs.each_value do |spec|
90
+ next unless spec.name.start_with?("decidim") || spec.name == "activestorage"
91
+
92
+ Dir.glob(File.join(spec.full_gem_path, "db/migrate/*.rb")).each do |path|
93
+ origin = File.basename(path)[/\A(\d+)/, 1]
94
+ origins[origin] = true if origin
95
+ end
96
+ end
97
+ origins
98
+ end
99
+
100
+ def purge_dummy_migration!(path, allowed_origins)
101
+ contents = File.read(path)
102
+ if contents.match?(/ActiveRecord::Migration\[(?:7\.[1-9]|8)/)
103
+ FileUtils.rm_f(path)
104
+ return
105
+ end
106
+
107
+ origin = contents[/originally (\d+)/, 1]
108
+ return if origin.blank? || allowed_origins[origin]
109
+
110
+ FileUtils.rm_f(path)
111
+ end
112
+
113
+ # Generators sometimes omit or later cleanup wipes these; Decidim 0.29 needs both to boot.
114
+ def ensure_dummy_decidim_boot_files!(dummy_root)
115
+ secrets = File.join(dummy_root, "config/secrets.yml")
116
+ unless File.exist?(secrets)
117
+ File.write(secrets, <<~YAML)
118
+ # Be sure to restart your server when you modify this file.
119
+
120
+ decidim_default: &decidim_default
121
+ application_name: "Zitadel Test"
122
+ mailer_sender: "change-me@example.org"
123
+ available_locales: ["en", "ca", "es"]
124
+ default_locale: en
125
+ # false (boolean) — string "false" is .present? and wrongly enables force_ssl.
126
+ force_ssl: false
127
+ enable_html_header_snippets: "false"
128
+ currency_unit: "€"
129
+ cors_enabled: "false"
130
+ image_uploader_quality: 80
131
+ maximum_attachment_size: 10
132
+ maximum_avatar_size: 5
133
+ max_reports_before_hiding: 3
134
+ track_newsletter_links: "true"
135
+ download_your_data_expiry_time: 7
136
+ throttling_max_requests: 100
137
+ throttling_period: 1
138
+ unconfirmed_access_for: 0
139
+ system_accesslist_ips: []
140
+ base_uploads_path:
141
+ default_csv_col_sep: ";"
142
+ consent_cookie_name: "decidim-consent"
143
+ cache_key_separator: "/"
144
+ cache_expiry_time: 1440
145
+ stats_cache_expiry_time: 10
146
+ expire_session_after: 30
147
+ session_timeout_interval: 10
148
+ enable_remember_me: "true"
149
+ follow_http_x_forwarded_host: "false"
150
+ maximum_conversation_message_length: 1000
151
+ password_similarity_length: 4
152
+ denied_passwords: []
153
+ allow_open_redirects: "false"
154
+ social_share_services: ["X", "Facebook", "WhatsApp", "Telegram"]
155
+ service_worker_enabled: "false"
156
+ page_blocks: ["terms-of-service"]
157
+ admin_password:
158
+ expiration_days: 90
159
+ min_length: 15
160
+ repetition_times: 5
161
+ strong: "true"
162
+ api:
163
+ schema_max_per_page: 50
164
+ schema_max_complexity: 5000
165
+ schema_max_depth: 15
166
+ proposals:
167
+ participatory_space_highlighted_proposals_limit: 4
168
+ process_group_highlighted_proposals_limit: 3
169
+ meetings:
170
+ upcoming_meeting_notification: 2
171
+ enable_proposal_linking: "true"
172
+ embeddable_services: []
173
+ budgets:
174
+ enable_proposal_linking: "true"
175
+ accountability:
176
+ enable_proposal_linking: "true"
177
+ initiatives:
178
+ creation_enabled: "true"
179
+ minimum_committee_members: 2
180
+ default_signature_time_period_length: 120
181
+ default_components: ["pages", "meetings"]
182
+ first_notification_percentage: 33
183
+ second_notification_percentage: 66
184
+ stats_cache_expiration_time: 5
185
+ max_time_in_validating_state: 60
186
+ print_enabled: "true"
187
+ do_not_require_authorization: "false"
188
+ verifications:
189
+ document_types: ["identification_number", "passport"]
190
+
191
+ storage_default: &storage_default
192
+ provider: "local"
193
+
194
+ default: &default
195
+ secret_key_base: "6a8af371b2b759541f253fd351431319d1c6d6ee8073419b911737e820a54cd7d6f00ac4e40c6dc107b3ee70d312dfe0ff236e49f9d95cfb10df2f8746b25712"
196
+ decidim:
197
+ <<: *decidim_default
198
+ omniauth:
199
+ facebook:
200
+ enabled: "false"
201
+ twitter:
202
+ enabled: "false"
203
+ google_oauth2:
204
+ enabled: "false"
205
+ maps:
206
+ dynamic_provider: ""
207
+ static_provider: ""
208
+ storage:
209
+ <<: *storage_default
210
+ vapid:
211
+ enabled: "false"
212
+
213
+ development:
214
+ <<: *default
215
+
216
+ test:
217
+ <<: *default
218
+ omniauth:
219
+ facebook:
220
+ enabled: true
221
+ app_id: fake-facebook-app-id
222
+ app_secret: fake-facebook-app-secret
223
+ twitter:
224
+ enabled: true
225
+ api_key: fake-twitter-api-key
226
+ api_secret: fake-twitter-api-secret
227
+ google_oauth2:
228
+ enabled: true
229
+ client_id:
230
+ client_secret:
231
+ test:
232
+ enabled: true
233
+ icon: tools-line
234
+
235
+ production:
236
+ <<: *default
237
+ YAML
238
+ end
239
+
240
+ initializer = File.join(dummy_root, "config/initializers/decidim.rb")
241
+ return if File.exist?(initializer)
242
+
243
+ template = Gem.loaded_specs["decidim-generators"]&.full_gem_path
244
+ template &&= File.join(template, "lib/decidim/generators/app_templates/initializer.rb")
245
+ FileUtils.cp(template, initializer) if template && File.exist?(template)
246
+ end
247
+
248
+ desc "Generates a dummy app for testing"
249
+ task :test_app do
250
+ expand_bundle_gemfile!
251
+ # Do not wrap in Bundler.with_original_env — that can pick a non-BUNDLE_PATH
252
+ # Rails and emit Rails 8-shaped templates under a 0.29 image.
253
+ generate_decidim_app(
254
+ "spec/decidim_dummy_app",
255
+ "--app_name",
256
+ "#{base_app_name}_test_app",
257
+ "--path",
258
+ "../..",
259
+ "--skip_spring",
260
+ "--demo",
261
+ "--force_ssl",
262
+ "false",
263
+ "--locales",
264
+ "en,ca,es"
265
+ )
266
+
267
+ dummy_root = File.expand_path("spec/decidim_dummy_app", __dir__)
268
+ dummy_gemfile = File.join(dummy_root, "Gemfile")
269
+ gemfile_contents = File.read(dummy_gemfile)
270
+ File.open(dummy_gemfile, "a") do |f|
271
+ # String#include? (not ActiveSupport#exclude?) — ruby:3.2.2 CI rake does not load AS inclusion.
272
+ f.puts %(gem "decidim-zitadel", path: "../..") unless gemfile_contents.include?("decidim-zitadel")
273
+ f.puts %(gem "decidim-toggle", git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle", branch: "main") unless gemfile_contents.include?("decidim-toggle")
274
+ # Shakapacker 7.1 + rack-proxy 1.x → 502 on /decidim-packs (0.29)
275
+ next unless legacy_decidim?
276
+
277
+ f.puts %(gem "rack-proxy", "< 1.0") unless gemfile_contents.include?(%(gem "rack-proxy"))
278
+ end
279
+
280
+ patch_dummy_app_rails70!(dummy_root)
281
+
282
+ # Generators may leave a mismatched .ruby-version (e.g. 3.4) under 0.29.
283
+ ruby_version_file = File.join(dummy_root, ".ruby-version")
284
+ if File.exist?(ruby_version_file)
285
+ File.write(ruby_version_file, "#{legacy_decidim? ? "3.2.2" : "3.4.7"}\n")
286
+ end
287
+
288
+ # Keep JS packs on the Decidim minor under test (generator/npm may write 0.32 pins).
289
+ package_json = File.join(dummy_root, "package.json")
290
+ if File.exist?(package_json) && ENV.fetch("DECIDIM_VERSION", "~> 0.29.7").include?("0.29")
291
+ ensure_shakapacker_stub!(dummy_root)
292
+ File.write(
293
+ package_json,
294
+ <<~JSON
295
+ {
296
+ "name": "decidim-zitadel-test-app",
297
+ "private": true,
298
+ "version": "0.1.0",
299
+ "dependencies": {
300
+ "@decidim/browserslist-config": "0.29.7",
301
+ "@decidim/core": "0.29.7",
302
+ "@decidim/webpacker": "0.29.7",
303
+ "sass-embedded": "1.69.5"
304
+ },
305
+ "devDependencies": {
306
+ "@decidim/dev": "0.29.7",
307
+ "shakapacker": "file:packages/shakapacker-stub"
308
+ },
309
+ "browserslist": [
310
+ "extends @decidim/browserslist-config"
311
+ ],
312
+ "overrides": {
313
+ "sass-embedded": "1.69.5"
314
+ }
315
+ }
316
+ JSON
317
+ )
318
+ end
319
+
320
+ ensure_shakapacker_stub!(dummy_root)
321
+ prepare_dummy_decidim_boot_files!(dummy_root)
322
+
323
+ # Decidim 0.29 needs config/initializers/decidim.rb + secrets.yml (do not remove).
324
+ File.write(File.join(dummy_root, ".decidim_appraisal"), ENV.fetch("BUNDLE_GEMFILE", "Gemfile"))
325
+
326
+ run!(dummy_root, "bundle install")
327
+
328
+ Rake::Task["prepare_tests"].invoke
329
+ end
330
+
331
+ desc "Prepare test database for dummy app (optional Docker compose)"
332
+ task :prepare_tests do
333
+ expand_bundle_gemfile!
334
+ disable_docker_compose = ENV.fetch("DISABLED_DOCKER_COMPOSE", "false") == "true"
335
+ unless disable_docker_compose
336
+ compose = File.expand_path("docker-compose.yml", __dir__)
337
+ system("docker-compose -f #{compose} down -v --remove-orphans") if File.exist?(compose)
338
+ system("docker-compose -f #{compose} up -d") if File.exist?(compose)
339
+ end
340
+
341
+ common_db_config = {
342
+ "adapter" => "postgresql",
343
+ "encoding" => "unicode",
344
+ "host" => ENV.fetch("DATABASE_HOST", "localhost"),
345
+ "port" => ENV.fetch("DATABASE_PORT", "5432").to_i,
346
+ "username" => ENV.fetch("DATABASE_USERNAME", "decidim"),
347
+ "password" => ENV.fetch("DATABASE_PASSWORD", "decidim")
348
+ }
349
+
350
+ dummy_root = File.expand_path("spec/decidim_dummy_app", __dir__)
351
+ patch_dummy_app_rails70!(dummy_root)
352
+ ensure_shakapacker_stub!(dummy_root)
353
+ prepare_dummy_decidim_boot_files!(dummy_root)
354
+ ruby_version_file = File.join(dummy_root, ".ruby-version")
355
+ if File.exist?(ruby_version_file)
356
+ File.write(ruby_version_file, "#{legacy_decidim? ? "3.2.2" : "3.4.7"}\n")
357
+ end
358
+
359
+ # System specs use HTTP; never leave force_ssl on in the dummy test env.
360
+ test_env = File.join(dummy_root, "config/environments/test.rb")
361
+ if File.exist?(test_env)
362
+ contents = File.read(test_env)
363
+ unless contents.include?("config.force_ssl")
364
+ File.write(
365
+ test_env,
366
+ contents.sub(
367
+ "Rails.application.configure do\n",
368
+ "Rails.application.configure do\n config.force_ssl = false\n"
369
+ )
370
+ )
371
+ end
372
+ end
373
+
374
+ config_file = File.join(dummy_root, "config/database.yml")
375
+ FileUtils.mkdir_p(File.dirname(config_file))
376
+ File.open(config_file, "w") do |f|
377
+ YAML.dump(
378
+ {
379
+ "test" => common_db_config.merge("database" => "#{base_app_name}_test_app_test"),
380
+ "development" => common_db_config.merge("database" => "#{base_app_name}_test_app_development")
381
+ },
382
+ f
383
+ )
384
+ end
385
+
386
+ prior_database_url = ENV.delete("DATABASE_URL")
387
+ prior_rails_env = ENV.fetch("RAILS_ENV", nil)
388
+ prior_disable_spring = ENV.fetch("DISABLE_SPRING", nil)
389
+ prior_db_env_check = ENV.fetch("DISABLE_DATABASE_ENVIRONMENT_CHECK", nil)
390
+ begin
391
+ ENV["RAILS_ENV"] = "test"
392
+ ENV["DISABLE_SPRING"] = "1"
393
+ ENV["DISABLE_DATABASE_ENVIRONMENT_CHECK"] = "1"
394
+
395
+ run!(dummy_root, "bundle exec rake decidim:choose_target_plugins")
396
+ prepare_dummy_decidim_boot_files!(dummy_root)
397
+ run!(dummy_root, "bundle exec rails railties:install:migrations")
398
+ prepare_dummy_decidim_boot_files!(dummy_root)
399
+ purge_non_legacy_dummy_migrations!(dummy_root) if legacy_decidim?
400
+
401
+ normalize_variant_migrations!(dummy_root)
402
+
403
+ run!(dummy_root, "bundle exec rake db:drop db:create db:migrate db:schema:dump")
404
+ prepare_dummy_decidim_boot_files!(dummy_root)
405
+
406
+ # Rails 7.0 Active Storage may not ship variant_records; Decidim attachments expect it.
407
+ ensure_variant_records_table!(dummy_root) if legacy_decidim?
408
+ ensure
409
+ restore_env!("RAILS_ENV", prior_rails_env)
410
+ ENV["DATABASE_URL"] = prior_database_url if prior_database_url
411
+ restore_env!("DISABLE_SPRING", prior_disable_spring)
412
+ restore_env!("DISABLE_DATABASE_ENVIRONMENT_CHECK", prior_db_env_check)
413
+ end
414
+ end
415
+
416
+ def ensure_variant_records_table!(dummy_root)
417
+ run!(
418
+ dummy_root,
419
+ "bundle exec rails runner \"" \
420
+ "unless ActiveRecord::Base.connection.table_exists?(:active_storage_variant_records); " \
421
+ "ActiveRecord::Base.connection.create_table(:active_storage_variant_records) do |t|; " \
422
+ "t.belongs_to :blob, null: false, index: false; t.string :variation_digest, null: false; " \
423
+ "t.index %i[blob_id variation_digest], name: 'index_active_storage_variant_records_uniqueness', unique: true; " \
424
+ "end; end\""
425
+ )
426
+ end
427
+
428
+ def restore_env!(key, prior)
429
+ if prior
430
+ ENV[key] = prior
431
+ else
432
+ ENV.delete(key)
433
+ end
434
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/translatable_attributes"
4
+
5
+ module Decidim
6
+ module Zitadel
7
+ module Admin
8
+ class ConfigForm < Decidim::Form
9
+ include Decidim::TranslatableAttributes
10
+ include Decidim::Toggle::TabForm
11
+ include Decidim::Toggle::ModuleConfigForm
12
+
13
+ self.module_config_name = MODULE_NAME
14
+
15
+ mimic :organization
16
+
17
+ attribute :enabled, :boolean, default: false
18
+ attribute :issuer_url, :string
19
+ attribute :client_id, :string
20
+ attribute :client_secret, :string
21
+ attribute :scopes, :string, default: "openid email profile"
22
+ attribute :profile_url, :string
23
+ translatable_attribute :omniauth_button_label, String
24
+
25
+ validates :issuer_url, presence: true, url: true, if: :enabled?
26
+ validates :client_id, presence: true, if: :enabled?
27
+ validates :client_secret, presence: true, if: :secret_required?
28
+ validates :profile_url, url: true, if: -> { profile_url.present? }
29
+ validate :discover_issuer, if: :discoverable?
30
+
31
+ disable :issuer_url, if_unchecked: :enabled
32
+ disable :client_id, if_unchecked: :enabled
33
+ disable :client_secret, if_unchecked: :enabled
34
+ disable :scopes, if_unchecked: :enabled
35
+ disable :profile_url, if_unchecked: :enabled
36
+ disable :omniauth_button_label, if_unchecked: :enabled
37
+
38
+ def enabled?
39
+ ActiveModel::Type::Boolean.new.cast(enabled)
40
+ end
41
+
42
+ # Toggle main persists +form.to_h+ (discovery keys + omit blank secret).
43
+ def to_h
44
+ persistable_config
45
+ end
46
+
47
+ private
48
+
49
+ def persistable_config
50
+ return { "enabled" => false } unless enabled?
51
+
52
+ enabled_config.merge(@discovered || {})
53
+ end
54
+
55
+ def enabled_config
56
+ {
57
+ "enabled" => true,
58
+ "issuer_url" => issuer_url,
59
+ "client_id" => client_id,
60
+ "scopes" => scopes,
61
+ "profile_url" => profile_url.to_s,
62
+ "omniauth_button_label" => omniauth_button_label_payload
63
+ }.tap { |hash| hash["client_secret"] = client_secret if client_secret.present? }
64
+ end
65
+
66
+ def omniauth_button_label_payload
67
+ (omniauth_button_label || {}).stringify_keys
68
+ end
69
+
70
+ def discoverable?
71
+ enabled? && issuer_url.present?
72
+ end
73
+
74
+ def discover_issuer
75
+ @discovered = Discovery.new(issuer_url).call
76
+ errors.add(:issuer_url, :discovery_failed) if @discovered.blank?
77
+ end
78
+
79
+ def secret_required?
80
+ enabled? && stored_secret.blank?
81
+ end
82
+
83
+ def stored_secret
84
+ Decidim::Toggle.config_for(context&.current_organization, MODULE_NAME)[:client_secret]
85
+ rescue StandardError
86
+ nil
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Zitadel
5
+ # View helper for Deface OmniAuth button partials.
6
+ module OmniauthButtonHelper
7
+ include Decidim::TranslatableAttributes
8
+ include Decidim::OmniauthHelper
9
+
10
+ def zitadel_omniauth_button_label(provider = PROVIDER, organization = nil)
11
+ organization ||= current_organization if respond_to?(:current_organization, true)
12
+ label = Availability.config_for(organization)[:omniauth_button_label]
13
+ translated_attribute(label, organization).presence ||
14
+ normalize_provider_name(provider).titleize
15
+ end
16
+ end
17
+ end
18
+ end