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.
- checksums.yaml +7 -0
- data/LICENSE.md +661 -0
- data/README.md +115 -0
- data/Rakefile +434 -0
- data/app/forms/decidim/zitadel/admin/config_form.rb +91 -0
- data/app/helpers/decidim/zitadel/omniauth_button_helper.rb +18 -0
- data/app/overrides/decidim_zitadel_account_profile.rb +51 -0
- data/app/overrides/decidim_zitadel_login.rb +35 -0
- data/app/overrides/decidim_zitadel_omniauth_buttons.rb +74 -0
- data/app/views/decidim/zitadel/deface_partials/_account_profile.html.erb +12 -0
- data/app/views/decidim/zitadel/deface_partials/_header_login.html.erb +7 -0
- data/app/views/decidim/zitadel/deface_partials/_omniauth_button_label.html.erb +8 -0
- data/config/locales/de.yml +50 -0
- data/config/locales/en.yml +50 -0
- data/config/locales/fr.yml +50 -0
- data/config/locales/it.yml +50 -0
- data/db/migrate/20260101000001_create_decidim_zitadel_sessions.rb +17 -0
- data/db/seeds.rb +3 -0
- data/lib/decidim/zitadel/account_form_freeze.rb +35 -0
- data/lib/decidim/zitadel/auth_attributes.rb +50 -0
- data/lib/decidim/zitadel/availability.rb +35 -0
- data/lib/decidim/zitadel/complete_session.rb +28 -0
- data/lib/decidim/zitadel/discovery.rb +58 -0
- data/lib/decidim/zitadel/email_linker.rb +43 -0
- data/lib/decidim/zitadel/engine/overrides.rb +23 -0
- data/lib/decidim/zitadel/engine.rb +64 -0
- data/lib/decidim/zitadel/http_clients.rb +18 -0
- data/lib/decidim/zitadel/identity_resolver.rb +44 -0
- data/lib/decidim/zitadel/invite_csv.rb +60 -0
- data/lib/decidim/zitadel/oidc_end_session.rb +79 -0
- data/lib/decidim/zitadel/omniauth_registrations_override.rb +114 -0
- data/lib/decidim/zitadel/omniauth_setup.rb +137 -0
- data/lib/decidim/zitadel/organization_omniauth.rb +14 -0
- data/lib/decidim/zitadel/profile_completion.rb +65 -0
- data/lib/decidim/zitadel/profile_completion_guard.rb +60 -0
- data/lib/decidim/zitadel/profile_updater.rb +49 -0
- data/lib/decidim/zitadel/registrations_override.rb +9 -0
- data/lib/decidim/zitadel/seed_hook.rb +12 -0
- data/lib/decidim/zitadel/seeds.rb +42 -0
- data/lib/decidim/zitadel/sessions_override.rb +53 -0
- data/lib/decidim/zitadel/settings_tab.rb +18 -0
- data/lib/decidim/zitadel/sign_in_button_cache.rb +23 -0
- data/lib/decidim/zitadel/upgrade_hooks.rb +63 -0
- data/lib/decidim/zitadel/user_factory.rb +54 -0
- data/lib/decidim/zitadel/version.rb +13 -0
- data/lib/decidim/zitadel.rb +42 -0
- data/lib/decidim-zitadel.rb +3 -0
- data/lib/tasks/decidim_zitadel_tasks.rake +21 -0
- data/lib/tasks/decidim_zitadel_upgrade.rake +47 -0
- metadata +183 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
class Engine < ::Rails::Engine
|
|
6
|
+
def self.prepend_overrides
|
|
7
|
+
Decidim::Organization.prepend(OrganizationOmniauth) unless Decidim::Organization.ancestors.include?(OrganizationOmniauth)
|
|
8
|
+
unless Decidim::Devise::OmniauthRegistrationsController.ancestors.include?(
|
|
9
|
+
OmniauthRegistrationsOverride
|
|
10
|
+
)
|
|
11
|
+
Decidim::Devise::OmniauthRegistrationsController.prepend(OmniauthRegistrationsOverride)
|
|
12
|
+
end
|
|
13
|
+
Decidim::AccountForm.prepend(AccountFormFreeze) unless Decidim::AccountForm.ancestors.include?(AccountFormFreeze)
|
|
14
|
+
Decidim::Devise::SessionsController.prepend(SessionsOverride) unless Decidim::Devise::SessionsController.ancestors.include?(SessionsOverride)
|
|
15
|
+
Decidim::ApplicationController.prepend(ProfileCompletionGuard) unless Decidim::ApplicationController.ancestors.include?(ProfileCompletionGuard)
|
|
16
|
+
|
|
17
|
+
return if Decidim::Devise::RegistrationsController.ancestors.include?(RegistrationsOverride)
|
|
18
|
+
|
|
19
|
+
Decidim::Devise::RegistrationsController.prepend(RegistrationsOverride)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Zitadel
|
|
7
|
+
class Engine < ::Rails::Engine
|
|
8
|
+
isolate_namespace Decidim::Zitadel
|
|
9
|
+
|
|
10
|
+
initializer "decidim_zitadel.ignore_deface_overrides", before: :set_autoload_paths do
|
|
11
|
+
overrides = root.join("app/overrides")
|
|
12
|
+
Rails.autoloaders.main.ignore(overrides) if overrides.exist?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
initializer "decidim_zitadel.session_store", before: :setup_default_session_store do |app|
|
|
16
|
+
require "active_record/session_store"
|
|
17
|
+
app.config.session_store :active_record_store
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
initializer "decidim_zitadel.organization_settings_tab",
|
|
21
|
+
after: "decidim_toggle.organization_settings_tabs" do
|
|
22
|
+
Decidim::Zitadel::SettingsTab.register!
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
initializer "decidim_zitadel.omniauth" do
|
|
26
|
+
Decidim::Zitadel::OmniauthSetup.register_provider!
|
|
27
|
+
Decidim::Zitadel::HttpClients.configure!
|
|
28
|
+
Decidim::Zitadel::OmniauthSetup.mount!
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
initializer "decidim_zitadel.seed_hook" do
|
|
32
|
+
next if Decidim.singleton_class.ancestors.include?(Decidim::Zitadel::SeedHook)
|
|
33
|
+
|
|
34
|
+
Decidim.singleton_class.prepend(Decidim::Zitadel::SeedHook)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
initializer "decidim_zitadel.sign_in_button_cache" do
|
|
38
|
+
config.to_prepare do
|
|
39
|
+
model = Decidim::Toggle::OrganizationModuleConfig
|
|
40
|
+
next if model.include?(Decidim::Zitadel::SignInButtonCache)
|
|
41
|
+
|
|
42
|
+
model.include(Decidim::Zitadel::SignInButtonCache)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
initializer "decidim_zitadel.helpers" do
|
|
47
|
+
config.to_prepare do
|
|
48
|
+
ActiveSupport.on_load(:action_view) do
|
|
49
|
+
include Decidim::Zitadel::OmniauthButtonHelper
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
initializer "decidim_zitadel.overrides", after: :load_config_initializers do
|
|
55
|
+
config.to_prepare do
|
|
56
|
+
Decidim::Zitadel::Engine.prepend_overrides
|
|
57
|
+
ActionController::Base.prepend_view_path(Decidim::Zitadel::Engine.root.join("app/views"))
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
require_relative "engine/overrides"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
module HttpClients
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def configure!
|
|
9
|
+
host = ENV.fetch("ZITADEL_PUBLIC_HOST", nil)
|
|
10
|
+
return if host.blank?
|
|
11
|
+
|
|
12
|
+
header = { "Host" => host }
|
|
13
|
+
Rack::OAuth2.http_config { |faraday| faraday.headers.update(header) }
|
|
14
|
+
OpenIDConnect.http_config { |faraday| faraday.headers.update(header) }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
class IdentityResolver
|
|
6
|
+
def initialize(attrs, organization)
|
|
7
|
+
@attrs = attrs
|
|
8
|
+
@organization = organization
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def user
|
|
12
|
+
from_identity || link_confirmed || create_user
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def from_identity
|
|
18
|
+
identity = Decidim::Identity.find_by(
|
|
19
|
+
organization: @organization,
|
|
20
|
+
provider: PROVIDER,
|
|
21
|
+
uid: @attrs.sub
|
|
22
|
+
)
|
|
23
|
+
identity&.user
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def link_confirmed
|
|
27
|
+
EmailLinker.new(@attrs, @organization).call
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def create_user
|
|
31
|
+
return if @attrs.email.blank?
|
|
32
|
+
return if email_taken?
|
|
33
|
+
|
|
34
|
+
UserFactory.new(@attrs, @organization).call
|
|
35
|
+
rescue ActiveRecord::RecordInvalid
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def email_taken?
|
|
40
|
+
@organization.users.exists?(email: @attrs.email)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "csv"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Zitadel
|
|
7
|
+
class InviteCsv
|
|
8
|
+
HEADER = %w(email firstname lastname locale).freeze
|
|
9
|
+
|
|
10
|
+
def self.resolve_organization!(arg: nil, host: nil, id: nil)
|
|
11
|
+
host = arg.presence || host.presence
|
|
12
|
+
raise ArgumentError, "Pass organization host/id or set ORGANIZATION_HOST / ORGANIZATION_ID" if host.blank? && id.blank?
|
|
13
|
+
|
|
14
|
+
org = if id.present?
|
|
15
|
+
Decidim::Organization.find_by(id:)
|
|
16
|
+
else
|
|
17
|
+
Decidim::Organization.find_by(host:) || Decidim::Organization.find_by(id: host)
|
|
18
|
+
end
|
|
19
|
+
raise ActiveRecord::RecordNotFound, "Organization not found" if org.blank?
|
|
20
|
+
|
|
21
|
+
org
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize(organization)
|
|
25
|
+
@organization = organization
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def render
|
|
29
|
+
CSV.generate(col_sep: ";", encoding: "UTF-8") do |csv|
|
|
30
|
+
csv << HEADER
|
|
31
|
+
users.find_each { |user| csv << row_for(user) }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
attr_reader :organization
|
|
38
|
+
|
|
39
|
+
def users
|
|
40
|
+
organization.users.not_deleted.where.not(email: [nil, ""])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def row_for(user)
|
|
44
|
+
firstname, lastname = split_name(user.name)
|
|
45
|
+
[user.email, firstname, lastname, locale_for(user)]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def split_name(name)
|
|
49
|
+
parts = name.to_s.strip.split(/\s+/, 2)
|
|
50
|
+
given = parts[0].presence || "-"
|
|
51
|
+
family = parts[1].presence || "-"
|
|
52
|
+
[given, family]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def locale_for(user)
|
|
56
|
+
user.locale.presence || organization.default_locale.to_s
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Zitadel
|
|
7
|
+
# Builds the Zitadel OIDC RP-Initiated Logout URL for an organization.
|
|
8
|
+
class OidcEndSession
|
|
9
|
+
def self.url_for(organization:, protocol:, id_token_hint: nil, redirect_params: {})
|
|
10
|
+
new(
|
|
11
|
+
organization:,
|
|
12
|
+
protocol:,
|
|
13
|
+
id_token_hint:,
|
|
14
|
+
redirect_params:
|
|
15
|
+
).url
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(organization:, protocol:, id_token_hint: nil, redirect_params: {})
|
|
19
|
+
@organization = organization
|
|
20
|
+
@protocol = protocol
|
|
21
|
+
@id_token_hint = id_token_hint
|
|
22
|
+
@redirect_params = redirect_params
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def url
|
|
26
|
+
return unless ready?
|
|
27
|
+
|
|
28
|
+
uri = parsed_end_session_uri
|
|
29
|
+
return unless uri
|
|
30
|
+
|
|
31
|
+
uri.query = URI.encode_www_form(end_session_query)
|
|
32
|
+
uri.to_s
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
attr_reader :organization, :protocol, :id_token_hint, :redirect_params
|
|
38
|
+
|
|
39
|
+
def ready?
|
|
40
|
+
Availability.enabled?(organization) && end_session_endpoint.present?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def config
|
|
44
|
+
Availability.config_for(organization)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def end_session_endpoint
|
|
48
|
+
config[:end_session_endpoint].presence
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def parsed_end_session_uri
|
|
52
|
+
uri = URI.parse(end_session_endpoint.to_s)
|
|
53
|
+
return unless %w(http https).include?(uri.scheme)
|
|
54
|
+
|
|
55
|
+
uri
|
|
56
|
+
rescue URI::InvalidURIError
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def end_session_query
|
|
61
|
+
query = {
|
|
62
|
+
"client_id" => config[:client_id],
|
|
63
|
+
"post_logout_redirect_uri" => post_logout_redirect_uri
|
|
64
|
+
}
|
|
65
|
+
query["id_token_hint"] = id_token_hint if id_token_hint.present?
|
|
66
|
+
query
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def post_logout_redirect_uri
|
|
70
|
+
# Engine url_helpers do not inherit request host (ArgumentError in production).
|
|
71
|
+
Decidim::Core::Engine.routes.url_helpers.new_user_session_url(
|
|
72
|
+
host: organization.host,
|
|
73
|
+
protocol:,
|
|
74
|
+
**redirect_params
|
|
75
|
+
)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
module OmniauthRegistrationsOverride
|
|
6
|
+
def create
|
|
7
|
+
return super unless zitadel_provider?
|
|
8
|
+
|
|
9
|
+
CompleteSession.call(oauth_hash, current_organization) do
|
|
10
|
+
on(:ok) { |user| sign_in_and_redirect_user(user) }
|
|
11
|
+
on(:invalid) { fail_zitadel_sign_in }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def failure
|
|
16
|
+
return recover_zitadel_auth! if zitadel_failure?
|
|
17
|
+
|
|
18
|
+
super
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def zitadel_provider?
|
|
24
|
+
provider = oauth_hash[:provider].presence || params[:action]
|
|
25
|
+
provider.to_s == PROVIDER
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def zitadel_failure?
|
|
29
|
+
failed_strategy&.name.to_s == PROVIDER
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def fail_zitadel_sign_in
|
|
33
|
+
recover_zitadel_auth!(id_token_hint: oauth_id_token)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def recover_zitadel_auth!(id_token_hint: nil)
|
|
37
|
+
hint = id_token_hint.presence || session[:zitadel_id_token]
|
|
38
|
+
sign_out if current_user
|
|
39
|
+
reset_session
|
|
40
|
+
|
|
41
|
+
url = OidcEndSession.url_for(
|
|
42
|
+
organization: current_organization,
|
|
43
|
+
protocol: request.scheme,
|
|
44
|
+
id_token_hint: hint,
|
|
45
|
+
redirect_params: { auth_failed: 1 }
|
|
46
|
+
)
|
|
47
|
+
return redirect_to(url, allow_other_host: true) if url
|
|
48
|
+
|
|
49
|
+
flash[:alert] = I18n.t("decidim.zitadel.sessions.auth_failed")
|
|
50
|
+
redirect_to decidim.new_user_session_path
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def sign_in_and_redirect_user(user)
|
|
54
|
+
return unconfirmed_zitadel_user unless user.active_for_authentication?
|
|
55
|
+
|
|
56
|
+
sign_in user, event: :authentication
|
|
57
|
+
store_id_token
|
|
58
|
+
set_flash_message :notice, :success, kind: "Zitadel"
|
|
59
|
+
redirect_to zitadel_post_sign_in_path(user)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def zitadel_post_sign_in_path(user)
|
|
63
|
+
intended = zitadel_resolved_after_sign_in_path(user)
|
|
64
|
+
if ProfileCompletion.incomplete?(user)
|
|
65
|
+
store_location_for(user, intended) if intended.present?
|
|
66
|
+
return ProfileCompletion.completion_path
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
intended
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def zitadel_resolved_after_sign_in_path(user)
|
|
73
|
+
path = after_sign_in_path_for(user)
|
|
74
|
+
return path unless ProfileCompletion.available?
|
|
75
|
+
return path if ProfileCompletion.user_fields_enabled?(user.organization)
|
|
76
|
+
return path unless zitadel_authorization_first_login_path?(path)
|
|
77
|
+
|
|
78
|
+
stored_location_for(user).presence || decidim.root_path
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def zitadel_authorization_first_login_path?(path)
|
|
82
|
+
return false if path.blank?
|
|
83
|
+
|
|
84
|
+
path_str = path.to_s
|
|
85
|
+
return true if path_str.include?("/authorizations/first_login")
|
|
86
|
+
return true if path_str.include?("first_login_authorizations")
|
|
87
|
+
|
|
88
|
+
helpers = respond_to?(:decidim_verifications, true) ? decidim_verifications : nil
|
|
89
|
+
return false if helpers.nil? || !helpers.respond_to?(:first_login_authorizations_path)
|
|
90
|
+
|
|
91
|
+
path_str == helpers.first_login_authorizations_path.to_s
|
|
92
|
+
rescue StandardError
|
|
93
|
+
false
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def unconfirmed_zitadel_user
|
|
97
|
+
expire_data_after_sign_in!
|
|
98
|
+
redirect_to decidim.root_path
|
|
99
|
+
flash[:notice] = t("devise.registrations.signed_up_but_unconfirmed")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def store_id_token
|
|
103
|
+
token = oauth_id_token
|
|
104
|
+
session[:zitadel_id_token] = token if token.present?
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def oauth_id_token
|
|
108
|
+
oauth_hash.dig(:credentials, :id_token).presence ||
|
|
109
|
+
oauth_hash.dig(:extra, :id_token).presence ||
|
|
110
|
+
oauth_hash.dig(:extra, :raw_info, :id_token).presence
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
module OmniauthSetup
|
|
6
|
+
CONFIG_KEYS = {
|
|
7
|
+
issuer: :issuer_url,
|
|
8
|
+
client_options__identifier: :client_id,
|
|
9
|
+
client_options__secret: :client_secret,
|
|
10
|
+
client_options__authorization_endpoint: :authorization_endpoint,
|
|
11
|
+
client_options__token_endpoint: :token_endpoint,
|
|
12
|
+
client_options__userinfo_endpoint: :userinfo_endpoint,
|
|
13
|
+
client_options__jwks_uri: :jwks_uri,
|
|
14
|
+
client_options__end_session_endpoint: :end_session_endpoint
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def register_provider!
|
|
20
|
+
config = { enabled: true, icon: "login-circle-line" }
|
|
21
|
+
apply_provider_config(config)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def apply_provider_config(config)
|
|
25
|
+
if Gem.loaded_specs.fetch("decidim").version >= Gem::Version.new("0.32.0")
|
|
26
|
+
Decidim.omniauth_providers[:zitadel] = config
|
|
27
|
+
else
|
|
28
|
+
Rails.application.secrets[:omniauth] ||= {}
|
|
29
|
+
Rails.application.secrets[:omniauth][:zitadel] = config
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def mount!
|
|
34
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
|
35
|
+
provider :openid_connect, setup: OmniauthSetup.provider_setup, name: :zitadel
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def provider_setup
|
|
40
|
+
lambda do |env|
|
|
41
|
+
OmniauthSetup.assign_options(env)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def assign_options(env)
|
|
46
|
+
request = Rack::Request.new(env)
|
|
47
|
+
config = org_config(request)
|
|
48
|
+
apply_mapping(env, config)
|
|
49
|
+
apply_scope(env, config)
|
|
50
|
+
apply_static_options(env, request)
|
|
51
|
+
apply_prompt(env, request)
|
|
52
|
+
apply_ui_locales(env, request)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def org_config(request)
|
|
56
|
+
organization = Decidim::Organization.find_by(host: request.host)
|
|
57
|
+
Availability.config_for(organization)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def apply_mapping(env, config)
|
|
61
|
+
CONFIG_KEYS.each do |option_key, config_key|
|
|
62
|
+
value = config[config_key]
|
|
63
|
+
next if value.blank?
|
|
64
|
+
|
|
65
|
+
write_option(env, option_key, value)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def write_option(env, option_key, value)
|
|
70
|
+
if option_key.to_s.include?("__")
|
|
71
|
+
parent, key = option_key.to_s.split("__", 2)
|
|
72
|
+
env["omniauth.strategy"].options[parent.to_sym] ||= {}
|
|
73
|
+
env["omniauth.strategy"].options[parent.to_sym][key.to_sym] = value
|
|
74
|
+
else
|
|
75
|
+
env["omniauth.strategy"].options[option_key] = value
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def apply_scope(env, config)
|
|
80
|
+
env["omniauth.strategy"].options[:scope] =
|
|
81
|
+
(config[:scopes].presence || "openid email profile").split
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def apply_static_options(env, request)
|
|
85
|
+
strategy = env["omniauth.strategy"]
|
|
86
|
+
strategy.options[:discovery] = false
|
|
87
|
+
strategy.options[:response_type] = :code
|
|
88
|
+
strategy.options[:uid_field] = "sub"
|
|
89
|
+
strategy.options[:client_options] ||= {}
|
|
90
|
+
strategy.options[:client_options][:redirect_uri] = redirect_uri(request)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def apply_prompt(env, request)
|
|
94
|
+
merge_authorize_param(env, :prompt, request.params["prompt"])
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def apply_ui_locales(env, request)
|
|
98
|
+
locale = ui_locales_for(request)
|
|
99
|
+
return if locale.blank?
|
|
100
|
+
|
|
101
|
+
write_ui_locales(env, locale)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def ui_locales_for(request)
|
|
105
|
+
session_locale(request) ||
|
|
106
|
+
I18n.locale.presence&.to_s ||
|
|
107
|
+
org_default_locale(request)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def session_locale(request)
|
|
111
|
+
request.env["rack.session"]&.[](:user_locale).presence&.to_s
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def org_default_locale(request)
|
|
115
|
+
Decidim::Organization.find_by(host: request.host)&.default_locale&.to_s
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def write_ui_locales(env, locale)
|
|
119
|
+
strategy = env["omniauth.strategy"]
|
|
120
|
+
strategy.options[:ui_locales] = locale
|
|
121
|
+
extra = strategy.options[:extra_authorize_params] || {}
|
|
122
|
+
strategy.options[:extra_authorize_params] = extra.merge(ui_locales: locale)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def merge_authorize_param(env, key, value)
|
|
126
|
+
return if value.blank?
|
|
127
|
+
|
|
128
|
+
current = env["omniauth.strategy"].options[:authorize_params] || {}
|
|
129
|
+
env["omniauth.strategy"].options[:authorize_params] = current.merge(key => value)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def redirect_uri(request)
|
|
133
|
+
"#{request.base_url}/users/auth/zitadel/callback"
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
module OrganizationOmniauth
|
|
6
|
+
def enabled_omniauth_providers
|
|
7
|
+
providers = super
|
|
8
|
+
return providers.slice(:zitadel, "zitadel") if Availability.enabled?(self)
|
|
9
|
+
|
|
10
|
+
providers.except(:zitadel, "zitadel")
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
# Policy for required decidim-user_fields registration values when Zitadel is
|
|
6
|
+
# enabled for the organization. No HTTP — used by the guard and OmniAuth redirect.
|
|
7
|
+
module ProfileCompletion
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def available?
|
|
11
|
+
Decidim::Toggle.gem_present?("decidim-user_fields")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def user_fields_enabled?(organization)
|
|
15
|
+
return false unless available?
|
|
16
|
+
return false if organization.blank?
|
|
17
|
+
|
|
18
|
+
registration_fields.enabled_customization_names(organization).any?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def required_for?(user)
|
|
22
|
+
return false if user.blank?
|
|
23
|
+
|
|
24
|
+
organization = user.organization
|
|
25
|
+
return false if organization.blank?
|
|
26
|
+
return false unless Availability.enabled?(organization)
|
|
27
|
+
return false unless user_fields_enabled?(organization)
|
|
28
|
+
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def missing_fields(user)
|
|
33
|
+
return [] unless required_for?(user)
|
|
34
|
+
return registration_fields.missing_required(user) if registration_fields.respond_to?(:missing_required)
|
|
35
|
+
|
|
36
|
+
# ponytail: local filter until hosts run user_fields with missing_required; delete when the gem pin requires it
|
|
37
|
+
registration_fields.active_registration_fields(user.organization).select do |field|
|
|
38
|
+
field.options[:required] && blank_stored?(user, field)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def incomplete?(user)
|
|
43
|
+
missing_fields(user).any?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def completion_path
|
|
47
|
+
return registration_fields.completion_path if registration_fields.respond_to?(:completion_path)
|
|
48
|
+
|
|
49
|
+
# ponytail: account_path until hosts run user_fields with completion_path; delete when the gem pin requires it
|
|
50
|
+
Decidim::Core::Engine.routes.url_helpers.account_path
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def blank_stored?(user, field)
|
|
54
|
+
value = (user.extended_data || {})[field.name.to_s]
|
|
55
|
+
value = value.strip if value.is_a?(String)
|
|
56
|
+
value.blank?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def registration_fields
|
|
60
|
+
Decidim::CustomUserFields::RegistrationFields
|
|
61
|
+
end
|
|
62
|
+
private_class_method :registration_fields
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Zitadel
|
|
5
|
+
# Redirects signed-in users with missing required user_fields to the completion form.
|
|
6
|
+
module ProfileCompletionGuard
|
|
7
|
+
def self.prepended(base)
|
|
8
|
+
base.before_action :enforce_zitadel_profile_completion, if: :user_signed_in?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def enforce_zitadel_profile_completion
|
|
14
|
+
return unless ProfileCompletion.incomplete?(current_user)
|
|
15
|
+
return if zitadel_profile_completion_allowed?
|
|
16
|
+
|
|
17
|
+
redirect_to ProfileCompletion.completion_path
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def zitadel_profile_completion_allowed?
|
|
21
|
+
return true if zitadel_profile_completion_form_action?
|
|
22
|
+
return true if zitadel_profile_completion_target_request?
|
|
23
|
+
return true if zitadel_profile_completion_session_destroy?
|
|
24
|
+
return true if zitadel_profile_completion_assetish_request?
|
|
25
|
+
|
|
26
|
+
false
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def zitadel_profile_completion_form_action?
|
|
30
|
+
return false unless self.class.name.to_s == "Decidim::CustomUserFields::ProfileCompletionsController"
|
|
31
|
+
|
|
32
|
+
%w(show update).include?(action_name)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Allow the resolved completion path (e.g. /account when user_fields has no completion_path).
|
|
36
|
+
def zitadel_profile_completion_target_request?
|
|
37
|
+
target = ProfileCompletion.completion_path.to_s.split("?", 2).first
|
|
38
|
+
return false if target.blank?
|
|
39
|
+
|
|
40
|
+
request.path == target || request.path.match?(%r{\A/[a-z]{2}(?:-[A-Z]{2})?#{Regexp.escape(target)}\z})
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def zitadel_profile_completion_session_destroy?
|
|
44
|
+
return true if is_a?(Decidim::Devise::SessionsController) && action_name == "destroy"
|
|
45
|
+
return true if self.class.name.to_s.end_with?("SessionsController") && action_name == "destroy"
|
|
46
|
+
|
|
47
|
+
false
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def zitadel_profile_completion_assetish_request?
|
|
51
|
+
return true unless request.format.html?
|
|
52
|
+
return true if request.path.start_with?("/rails/")
|
|
53
|
+
return true if request.path.start_with?("/assets/")
|
|
54
|
+
return true if request.path.include?("download_your_data")
|
|
55
|
+
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|