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

Do it!

Jan Sandbrink77c203f9b75d
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 36891712bf7..b32c8b10aa6 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
@@ -32,6 +32,7 @@ module OpenIDConnect
module UserTokens
class ExchangeService
include Dry::Monads[:result]
+ include Dry::Monads::Do.for(:call)
class Disabled
class << self
@@ -50,19 +51,17 @@ module OpenIDConnect
def call(audience)
return Failure("Provider does not support token exchange") unless supported?
- FetchService.new(user: @user, token_exchange: Disabled)
- .access_token_for(audience: UserToken::IDP_AUDIENCE)
- .bind do |idp_token|
- exchange_token_request(idp_token, audience).bind do |json|
- access_token = json["access_token"]
- refresh_token = json["refresh_token"]
- break Failure("Token exchange response invalid") if access_token.blank?
+ idp_token = yield FetchService.new(user: @user, token_exchange: Disabled)
+ .access_token_for(audience: UserToken::IDP_AUDIENCE)
- token = store_exchanged_token(audience:, access_token:, refresh_token:)
+ json = yield exchange_token_request(idp_token, audience)
- Success(token)
- end
- end
+ access_token = json["access_token"]
+ refresh_token = json["refresh_token"]
+ return Failure("Token exchange response invalid") if access_token.blank?
+
+ token = store_exchanged_token(audience:, access_token:, refresh_token:)
+ Success(token)
end
def supported?
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 6ca2ff4a951..2baa1a62ea0 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
@@ -36,6 +36,7 @@ module OpenIDConnect
# client_id at an identity provider that OpenProject and the application have in common.
class FetchService
include Dry::Monads[:result]
+ include Dry::Monads::Do.for(:access_token_for, :refreshed_access_token_for)
def initialize(user:,
jwt_parser: JwtParser.new(verify_audience: false, verify_expiration: false),
@@ -59,16 +60,10 @@ module OpenIDConnect
# A token exchange is attempted, if the provider supports OAuth 2.0 Token Exchange and a token
# for the target audience either can't be found or it has expired, but has no available refresh token.
def access_token_for(audience:)
- token = token_with_audience(audience)
- token = token.bind do |t|
- if expired?(t.access_token)
- @token_refresh.call(t)
- else
- Success(t)
- end
- end
+ token = yield token_with_audience(audience)
+ token = yield @token_refresh.call(token) if expired?(token.access_token)
- token.fmap(&:access_token)
+ Success(token.access_token)
end
##
@@ -82,9 +77,9 @@ module OpenIDConnect
# A token exchange is attempted, if the provider supports OAuth 2.0 Token Exchange and a token
# for the target audience either can't be found or it has expired, but has no available refresh token.
def refreshed_access_token_for(audience:)
- token_with_audience(audience)
- .bind { |t| @token_refresh.call(t) }
- .fmap(&:access_token)
+ token = yield token_with_audience(audience)
+ token = yield @token_refresh.call(token)
+ Success(token.access_token)
end
private
diff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb
index f8658d89205..8d73536d6d6 100644
--- a/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb
+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb
@@ -32,6 +32,7 @@ module OpenIDConnect
module UserTokens
class RefreshService
include Dry::Monads[:result]
+ include Dry::Monads::Do.for(:call)
def initialize(user:, token_exchange:)
@user = user
@@ -43,15 +44,15 @@ module OpenIDConnect
return exchange_instead_of_refresh(token)
end
- refresh_token_request(token.refresh_token).bind do |json|
- access_token = json["access_token"]
- refresh_token = json["refresh_token"]
- break Failure("Refresh token response invalid") if access_token.blank?
+ json = yield refresh_token_request(token.refresh_token)
- token.update!(access_token:, refresh_token:)
+ access_token = json["access_token"]
+ refresh_token = json["refresh_token"]
+ return Failure("Refresh token response invalid") if access_token.blank?
- Success(token)
- end
+ token.update!(access_token:, refresh_token:)
+
+ Success(token)
end
private