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
Validate scope of JWTs
Jan Sandbrink52061873bce2
diff --git a/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb b/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rbindex 51cd9698e53..aa067d434bf 100644--- a/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb+++ b/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb@@ -25,6 +25,11 @@ module OpenProject::OpenIDConnect::JwtParser.new(required_claims: ["sub"]).parse(@access_token).either(->(payload_and_provider) dopayload, provider = payload_and_provider+ unless valid_scope?(payload)+ return fail_with_header! error: "insufficient_scope",+ error_description: "Requires scope #{scope} to access this resource."+ end+user = User.find_by(identity_url: "#{provider.slug}:#{payload['sub']}")authentication_result(user)end,@@ -51,6 +56,11 @@ module OpenProject)endend++ def valid_scope?(payload)+ scopes = (payload["scope"] || "").split+ scopes.include?(scope.to_s)+ endendendenddiff --git a/spec/requests/api/v3/authentication_spec.rb b/spec/requests/api/v3/authentication_spec.rbindex c985ca4cbba..606aeaa0e4f 100644--- a/spec/requests/api/v3/authentication_spec.rb+++ b/spec/requests/api/v3/authentication_spec.rb@@ -406,7 +406,7 @@ RSpec.describe "API V3 Authentication" do"manage-clients","query-groups"] },"account" => { "roles" => ["manage-account", "manage-account-links", "view-profile"] } },- "scope" => "email profile",+ "scope" => token_scope,"sid" => "eb235240-0b47-48fa-8b3e-f3b310d352e3","email_verified" => false,"preferred_username" => "admin"@@ -417,6 +417,7 @@ RSpec.describe "API V3 Authentication" dolet(:token_sub) { "b70e2fbf-ea68-420c-a7a5-0a287cb689c6" }let(:token_aud) { ["https://openproject.local", "master-realm", "account"] }let(:token_issuer) { "https://keycloak.local/realms/master" }+ let(:token_scope) { "email profile api_v3" }let(:expected_message) { "You did not provide the correct credentials." }let(:keys_request_stub) dostub_request(:get, "https://keycloak.local/realms/master/protocol/openid-connect/certs")@@ -479,6 +480,20 @@ RSpec.describe "API V3 Authentication" doendend+ context "when the scope does not permit access to APIv3" do+ let(:token_scope) { "profile email" }++ it "fails with HTTP 403 Forbidden" do+ get resource++ expect(last_response).to have_http_status :forbidden+ error = "Requires scope api_v3 to access this resource."+ expect(last_response.header["WWW-Authenticate"])+ .to eq(%{Bearer realm="OpenProject API", error="insufficient_scope", error_description="#{error}"})+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end+ end+context "when access token has expired already" dolet(:token_exp) { 5.minutes.ago }