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

Fix omission in token exchange implementation

Jan Sandbrink06a0917c216a
diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rb
index d74b6c00410..0ab643a9c91 100644
--- a/modules/openid_connect/app/models/openid_connect/provider.rb
+++ b/modules/openid_connect/app/models/openid_connect/provider.rb
@@ -34,8 +34,6 @@ module OpenIDConnect
has_many :remote_identities, as: :auth_source, dependent: :destroy
- TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange"
-
OIDC_PROVIDERS = %w[google microsoft_entra custom].freeze
DISCOVERABLE_STRING_ATTRIBUTES_MANDATORY = %i[authorization_endpoint
userinfo_endpoint
@@ -118,7 +116,7 @@ module OpenIDConnect
def token_exchange_capable?
return false if grant_types_supported.blank?
- grant_types_supported.include?(TOKEN_EXCHANGE_GRANT_TYPE)
+ grant_types_supported.include?(OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE)
end
def icon
diff --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.rb
index 36be64a145b..ed679d05ba6 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
@@ -45,8 +45,9 @@ module OpenIDConnect
def exchange(access_token, audience)
request_token(form: {
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE,
+ grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE,
subject_token: access_token,
+ subject_token_type: OpenProject::OpenIDConnect::ACCESS_TOKEN_TYPE,
audience:
})
end
diff --git a/modules/openid_connect/lib/open_project/openid_connect.rb b/modules/openid_connect/lib/open_project/openid_connect.rb
index 4516474df1f..e64deb8e14e 100644
--- a/modules/openid_connect/lib/open_project/openid_connect.rb
+++ b/modules/openid_connect/lib/open_project/openid_connect.rb
@@ -34,6 +34,9 @@ require "open_project/openid_connect/engine"
module OpenProject
module OpenIDConnect
+ ACCESS_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token"
+ TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange"
+
def self.configuration
providers = ::OpenIDConnect::Provider.where(available: true)
diff --git a/modules/openid_connect/spec/factories/oidc_provider_factory.rb b/modules/openid_connect/spec/factories/oidc_provider_factory.rb
index 3f385241eb0..906fa0d7a35 100644
--- a/modules/openid_connect/spec/factories/oidc_provider_factory.rb
+++ b/modules/openid_connect/spec/factories/oidc_provider_factory.rb
@@ -52,7 +52,7 @@ FactoryBot.define do
trait :token_exchange_capable do
callback(:after_build) do |provider|
provider.options["grant_types_supported"] ||= []
- provider.options["grant_types_supported"] << OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE
+ provider.options["grant_types_supported"] << OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE
end
end
end
diff --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.rb
index bd52fb83499..e4596ae34e6 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
@@ -57,7 +57,7 @@ RSpec.describe OpenIDConnect::UserTokens::ExchangeService, :webmock do
user.oidc_user_tokens.create!(access_token: idp_access_token, audiences: [OpenIDConnect::UserToken::IDP_AUDIENCE])
user.oidc_user_tokens.create!(access_token:, refresh_token:, audiences: [existing_audience])
stub_request(:post, provider.token_endpoint)
- .with(body: hash_including(grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE))
+ .with(body: hash_including(grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE))
.to_return(**exchange_response)
end
diff --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.rb
index 9d1c6d70f44..48559d695a7 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
@@ -121,7 +121,12 @@ RSpec.describe OpenIDConnect::UserTokens::TokenRequest, :webmock do
it "uses a properly formatted request body" do
subject
expect(WebMock).to have_requested(:post, provider.token_endpoint)
- .with(body: { grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE, subject_token: token, audience: })
+ .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:
+ })
end
it "authenticates the request via HTTP Basic auth using Client ID and Client Secret" do
diff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb
index b89415600cf..1caef612c63 100644
--- a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb
@@ -178,12 +178,13 @@ module Storages
context "when the server supports token exchange" do
let(:oidc_provider) { create(:oidc_provider, :token_exchange_capable, scope: "offline_access") }
- let(:exchangeable_token) { create(:oidc_user_token, user:, refresh_token: nil) }
+ let!(:exchangeable_token) { create(:oidc_user_token, user:, refresh_token: nil) }
it "favors token exchange when refreshing" do
exchange_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })
+ .with(body: hash_including(
+ grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE
+ ))
.and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })
expect(validator.call).to be_success
@@ -192,8 +193,9 @@ module Storages
it "fails if the exchange is met with an unexpected body" do
exchange_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })
+ .with(body: hash_including(
+ grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE
+ ))
.and_return_json(status: 200, body: { error: "failed " })
result = validator.call
@@ -205,8 +207,9 @@ module Storages
it "fails if the exchange fails" do
exchange_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })
+ .with(body: hash_including(
+ grant_type: OpenProject::OpenIDConnect::TOKEN_EXCHANGE_GRANT_TYPE
+ ))
.and_return(status: 401)
result = validator.call