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

Fail when user can't be found

Jan Sandbrink3cdab446a397
diff --git a/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb b/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb
index 573cddb809f..a3fadbb4fb7 100644
--- a/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb
+++ b/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module OpenProject
module Authentication
module Strategies
@@ -24,7 +26,11 @@ module OpenProject
->(payload_and_provider) do
payload, provider = payload_and_provider
user = User.find_by(identity_url: "#{provider.slug}:#{payload['sub']}")
- success!(user) if user
+ if user
+ success!(user)
+ else
+ fail_with_header!(error: "invalid_token", error_description: "The user identified by the token is not known")
+ end
end,
->(error) { fail_with_header!(error: "invalid_token", error_description: error) }
)
diff --git a/spec/requests/api/v3/authentication_spec.rb b/spec/requests/api/v3/authentication_spec.rb
index 02414c634bd..3be1938395a 100644
--- a/spec/requests/api/v3/authentication_spec.rb
+++ b/spec/requests/api/v3/authentication_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -421,10 +423,11 @@ RSpec.describe "API V3 Authentication" do
.to_return(status: 200, body: JWT::JWK::Set.new(jwk_response).export.to_json, headers: {})
end
let(:jwk_response) { jwk }
+ let(:user) { create(:user, identity_url: "keycloak:#{token_sub}") }
before do
create(:oidc_provider, slug: "keycloak")
- create(:user, identity_url: "keycloak:#{token_sub}")
+ user.save!
keys_request_stub
header "Authorization", "Bearer #{token}"
@@ -511,5 +514,18 @@ RSpec.describe "API V3 Authentication" do
.to eq(%{Bearer realm="OpenProject API", error="invalid_token", error_description="#{error}"})
end
end
+
+ context "when user identified by token is not known" do
+ let(:user) { create(:user, identity_url: "keycloak:not-the-token-sub") }
+
+ it "fails with HTTP 401 Unauthorized" do
+ get resource
+ expect(last_response).to have_http_status :unauthorized
+ expect(JSON.parse(last_response.body)).to eq(error_response_body)
+ error = "The user identified by the token is not known"
+ expect(last_response.header["WWW-Authenticate"])
+ .to eq(%{Bearer realm="OpenProject API", error="invalid_token", error_description="#{error}"})
+ end
+ end
end
end