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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow to pass a scope during token exchange
Jan Sandbrink79a6b56027d0
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.rbindex d741955d97f..5d4977250af 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@@ -46,8 +46,9 @@ module OpenIDConnectattr_reader :user- def initialize(user:)+ def initialize(user:, scope: nil)@user = user+ @scope = scopeenddef call(audience)@@ -85,7 +86,7 @@ module OpenIDConnectenddef exchange_token_request(idp_token, audience)- TokenRequest.new(provider:).exchange(idp_token, audience).alt_map do+ TokenRequest.new(provider:).exchange(idp_token, audience, @scope).alt_map doit.with(code: :"token_exchange_#{it.code}", source: self.class)endenddiff --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.rbindex f731c3a7ab7..eff345978e8 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@@ -43,8 +43,9 @@ module OpenIDConnectattr_reader :userdef initialize(user:,+ exchange_scope: nil,jwt_parser: JwtParser.new(verify_audience: false, verify_expiration: false),- token_exchange: ExchangeService.new(user:),+ token_exchange: ExchangeService.new(user:, scope: exchange_scope),token_refresh: RefreshService.new(user:, token_exchange:))@user = user@token_exchange = token_exchangediff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rbindex ed679d05ba6..f774185c546 100644--- a/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rb+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rb@@ -43,13 +43,16 @@ module OpenIDConnectrequest_token(form: { grant_type: :refresh_token, refresh_token: })end- def exchange(access_token, audience)- request_token(form: {- grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE,- subject_token: access_token,- subject_token_type: OpenProject::OpenIDConnect::ACCESS_TOKEN_TYPE,- audience:- })+ def exchange(access_token, audience, scope)+ parameters = {+ grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE,+ subject_token: access_token,+ subject_token_type: OpenProject::OpenIDConnect::ACCESS_TOKEN_TYPE,+ audience:+ }+ parameters[:scope] = scope unless scope.nil?++ request_token(form: parameters)endprivatediff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rbindex e4596ae34e6..6dea15ccb5c 100644--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rb+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rb@@ -85,6 +85,22 @@ RSpec.describe OpenIDConnect::UserTokens::ExchangeService, :webmock do.with(body: hash_including(subject_token: idp_access_token))end+ it "doesn't request any scopes" do+ subject+ expect(WebMock).to(have_requested(:post, provider.token_endpoint).with { |req| expect(req.body).not_to include("scope") })+ end++ context "when configuring a scope" do+ let(:service) { described_class.new(user:, scope:) }+ let(:scope) { "scope-a scope-b" }++ it "requests the scopes during token exchange" do+ subject+ expect(WebMock).to have_requested(:post, provider.token_endpoint)+ .with(body: hash_including(scope:))+ end+ end+context "when the response has no expires_in" dolet(:exchange_response) do{diff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rbindex a940b65d6de..8ab4aeaa361 100644--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb@@ -164,4 +164,48 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock doendendend++ describe "instantiation" do+ let(:service) { described_class.new(user:) }+ let(:user) { create(:user) }++ let(:jwt_parser) { instance_double(OpenIDConnect::JwtParser) }+ let(:exchange_service) { instance_double(OpenIDConnect::UserTokens::ExchangeService) }+ let(:refresh_service) { instance_double(OpenIDConnect::UserTokens::RefreshService) }++ before do+ allow(OpenIDConnect::JwtParser).to receive(:new).and_return(jwt_parser)+ allow(OpenIDConnect::UserTokens::ExchangeService).to receive(:new).and_return(exchange_service)+ allow(OpenIDConnect::UserTokens::RefreshService).to receive(:new).and_return(refresh_service)+ end++ it "uses a non-verifying JWT parser" do+ service+ expect(OpenIDConnect::JwtParser).to have_received(:new).with(verify_audience: false, verify_expiration: false)+ end++ it "configures an exchange service" do+ service+ expect(OpenIDConnect::UserTokens::ExchangeService).to have_received(:new).with(user:, scope: nil)+ end++ it "configures a refresh service" do+ service+ expect(OpenIDConnect::UserTokens::RefreshService).to have_received(:new).with(user:, token_exchange: exchange_service)+ end++ context "when specifying exchange_scope" do+ let(:service) { described_class.new(user:, exchange_scope: "scope-a scope-b") }++ it "configures the exchange service with the correct scope" do+ service+ expect(OpenIDConnect::UserTokens::ExchangeService).to have_received(:new).with(user:, scope: "scope-a scope-b")+ end++ it "configures a refresh service" do+ service+ expect(OpenIDConnect::UserTokens::RefreshService).to have_received(:new).with(user:, token_exchange: exchange_service)+ end+ end+ endenddiff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rbindex 48559d695a7..705cb7e6386 100644--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rb+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rb@@ -107,10 +107,11 @@ RSpec.describe OpenIDConnect::UserTokens::TokenRequest, :webmock doenddescribe "#exchange" do- subject { service.exchange(token, audience) }+ subject { service.exchange(token, audience, scope) }let(:token) { "a-refresh-token" }let(:audience) { "target-audience" }+ let(:scope) { nil }it { is_expected.to be_success }@@ -153,6 +154,22 @@ RSpec.describe OpenIDConnect::UserTokens::TokenRequest, :webmock doendend+ context "when passing a scope" do+ let(:scope) { "scope-a scope-b" }++ it "includes the scope in the request body" do+ subject+ expect(WebMock).to have_requested(:post, provider.token_endpoint)+ .with(body: {+ grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",+ subject_token: token,+ subject_token_type: "urn:ietf:params:oauth:token-type:access_token",+ audience:,+ scope:+ })+ end+ end+context "when the request fails" dolet(:response) do{diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rbindex a78ff16f6b6..9e760c7303e 100644--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb@@ -104,7 +104,7 @@ module Storagesenddef token_negotiable- service = OpenIDConnect::UserTokens::FetchService.new(user: @user)+ service = OpenIDConnect::UserTokens::FetchService.new(user: @user, exchange_scope: @storage.storage_scope)result = service.access_token_for(audience: @storage.audience)return pass_check(:token_negotiable) if result.success?diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rbindex 3e02634232c..88137f28e8e 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb@@ -43,7 +43,7 @@ module Storagesdef call(storage:, http_options: {}, &)OpenIDConnect::UserTokens::FetchService- .new(user: @user)+ .new(user: @user, exchange_scope: storage.storage_scope).access_token_for(audience: storage.audience).either(->(token) dodiff --git a/modules/storages/app/controllers/storages/admin/storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages_controller.rbindex dfc3f4a5733..17d61b14b4e 100644--- a/modules/storages/app/controllers/storages/admin/storages_controller.rb+++ b/modules/storages/app/controllers/storages/admin/storages_controller.rb@@ -249,6 +249,7 @@ class Storages::Admin::StoragesController < ApplicationController"authentication_method","audience_configuration","storage_audience",+ "storage_scope","oauth_client_id","oauth_client_secret","tenant_id",diff --git a/modules/storages/app/forms/storages/admin/storage_audience_input_form.rb b/modules/storages/app/forms/storages/admin/storage_audience_input_form.rbindex 73e9555e71b..220c9d61296 100644--- a/modules/storages/app/forms/storages/admin/storage_audience_input_form.rb+++ b/modules/storages/app/forms/storages/admin/storage_audience_input_form.rb@@ -60,6 +60,14 @@ module Storages::Admindata: { "storages--storage-audience-target": "audienceInput" },value: prefilled_audience)++ toggleable_group.text_field(+ name: :storage_scope,+ label: I18n.t("activerecord.attributes.storages/nextcloud_storage.storage_scope"),+ required: false,+ caption: I18n.t("storages.instructions.nextcloud.storage_scope"),+ input_width: :large+ )endenddiff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rbindex 5ac5be153d1..3e05cf609c5 100644--- a/modules/storages/app/models/storages/nextcloud_storage.rb+++ b/modules/storages/app/models/storages/nextcloud_storage.rb@@ -49,6 +49,7 @@ module Storagesstore_attribute :provider_fields, :group_folder, :stringstore_attribute :provider_fields, :authentication_method, :string, default: "two_way_oauth2"store_attribute :provider_fields, :storage_audience, :string+ store_attribute :provider_fields, :storage_scope, :stringdef oauth_configurationPeripherals::OAuthConfigurations::NextcloudConfiguration.new(self)diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex f10647158d7..6acfd1fdf2c 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -16,6 +16,7 @@ en:oauth2_sso_with_two_way_oauth2_fallback: Single-Sign-On with Two-Way OAuth 2.0 as fallbacktwo_way_oauth2: Two-way OAuth 2.0 authorization code flowstorage_audience: Storage Audience+ storage_scope: Storage Scopestorages/project_storage:project_folder: Project folderproject_folder_mode: Project folder mode@@ -353,6 +354,7 @@ en:provider_configuration: Please make sure you have administration privileges in your Nextcloud instance and the %{application_link_text} is installed before doing the setup.storage_audience: Please enter the Client ID that the Nextcloud instance uses to communicate with the identity provider.storage_audience_placeholder: e.g. nextcloud+ storage_scope: Enter scopes that should be requested during token exchange. Multiple scopes can be separated by a space.no_specific_folder: By default, each user will start at their own home folder when they upload a file.no_storage_set_up: There are no file storages set up yet.not_logged_into_storage: To select a project folder, please first login