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

[#61178] Update RemoteIdentity after token obtained.

Pavel Balashou2277dec69f67
diff --git a/app/models/oauth_client.rb b/app/models/oauth_client.rb
index 60b01ed552f..63cd63472ef 100644
--- a/app/models/oauth_client.rb
+++ b/app/models/oauth_client.rb
@@ -30,7 +30,7 @@
class OAuthClient < ApplicationRecord
belongs_to :integration, polymorphic: true
- has_many :remote_identities, dependent: :destroy
+ has_many :remote_identities, as: :auth_source, dependent: :destroy
has_many :oauth_client_tokens, dependent: :destroy
def redirect_uri
diff --git a/app/models/oauth_client_token.rb b/app/models/oauth_client_token.rb
index a1baede5100..7bbbedaf660 100644
--- a/app/models/oauth_client_token.rb
+++ b/app/models/oauth_client_token.rb
@@ -28,13 +28,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-# OAuthClientToken stores the OAuth2 Bearer+Refresh tokens that
-# an OAuth2 server (Nextcloud or similar) provides after a user
-# has granted access.
class OAuthClientToken < ApplicationRecord
- # OAuthClientToken sits between User and OAuthClient
belongs_to :user, optional: false
belongs_to :oauth_client, optional: false
+ alias_method :auth_source, :oauth_client
validates :user, uniqueness: { scope: :oauth_client }
diff --git a/app/models/remote_identity.rb b/app/models/remote_identity.rb
index 94ac387f93a..2c4a8d6e62e 100644
--- a/app/models/remote_identity.rb
+++ b/app/models/remote_identity.rb
@@ -30,8 +30,9 @@
class RemoteIdentity < ApplicationRecord
belongs_to :user
- belongs_to :oauth_client, class_name: "OAuthClient"
+ belongs_to :auth_source, polymorphic: true
+ belongs_to :integration, polymorphic: true
- validates :user, uniqueness: { scope: :oauth_client }
- validates :origin_user_id, :user, :oauth_client, presence: true
+ validates :user, uniqueness: { scope: %i[auth_source integration] }
+ validates :origin_user_id, :user, :auth_source, :integration, presence: true
end
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index e18b5b5ff59..f5c75376fc5 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -115,7 +115,7 @@ module OAuthClients
oauth_client_token.save!
RemoteIdentities::CreateService
- .call(user: @user, oauth_config: @config, oauth_client_token:)
+ .call(user: @user, integration: @oauth_client.integration, token: oauth_client_token)
.on_failure { raise ActiveRecord::Rollback }
end
diff --git a/app/services/remote_identities/create_service.rb b/app/services/remote_identities/create_service.rb
index 3542a8f9925..dc9409f5cbb 100644
--- a/app/services/remote_identities/create_service.rb
+++ b/app/services/remote_identities/create_service.rb
@@ -32,35 +32,31 @@ module RemoteIdentities
class CreateService
attr_reader :user, :model
- def self.call(user:, oauth_config:, oauth_client_token:)
- new(user:, oauth_config:, oauth_client_token:).call
+ def self.call(user:, integration:, token:)
+ new(user:, integration:, token:).call
end
- def initialize(user:, oauth_config:, oauth_client_token:)
+ def initialize(user:, integration:, token:)
@user = user
- @oauth_config = oauth_config
- @oauth_client_token = oauth_client_token
+ @integration = integration
+ @token = token
- @model = RemoteIdentity.find_or_initialize_by(user:, oauth_client: oauth_config.oauth_client)
+ @model = RemoteIdentity.find_or_initialize_by(user:, auth_source: token.auth_source, integration:)
@result = ServiceResult.success(result: @model, errors: @model.errors)
end
def call
- @model.origin_user_id = @oauth_config.extract_origin_user_id(@oauth_client_token)
+ @model.origin_user_id = @integration.extract_origin_user_id(@token)
if @model.save
- emit_event(@oauth_config.oauth_client.integration)
+ OpenProject::Notifications.send(
+ OpenProject::Events::REMOTE_IDENTITY_CREATED,
+ integration: @integration
+ )
else
@result.success = false
end
@result
end
-
- def emit_event(integration)
- OpenProject::Notifications.send(
- OpenProject::Events::REMOTE_IDENTITY_CREATED,
- integration:
- )
- end
end
end
diff --git a/db/migrate/20250220123358_add_polymorphic_auth_source_and_integration_to_remote_identities.rb b/db/migrate/20250220123358_add_polymorphic_auth_source_and_integration_to_remote_identities.rb
new file mode 100644
index 00000000000..5aaf92b226f
--- /dev/null
+++ b/db/migrate/20250220123358_add_polymorphic_auth_source_and_integration_to_remote_identities.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class AddPolymorphicAuthSourceAndIntegrationToRemoteIdentities < ActiveRecord::Migration[7.1]
+ def up
+ remove_foreign_key :remote_identities, :oauth_clients
+
+ rename_column :remote_identities, :oauth_client_id, :auth_source_id
+ add_column :remote_identities, :auth_source_type, :string
+ remove_index :remote_identities, :auth_source_id, if_exists: true
+ add_index :remote_identities, %i[auth_source_type auth_source_id]
+
+ add_column :remote_identities, :integration_type, :string
+ add_column :remote_identities, :integration_id, :bigint
+ add_index :remote_identities, %i[integration_type integration_id]
+
+ execute <<~SQL.squish
+ UPDATE remote_identities
+ SET auth_source_type = 'OAuthClient',
+ integration_id = oauth_clients.integration_id,
+ integration_type = oauth_clients.integration_type
+ FROM oauth_clients
+ WHERE remote_identities.auth_source_id = oauth_clients.id;
+ SQL
+
+ change_column_null(:remote_identities, :integration_id, false)
+ change_column_null(:remote_identities, :integration_type, false)
+ change_column_null(:remote_identities, :auth_source_type, false)
+
+ add_index(:remote_identities,
+ %i[user_id auth_source_type auth_source_id integration_id integration_type],
+ unique: true)
+ end
+
+ def down
+ remove_index(:remote_identities,
+ %i[user_id auth_source_type auth_source_id integration_id integration_type])
+ rename_column :remote_identities, :auth_source_id, :oauth_client_id
+ add_foreign_key :remote_identities, :oauth_clients
+ add_index :remote_identities, :oauth_client_id
+ remove_column :remote_identities, :auth_source_type
+
+ remove_column :remote_identities, :integration_id
+ remove_column :remote_identities, :integration_type
+ end
+end
diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rb
index ae0046f75b7..7c51c9c2888 100644
--- a/modules/openid_connect/app/models/openid_connect/provider.rb
+++ b/modules/openid_connect/app/models/openid_connect/provider.rb
@@ -1,7 +1,11 @@
+# frozen_string_literal: true
+
module OpenIDConnect
class Provider < AuthProvider
include HashBuilder
+ has_many :remote_identities, as: :auth_source, dependent: :destroy
+
OIDC_PROVIDERS = %w[google microsoft_entra custom].freeze
DISCOVERABLE_STRING_ATTRIBUTES_MANDATORY = %i[authorization_endpoint
userinfo_endpoint
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 81abf71d852..8f4a0cbfaea 100644
--- a/modules/openid_connect/app/models/openid_connect/user_token.rb
+++ b/modules/openid_connect/app/models/openid_connect/user_token.rb
@@ -38,5 +38,9 @@ module OpenIDConnect
scope :idp, -> { where(audience: IDP_AUDIENCE) }
scope :with_audience, ->(audience) { where("audiences ? :aud", aud: audience) }
+
+ def auth_source
+ user.authentication_provider
+ end
end
end
diff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb
index 60670b3b99c..2f803d6357f 100644
--- a/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb
+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb
@@ -44,6 +44,8 @@ module OpenIDConnect
end
end
+ attr_reader :user
+
def initialize(user:)
@user = user
end
@@ -51,7 +53,7 @@ module OpenIDConnect
def call(audience)
return Failure("Provider does not support token exchange") unless supported?
- idp_token = yield FetchService.new(user: @user, token_exchange: Disabled)
+ idp_token = yield FetchService.new(user:, token_exchange: Disabled)
.access_token_for(audience: UserToken::IDP_AUDIENCE)
json = yield TokenRequest.new(provider:).exchange(idp_token, audience)
@@ -65,6 +67,7 @@ module OpenIDConnect
# A second reason is that at least Keycloak (an IDP we implement against), offers broken
# refresh tokens after token exchange (see https://github.com/keycloak/keycloak/issues/37016)
token = store_exchanged_token(audience:, access_token:, refresh_token: nil, expires_in:)
+
Success(token)
end
@@ -76,7 +79,7 @@ module OpenIDConnect
def store_exchanged_token(audience:, access_token:, refresh_token:, expires_in:)
token_data = { access_token:, refresh_token:, expires_at: expires_in&.seconds&.from_now }
- token = @user.oidc_user_tokens.where("audiences ? :audience", audience:).first
+ token = user.oidc_user_tokens.where("audiences ? :audience", audience:).first
if token
if token.audiences.size > 1
raise "Did not expect to update token with multiple audiences (#{token.audiences}) in-place."
@@ -84,14 +87,14 @@ module OpenIDConnect
token.update!(**token_data)
else
- token = @user.oidc_user_tokens.create!(audiences: [audience], **token_data)
+ token = user.oidc_user_tokens.create!(audiences: [audience], **token_data)
end
token
end
def provider
- @user.authentication_provider
+ user.authentication_provider
end
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 79705fe0c44..bfdc70622fa 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
@@ -38,6 +38,10 @@ module OpenIDConnect
include Dry::Monads[:result]
include Dry::Monads::Do.for(:access_token_for, :refreshed_access_token_for)
+ TOKEN_OBTAINED = "access_token_obtained"
+
+ attr_reader :user
+
def initialize(user:,
jwt_parser: JwtParser.new(verify_audience: false, verify_expiration: false),
token_exchange: ExchangeService.new(user:),
@@ -63,6 +67,7 @@ module OpenIDConnect
token = yield token_with_audience(audience)
token = yield @token_refresh.call(token) if expired?(token)
+ emit_event(token, audience)
Success(token.access_token)
end
@@ -79,11 +84,21 @@ module OpenIDConnect
def refreshed_access_token_for(audience:)
token = yield token_with_audience(audience)
token = yield @token_refresh.call(token)
+
+ emit_event(token, audience)
Success(token.access_token)
end
private
+ def emit_event(token, audience)
+ OpenProject::Notifications.send(
+ TOKEN_OBTAINED,
+ audience:,
+ token:
+ )
+ end
+
def token_with_audience(aud)
token = @user.oidc_user_tokens.with_audience(aud).first
return Success(token) if token
diff --git a/modules/storages/app/common/storages/peripherals/nextcloud_registry.rb b/modules/storages/app/common/storages/peripherals/nextcloud_registry.rb
index a5842ff819a..f1ec42c1045 100644
--- a/modules/storages/app/common/storages/peripherals/nextcloud_registry.rb
+++ b/modules/storages/app/common/storages/peripherals/nextcloud_registry.rb
@@ -88,6 +88,7 @@ module Storages
namespace("authentication") do
register(:userless, StorageInteraction::AuthenticationStrategies::NextcloudStrategies::UserLess, call: false)
register(:user_bound, StorageInteraction::AuthenticationStrategies::NextcloudStrategies::UserBound)
+ register(:bearer_token, StorageInteraction::AuthenticationStrategies::NextcloudStrategies::BearerToken)
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
index dde493e85d2..463d1824e06 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
@@ -43,19 +43,6 @@ module Storages
def authorization_uri(state: nil)
basic_rack_oauth_client.authorization_uri(scope:, state:)
end
-
- def extract_origin_user_id(oauth_client_token)
- auth_strategy = Registry
- .resolve("#{@storage}.authentication.user_bound")
- .call(user: oauth_client_token.user, storage: @storage)
- Registry
- .resolve("#{@storage}.queries.user")
- .call(auth_strategy:, storage: @storage)
- .match(
- on_success: ->(user) { user[:id] },
- on_failure: ->(error) { raise "UserQuery responed with #{error}" }
- )
- end
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/one_drive_registry.rb b/modules/storages/app/common/storages/peripherals/one_drive_registry.rb
index 6da98cbb453..3d2f753939a 100644
--- a/modules/storages/app/common/storages/peripherals/one_drive_registry.rb
+++ b/modules/storages/app/common/storages/peripherals/one_drive_registry.rb
@@ -79,6 +79,7 @@ module Storages
namespace("authentication") do
register(:userless, StorageInteraction::AuthenticationStrategies::OneDriveStrategies::UserLess, call: false)
register(:user_bound, StorageInteraction::AuthenticationStrategies::OneDriveStrategies::UserBound)
+ register(:bearer_token, StorageInteraction::AuthenticationStrategies::OneDriveStrategies::BearerToken)
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
index c592e65b22f..5a83af9f697 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
@@ -42,6 +42,8 @@ module Storages
AuthenticationStrategies::Failure.new
when :basic_auth
AuthenticationStrategies::BasicAuth.new
+ when :bearer_token
+ AuthenticationStrategies::BearerToken.new(strategy.token)
when :sso_user_token
AuthenticationStrategies::SsoUserToken.new(strategy.user)
when :oauth_user_token
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/bearer_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/bearer_token.rb
new file mode 100644
index 00000000000..bd4bd3796a2
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/bearer_token.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal:true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages
+ module Peripherals
+ module StorageInteraction
+ module AuthenticationStrategies
+ class BearerToken
+ def self.strategy
+ Strategy.new(:bearer_token)
+ end
+
+ attr_reader :bearer_token
+
+ def initialize(bearer_token)
+ @bearer_token = bearer_token
+ end
+
+ # rubocop:disable Lint/UnusedMethodArgument
+ def call(storage:, http_options: {})
+ yield OpenProject.httpx.bearer_auth(bearer_token).with(http_options)
+ end
+ # rubocop:enable Lint/UnusedMethodArgument
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb
index ee354bdd5ab..685170586ad 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb
@@ -33,6 +33,10 @@ module Storages
module StorageInteraction
module AuthenticationStrategies
module NextcloudStrategies
+ BearerToken = -> do
+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::BearerToken.strategy
+ end
+
UserLess = -> do
::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::BasicAuth.strategy
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb
index cd751169908..22af767abe8 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb
@@ -33,6 +33,10 @@ module Storages
module StorageInteraction
module AuthenticationStrategies
module OneDriveStrategies
+ BearerToken = -> do
+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::BearerToken.strategy
+ end
+
UserLess = -> do
::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.strategy
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/strategy.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/strategy.rb
index 3e1aef96d39..fe8098ad1bf 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/strategy.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/strategy.rb
@@ -33,7 +33,7 @@ module Storages
module StorageInteraction
module AuthenticationStrategies
class Strategy
- attr_reader :key, :user, :use_cache
+ attr_reader :key, :user, :use_cache, :token
def initialize(key)
@key = key
@@ -51,6 +51,11 @@ module Storages
@use_cache = use_cache
self
end
+
+ def with_token(token)
+ @token = token
+ self
+ end
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 2888c25fd8e..b222e82145b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -71,7 +71,8 @@ module Storages
when :basic_auth
ServiceResult.success(result: storage.username)
when :oauth_user_token, :sso_user_token
- origin_user_id = RemoteIdentity.where(user_id: auth_strategy.user, oauth_client: storage.oauth_client)
+ origin_user_id = RemoteIdentity.where(user_id: auth_strategy.user, auth_source: storage.oauth_client,
+ integration: storage)
.pick(:origin_user_id)
if origin_user_id.present?
ServiceResult.success(result: origin_user_id)
diff --git a/modules/storages/app/components/storages/project_storages/members/row_component.rb b/modules/storages/app/components/storages/project_storages/members/row_component.rb
index 62578a35d21..6c9f84d45fb 100644
--- a/modules/storages/app/components/storages/project_storages/members/row_component.rb
+++ b/modules/storages/app/components/storages/project_storages/members/row_component.rb
@@ -100,7 +100,7 @@ module Storages::ProjectStorages::Members
def oauth_client_connected?
storage.oauth_client.present? &&
- member.principal.remote_identities.exists?(oauth_client: storage.oauth_client)
+ member.principal.remote_identities.exists?(auth_source: storage.oauth_client)
end
def can_read_files?
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 09b765d3a76..d9378233c09 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -40,6 +40,8 @@
# db/migrate/20220113144323_create_storage.rb "migration".
module Storages
class Storage < ApplicationRecord
+ using Peripherals::ServiceResultRefinements
+
PROVIDER_TYPES = [
PROVIDER_TYPE_NEXTCLOUD = "Storages::NextcloudStorage",
PROVIDER_TYPE_ONE_DRIVE = "Storages::OneDriveStorage"
@@ -61,6 +63,7 @@ module Storages
has_many :projects, through: :project_storages
has_one :oauth_client, as: :integration, dependent: :destroy
has_one :oauth_application, class_name: "::Doorkeeper::Application", as: :integration, dependent: :destroy
+ has_many :remote_identities, as: :integration, dependent: :destroy
validates_uniqueness_of :host, allow_nil: true
validates_uniqueness_of :name
@@ -220,5 +223,18 @@ module Storages
def health_reason_description
@health_reason_description ||= self.class.extract_part_from_piped_string(health_reason, 1)
end
+
+ def extract_origin_user_id(token)
+ auth_strategy = ::Storages::Peripherals::Registry
+ .resolve("#{self}.authentication.bearer_token")
+ .with_token(token.access_token)
+ ::Storages::Peripherals::Registry
+ .resolve("#{self}.queries.user")
+ .call(auth_strategy:, storage: self)
+ .match(
+ on_success: ->(user) { user[:id] },
+ on_failure: ->(error) { raise "UserQuery responed with #{error}" }
+ )
+ end
end
end
diff --git a/modules/storages/app/services/storages/nextcloud_managed_folder_sync_service.rb b/modules/storages/app/services/storages/nextcloud_managed_folder_sync_service.rb
index 78e27cc34ae..bad148abce9 100644
--- a/modules/storages/app/services/storages/nextcloud_managed_folder_sync_service.rb
+++ b/modules/storages/app/services/storages/nextcloud_managed_folder_sync_service.rb
@@ -318,7 +318,7 @@ module Storages
end
def remote_identities_scope
- RemoteIdentity.includes(:user).where(oauth_client: @storage.oauth_client)
+ RemoteIdentity.includes(:user).where(integration: @storage, auth_source: @storage.oauth_client)
end
def auth_strategy
@@ -326,7 +326,7 @@ module Storages
end
def admin_remote_identities_scope
- RemoteIdentity.includes(:user).where(oauth_client: @storage.oauth_client, user: User.admin.active)
+ RemoteIdentity.includes(:user).where(integration: @storage, auth_source: @storage.oauth_client, user: User.admin.active)
end
end
end
diff --git a/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb b/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb
index cecd733d55d..c18b78362fa 100644
--- a/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb
+++ b/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb
@@ -223,11 +223,11 @@ module Storages
end
def client_remote_identities_scope
- RemoteIdentity.includes(:user).where(oauth_client: @storage.oauth_client)
+ RemoteIdentity.includes(:user).where(integration: @storage, auth_source: @storage.oauth_client)
end
def admin_remote_identities_scope
- RemoteIdentity.includes(:user).where(oauth_client: @storage.oauth_client, user: User.admin.active)
+ RemoteIdentity.includes(:user).where(integration: @storage, auth_source: @storage.oauth_client, user: User.admin.active)
end
def root_folder = Peripherals::ParentFolder.new("/")
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 5cb7411a33b..6d5c4b1c619 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -135,6 +135,21 @@ module OpenProject::Storages
) do |payload|
::Storages::HealthService.new(storage: payload[:storage]).healthy
end
+
+ OpenProject::Notifications.subscribe(
+ ::OpenIDConnect::UserTokens::FetchService::TOKEN_OBTAINED
+ ) do |payload|
+ audience = payload[:audience]
+ token = payload[:token]
+ storage = Storages::Storage.all.find { |s| s.audience == audience }
+ if storage
+ RemoteIdentities::CreateService
+ .call(user: token.user, integration: storage, token:)
+ .on_failure { raise "RemoteIdentity creation failed" }
+ else
+ puts "WARNING: no integration was found for audience: #{audience}"
+ end
+ end
end
end
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
index a2e094e52f4..bc2b6196559 100644
--- a/modules/storages/spec/factories/storage_factory.rb
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -157,7 +157,8 @@ FactoryBot.define do
token_type: "bearer")
create(:remote_identity,
- oauth_client: storage.oauth_client,
+ auth_source: storage.oauth_client,
+ integration: storage,
user: evaluator.oauth_client_token_user,
origin_user_id: evaluator.origin_user_id)
end
@@ -221,8 +222,11 @@ FactoryBot.define do
refresh_token: ENV.fetch("ONE_DRIVE_TEST_OAUTH_CLIENT_REFRESH_TOKEN",
"MISSING_ONE_DRIVE_TEST_OAUTH_CLIENT_REFRESH_TOKEN"),
token_type: "bearer")
- create(:remote_identity, oauth_client: storage.oauth_client, user: evaluator.oauth_client_token_user,
- origin_user_id: "33db2c84-275d-46af-afb0-c26eb786b194")
+ create(:remote_identity,
+ auth_source: storage.oauth_client,
+ user: evaluator.oauth_client_token_user,
+ integration: storage,
+ origin_user_id: "33db2c84-275d-46af-afb0-c26eb786b194")
end
end
end
diff --git a/modules/storages/spec/features/create_file_links_spec.rb b/modules/storages/spec/features/create_file_links_spec.rb
index c16a9d41cff..27fcb01c9ff 100644
--- a/modules/storages/spec/features/create_file_links_spec.rb
+++ b/modules/storages/spec/features/create_file_links_spec.rb
@@ -41,7 +41,9 @@ RSpec.describe "Managing file links in work package", :js, :webmock do
let(:storage) { create(:nextcloud_storage, name: "My Storage", oauth_application:) }
let(:oauth_client) { create(:oauth_client, integration: storage) }
let(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user: current_user) }
- let(:remote_identity) { create(:remote_identity, oauth_client:, user: current_user, origin_user_id: "admin") }
+ let(:remote_identity) do
+ create(:remote_identity, auth_source: oauth_client, integration: storage, user: current_user, origin_user_id: "admin")
+ end
let(:project_storage) { create(:project_storage, project:, storage:, project_folder_id: nil, project_folder_mode: "inactive") }
let(:file_link) { create(:file_link, container: work_package, storage:, origin_id: "22", origin_name: "jingle.ogg") }
diff --git a/modules/storages/spec/features/manage_project_storage_spec.rb b/modules/storages/spec/features/manage_project_storage_spec.rb
index 296b76f3f28..a75bb9b8fd2 100644
--- a/modules/storages/spec/features/manage_project_storage_spec.rb
+++ b/modules/storages/spec/features/manage_project_storage_spec.rb
@@ -65,7 +65,9 @@ RSpec.describe("Activation of storages in projects",
let(:oauth_client) { create(:oauth_client, integration: storage) }
let(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user:) }
- let(:remote_identity) { create(:remote_identity, user:, oauth_client:, origin_user_id: "admin") }
+ let(:remote_identity) do
+ create(:remote_identity, user:, auth_source: oauth_client, integration: storage, origin_user_id: "admin")
+ end
let(:location_picker) { Components::FilePickerDialog.new }
diff --git a/modules/storages/spec/features/storages/admin/project_storages_spec.rb b/modules/storages/spec/features/storages/admin/project_storages_spec.rb
index 4c2258a1395..c728cb775d9 100644
--- a/modules/storages/spec/features/storages/admin/project_storages_spec.rb
+++ b/modules/storages/spec/features/storages/admin/project_storages_spec.rb
@@ -45,7 +45,7 @@ RSpec.describe "Admin lists project mappings for a storage",
shared_let(:oauth_client_token) { create(:oauth_client_token, oauth_client: storage.oauth_client, user: admin) }
shared_let(:remote_identity) do
- create(:remote_identity, oauth_client: storage.oauth_client, user: admin, origin_user_id: "admin")
+ create(:remote_identity, auth_source: storage.oauth_client, integration: storage, user: admin, origin_user_id: "admin")
end
shared_let(:archived_project_project_storage) do
diff --git a/modules/storages/spec/features/view_project_storage_members_spec.rb b/modules/storages/spec/features/view_project_storage_members_spec.rb
index fef182dbe67..de908b69b24 100644
--- a/modules/storages/spec/features/view_project_storage_members_spec.rb
+++ b/modules/storages/spec/features/view_project_storage_members_spec.rb
@@ -134,7 +134,8 @@ RSpec.describe "Project storage members connection status view", :js do
def create_remote_identities_for_users(oauth_client:, users:)
users.each do |user|
- create(:remote_identity, oauth_client:, user:, origin_user_id: "origin-user-id-#{user.id}")
+ create(:remote_identity, auth_source: oauth_client, integration: oauth_client.integration, user:,
+ origin_user_id: "origin-user-id-#{user.id}")
end
end
end
diff --git a/modules/storages/spec/services/storages/nextcloud_managed_folder_sync_service_spec.rb b/modules/storages/spec/services/storages/nextcloud_managed_folder_sync_service_spec.rb
index 37b15f03ee8..6ce8b3636ff 100644
--- a/modules/storages/spec/services/storages/nextcloud_managed_folder_sync_service_spec.rb
+++ b/modules/storages/spec/services/storages/nextcloud_managed_folder_sync_service_spec.rb
@@ -54,14 +54,20 @@ module Storages
shared_let(:storage) { create(:nextcloud_storage_with_complete_configuration, :as_automatically_managed) }
shared_let(:remote_identities) do
- [create(:remote_identity, user: admin, oauth_client: storage.oauth_client, origin_user_id: "admin"),
+ [create(:remote_identity,
+ user: admin,
+ auth_source: storage.oauth_client,
+ integration: storage,
+ origin_user_id: "admin"),
create(:remote_identity,
user: multiple_projects_user,
- oauth_client: storage.oauth_client,
+ auth_source: storage.oauth_client,
+ integration: storage,
origin_user_id: "multiple_projects_user"),
create(:remote_identity,
user: single_project_user,
- oauth_client: storage.oauth_client,
+ auth_source: storage.oauth_client,
+ integration: storage,
origin_user_id: "single_project_user")]
end
diff --git a/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb b/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb
index a67232a398a..87f73c0a544 100644
--- a/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb
+++ b/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb
@@ -48,7 +48,8 @@ RSpec.describe Storages::OneDriveManagedFolderSyncService, :webmock do
shared_let(:single_project_user_token) do
create(:remote_identity,
user: single_project_user,
- oauth_client: storage.oauth_client,
+ auth_source: storage.oauth_client,
+ integration: storage,
origin_user_id: "2ff33b8f-2843-40c1-9a17-d786bca17fba")
end
@@ -56,7 +57,8 @@ RSpec.describe Storages::OneDriveManagedFolderSyncService, :webmock do
shared_let(:multiple_project_user_token) do
create(:remote_identity,
user: multiple_projects_user,
- oauth_client: storage.oauth_client,
+ auth_source: storage.oauth_client,
+ integration: storage,
origin_user_id: "248aeb72-b231-4e71-a466-67fa7df2a285")
end
diff --git a/spec/factories/remote_identity_factory.rb b/spec/factories/remote_identity_factory.rb
index 9b4e3c23ae1..b6801601a70 100644
--- a/spec/factories/remote_identity_factory.rb
+++ b/spec/factories/remote_identity_factory.rb
@@ -30,8 +30,10 @@
FactoryBot.define do
factory :remote_identity do
- user
- oauth_client
+ user factory: %i[user]
+
+ auth_source factory: %i[oauth_client]
+ integration factory: %i[nextcloud_storage]
sequence(:origin_user_id) { |n| "remote-user-identifier-#{n}" }
end
diff --git a/spec/models/remote_identity_spec.rb b/spec/models/remote_identity_spec.rb
index 96192c1c18c..3e57caf0650 100644
--- a/spec/models/remote_identity_spec.rb
+++ b/spec/models/remote_identity_spec.rb
@@ -32,20 +32,21 @@ require "spec_helper"
RSpec.describe RemoteIdentity do
let(:user) { create(:user) }
- let(:oauth_client) { create(:oauth_client) }
+ let(:integration) { create(:nextcloud_storage) }
+ let(:auth_source) { create(:oauth_client) }
it "a user can have multiple identities in different oauth clients" do
other_client = create(:oauth_client)
- create(:remote_identity, user:, oauth_client:)
- second_identity = build(:remote_identity, user:, oauth_client: other_client)
+ create(:remote_identity, user:, auth_source:)
+ second_identity = build(:remote_identity, user:, auth_source: other_client)
expect(second_identity).to be_valid
end
- it "a user can only have one identity per oauth client" do
- create(:remote_identity, user:, oauth_client:)
+ it "a user can only have one identity per oauth client and integration" do
+ create(:remote_identity, user:, auth_source:, integration:)
- invalid = build(:remote_identity, user:, oauth_client:)
+ invalid = build(:remote_identity, user:, auth_source:, integration:)
expect(invalid).not_to be_valid
p invalid.errors
@@ -59,8 +60,8 @@ RSpec.describe RemoteIdentity do
end
it "is destroyed when the related oauth client is destroyed" do
- create(:remote_identity, oauth_client:)
+ create(:remote_identity, auth_source:)
- expect { oauth_client.destroy }.to change(described_class, :count).by(-1)
+ expect { auth_source.destroy }.to change(described_class, :count).by(-1)
end
end
diff --git a/spec/services/oauth_clients/connection_manager_one_drive_spec.rb b/spec/services/oauth_clients/connection_manager_one_drive_spec.rb
index b23e921e3be..4c843461c01 100644
--- a/spec/services/oauth_clients/connection_manager_one_drive_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_one_drive_spec.rb
@@ -63,7 +63,7 @@ RSpec.describe OAuthClients::ConnectionManager, :oauth_connection_helpers, :webm
it "fills in the origin_user_id" do
expect { subject.code_to_token(code) }.to change(OAuthClientToken, :count).by(1).and(change(RemoteIdentity, :count).by(1))
- last_token = RemoteIdentity.find_by!(user:, oauth_client: storage.oauth_client)
+ last_token = RemoteIdentity.find_by!(user:, auth_source: storage.oauth_client)
expect(last_token.origin_user_id).to eq("87d349ed-44d7-43e1-9a83-5f2406dee5bd")
end
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index cbdc5f1fe07..07bb1c571e3 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -124,7 +124,7 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
it "fills in the origin_user_id" do
expect { subject }.to change(OAuthClientToken, :count).by(1).and(change(RemoteIdentity, :count).by(1))
- last_token = RemoteIdentity.find_by!(user:, oauth_client:)
+ last_token = RemoteIdentity.find_by!(user:, auth_source: oauth_client)
expect(last_token.origin_user_id).to eq("admin")
end
diff --git a/spec/services/remote_identities/create_service_spec.rb b/spec/services/remote_identities/create_service_spec.rb
index e6f64065abb..03b99b0fe0b 100644
--- a/spec/services/remote_identities/create_service_spec.rb
+++ b/spec/services/remote_identities/create_service_spec.rb
@@ -6,27 +6,28 @@ require "services/base_services/behaves_like_create_service"
RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, :webmock, type: :model do
let(:user) { create(:user) }
- let(:storage) { create(:nextcloud_storage_configured) }
- let(:oauth_config) { storage.oauth_configuration }
+ let(:integration) { create(:nextcloud_storage_configured) }
+ let(:oauth_config) { integration.oauth_configuration }
+ let(:auth_source) { oauth_config.oauth_client }
let(:oauth_client_token) do
create(:oauth_client_token,
user:,
oauth_client: oauth_config.oauth_client)
end
- subject(:service) { described_class.new(user:, oauth_config:, oauth_client_token:) }
+ subject(:service) { described_class.new(user:, token: oauth_client_token, integration:) }
- before { stub_nextcloud_user_query(storage.host) }
+ before { stub_nextcloud_user_query(integration.host) }
describe ".call" do
it "requires a user, a oauth configuration and a rack token" do
method = described_class.method :call
- expect(method.parameters).to contain_exactly(%i[keyreq user], %i[keyreq oauth_config], %i[keyreq oauth_client_token])
+ expect(method.parameters).to contain_exactly(%i[keyreq user], %i[keyreq token], %i[keyreq integration])
end
it "succeeds", :webmock do
- expect(described_class.call(user:, oauth_config:, oauth_client_token:)).to be_success
+ expect(described_class.call(user:, token: oauth_client_token, integration:)).to be_success
end
end