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
Refactor DoorkeeperOAuth strategy
Jan Sandbrink1fafb59c1844
diff --git a/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rb b/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rbindex 7c79e3d9b8e..60adb896618 100644--- a/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rb+++ b/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rb@@ -28,26 +28,21 @@ module OpenProjectdef authenticate!access_token = ::Doorkeeper::OAuth::Token.authenticate(decorated_request,*Doorkeeper.configuration.access_token_methods)- return fail_with_header!(error: "invalid_token") if access_token.blank?- return fail_with_header!(error: "invalid_token") if access_token.expired? || access_token.revoked?- return fail_with_header!(error: "invalid_token") unless access_token.application.enabled?+ return fail_with_header!(error: "invalid_token") if access_token_invalid?(access_token)return fail_with_header!(error: "insufficient_scope") if !access_token.includes_scope?(scope)- if access_token.resource_owner_id.nil?- user_id = ::Doorkeeper::Application- .where(id: access_token.application_id)- .where.not(client_credentials_user_id: nil)- .pick(:client_credentials_user_id)- authenticate_user(user_id) if user_id- else- authenticate_user(access_token.resource_owner_id)- end+ user_id = access_token.resource_owner_id || access_token.application.client_credentials_user_id+ authenticate_user(user_id) if user_idendprivate+ def access_token_invalid?(access_token)+ access_token.blank? || access_token.expired? || access_token.revoked? || !access_token.application.enabled?+ end+def authenticate_user(id)- user = User.find_by(id:)+ user = id && User.find_by(id:)if usersuccess!(user)elsediff --git a/spec/factories/oauth_access_token_factory.rb b/spec/factories/oauth_access_token_factory.rbindex 72c564d0ced..7556c4260a2 100644--- a/spec/factories/oauth_access_token_factory.rb+++ b/spec/factories/oauth_access_token_factory.rb@@ -35,7 +35,7 @@ FactoryBot.define doendafter(:build) do |token, evaluator|- token.resource_owner_id = evaluator.resource_owner.id+ token.resource_owner_id = evaluator.resource_owner.id if evaluator.resource_ownerendapplication factory: :oauth_applicationdiff --git a/spec/requests/api/v3/authentication_spec.rb b/spec/requests/api/v3/authentication_spec.rbindex 54c28cf2b26..7d38c757421 100644--- a/spec/requests/api/v3/authentication_spec.rb+++ b/spec/requests/api/v3/authentication_spec.rb@@ -122,8 +122,9 @@ RSpec.describe "API V3 Authentication" doendend- context "with not found user" do- let(:token) { create(:oauth_access_token, resource_owner: user) }+ context "when the token's resource owner can't be found" do+ let(:token) { create(:oauth_access_token, resource_owner: user, application:) }+ let(:application) { create(:oauth_application) }let(:oauth_access_token) { token.plaintext_token }around do |ex|@@ -136,6 +137,43 @@ RSpec.describe "API V3 Authentication" doexpect(last_response.header["WWW-Authenticate"]).to eq('Bearer realm="OpenProject API", error="invalid_token"')expect(JSON.parse(last_response.body)).to eq(error_response_body)end++ context "when the application has a client credentials user configured" do+ let(:application) { create(:oauth_application, client_credentials_user_id: create(:user).id) }++ it "returns unauthorized" do+ expect(last_response).to have_http_status :unauthorized+ expect(last_response.header["WWW-Authenticate"]).to eq('Bearer realm="OpenProject API", error="invalid_token"')+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end+ end+ end++ context "when there is no resource owner on the token" do+ let(:token) { create(:oauth_access_token, resource_owner: nil, application:) }+ let(:application) { create(:oauth_application) }+ let(:oauth_access_token) { token.plaintext_token }++ # Note: This is just caused by DoorkeeperOauth rejecting to handle this case and auth falling through to basic auth+ # more specific examples can be found at spec/requests/oauth/client_credentials_flow_spec.rb+ let(:expected_message) { "You need to be authenticated to access this resource." }++ it "returns unauthorized" do+ expect(last_response).to have_http_status :unauthorized++ # Note: This is just caused by DoorkeeperOauth rejecting to handle this case and auth falling through to basic auth+ # more specific examples can be found at spec/requests/oauth/client_credentials_flow_spec.rb+ expect(last_response.header["WWW-Authenticate"]).to eq('Basic realm="OpenProject API"')+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end++ context "when the application has a client credentials user configured" do+ let(:application) { create(:oauth_application, client_credentials_user_id: user.id) }++ it "authenticates successfully" do+ expect(last_response).to have_http_status :ok+ end+ endendend