Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Adds the preliminar check for an existing token (#17999)

Marcello Rocha1fbb5442f4d0
diff --git a/modules/openid_connect/app/models/openid_connect/user_token.rb b/modules/openid_connect/app/models/openid_connect/user_token.rb
index 485d14b971a..81abf71d852 100644
--- a/modules/openid_connect/app/models/openid_connect/user_token.rb
+++ b/modules/openid_connect/app/models/openid_connect/user_token.rb
@@ -37,5 +37,6 @@ module OpenIDConnect
belongs_to :user
scope :idp, -> { where(audience: IDP_AUDIENCE) }
+ scope :with_audience, ->(audience) { where("audiences ? :aud", aud: audience) }
end
end
diff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb
index 2baa1a62ea0..aaad3a85738 100644
--- a/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb
+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb
@@ -85,7 +85,7 @@ module OpenIDConnect
private
def token_with_audience(aud)
- token = @user.oidc_user_tokens.where("audiences ? :aud", aud:).first
+ token = @user.oidc_user_tokens.with_audience(aud).first
return Success(token) if token
return @token_exchange.call(aud) if @token_exchange.supported?
diff --git a/modules/openid_connect/spec/factories/oidc_user_token_factory.rb b/modules/openid_connect/spec/factories/oidc_user_token_factory.rb
new file mode 100644
index 00000000000..7c3ede38c5c
--- /dev/null
+++ b/modules/openid_connect/spec/factories/oidc_user_token_factory.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+FactoryBot.define do
+ factory :oidc_user_token, class: "OpenIDConnect::UserToken" do
+ transient do
+ extra_audiences { nil }
+ end
+
+ user
+ access_token { "INVALID_TOKEN" }
+ refresh_token { "COOL_AID_TOKEN" }
+ audiences { [OpenIDConnect::UserToken::IDP_AUDIENCE] + Array(extra_audiences) }
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb b/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb
index f07cd169385..43c82566e17 100644
--- a/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb
+++ b/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb
@@ -58,7 +58,7 @@ module Storages
def sso_misconfigured
return None() unless @storage.authenticate_via_idp?
- non_provisioned_user.or { non_oidc_provisioned_user }
+ non_provisioned_user.or { non_oidc_provisioned_user }.or { token_usability }
end
def non_provisioned_user
@@ -70,6 +70,20 @@ module Storages
description: I18n.t("storages.health.connection_validation.oidc_non_provisioned_user")))
end
+ def token_usability
+ usable_token = @user.oidc_user_tokens.with_audience(@storage.audience).first
+ return None() if usable_token
+
+ # Temporary error as I implement the next steps
+ Some(ConnectionValidation
+ .new(type: :error,
+ error_code: :oidc_no_token_with_required_audience,
+ timestamp: Time.current,
+ description: I18n.t(
+ "storages.health.connection_validation.oidc_no_token_with_required_audience", audience: @storage.audience
+ )))
+ end
+
def non_oidc_provisioned_user
return None() if @user.authentication_provider.is_a?(OpenIDConnect::Provider)
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index a1da54a66cc..90a3ddf3106 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -233,6 +233,7 @@ en:
host_not_found: No Nextcloud server found at the configured host url. Please check the configuration.
missing_dependencies: 'A required dependency is missing on the file storage. Please add the following dependency: %{dependency}.'
not_configured: The connection could not be validated. Please finish configuration first.
+ oidc_no_token_with_required_audience: There's no OpenID Connect token with the required %{audience} audience.
oidc_non_oidc_user: The current user, while provisioned, wasn't provisioned by an OpenID Connect (OIDC) Identity Provider. Please re-run the check with an OIDC provisioned user.
oidc_non_provisioned_user: The current user isn't provided by an OpenID Connect Identity Provider. Please re-run the check with a provided user.
placeholder: Check your connection against the server.
diff --git a/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb
index ed7a3092c68..867502b3597 100644
--- a/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb
@@ -248,7 +248,8 @@ RSpec.describe Storages::Peripherals::NextcloudConnectionValidator do
before { User.current = user }
it "returns a success" do
- user.update(identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER")
+ create(:oidc_user_token, user:, extra_audiences: storage.audience)
+ user.update!(identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER")
expect(subject.type).to eq(:healthy)
expect(subject.error_code).to eq(:none)
@@ -269,12 +270,21 @@ RSpec.describe Storages::Peripherals::NextcloudConnectionValidator do
end
it "returns a warning if the user is not provided by an oidc provider" do
- user.update(identity_url: "ldap-provider:this-will-trigger-a-warning")
+ user.update!(identity_url: "ldap-provider:this-will-trigger-a-warning")
expect(subject.type).to eq(:warning)
expect(subject.error_code).to eq(:oidc_non_oidc_user)
expect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_non_oidc_user"))
end
+
+ it "returns an error if the user does not have a usable token" do
+ user.update!(identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER")
+
+ expect(subject.type).to eq(:error)
+ expect(subject.error_code).to eq(:oidc_no_token_with_required_audience)
+ expect(subject.description)
+ .to eq(I18n.t("storages.health.connection_validation.oidc_no_token_with_required_audience", audience: storage.audience))
+ end
end
private