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
[#55643] Extend API authentication to accept JWT issued by OpenID provider to other client.
Pavel Balashouf611ba3bcb6e
diff --git a/config/initializers/jwt.rb b/config/initializers/jwt.rbnew file mode 100644index 00000000000..4442e70229d--- /dev/null+++ b/config/initializers/jwt.rb@@ -0,0 +1,29 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++JWT.configuration.strict_base64_decoding = truediff --git a/config/initializers/warden.rb b/config/initializers/warden.rbindex c94f72f62d5..b2a43c58277 100644--- a/config/initializers/warden.rb+++ b/config/initializers/warden.rb@@ -5,16 +5,23 @@ Rails.application.config.after_initialize do[:basic_auth_failure, namespace::BasicAuthFailure, "Basic"],[:global_basic_auth, namespace::GlobalBasicAuth, "Basic"],[:user_basic_auth, namespace::UserBasicAuth, "Basic"],- [:oauth, namespace::DoorkeeperOAuth, "OAuth"],+ [:oauth, namespace::DoorkeeperOAuth, "Bearer"],[:anonymous_fallback, namespace::AnonymousFallback, "Basic"],+ [:jwt_oidc, namespace::JwtOidc, "Bearer"],[:session, namespace::Session, "Session"]]strategies.each do |name, clazz, auth_scheme|- OpenProject::Authentication.add_strategy name, clazz, auth_scheme+ OpenProject::Authentication.add_strategy(name, clazz, auth_scheme)endOpenProject::Authentication.update_strategies(OpenProject::Authentication::Scope::API_V3, { store: false }) do |_|- %i[global_basic_auth user_basic_auth basic_auth_failure oauth session anonymous_fallback]+ %i[global_basic_auth+ user_basic_auth+ basic_auth_failure+ oauth+ jwt_oidc+ session+ anonymous_fallback]endenddiff --git a/docker/dev/keycloak/docker-compose.yml b/docker/dev/keycloak/docker-compose.ymlindex 77b0aae0b21..aac66478bc4 100644--- a/docker/dev/keycloak/docker-compose.yml+++ b/docker/dev/keycloak/docker-compose.yml@@ -20,14 +20,16 @@ services:extra_hosts:- "openproject.local:host-gateway"environment:- - KC_DB_URL_HOST=db+ - KC_DB=postgres- KC_DB_USERNAME=keycloak- KC_DB_PASSWORD=keycloak- - KC_DB_URL_DATABASE=jdbc:postgresql://db:5432/keycloak+ - KC_DB_URL=jdbc:postgresql://db-keycloak:5432/keycloak- KEYCLOAK_ADMIN=admin- KEYCLOAK_ADMIN_PASSWORD=admin- KC_DB_SCHEMA=public- KC_HOSTNAME=keycloak.local+ - KC_FEATURES=token-exchange+ - KC_TRANSACTION_XA_ENABLED=falsevolumes:- /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt:ro- keycloak-data:/opt/keycloak/data/diff --git a/lib/api/root.rb b/lib/api/root.rbindex b13d1697767..a218fcf5ec7 100644--- a/lib/api/root.rb+++ b/lib/api/root.rb@@ -39,13 +39,14 @@ module APIerror_representer ::API::V3::Errors::ErrorRepresenter, "application/hal+json; charset=utf-8"authentication_scope OpenProject::Authentication::Scope::API_V3- OpenProject::Authentication.handle_failure(scope: API_V3) do |warden, _opts|- e = grape_error_for warden.env, self+ OpenProject::Authentication.handle_failure(scope: API_V3) do |warden, opts|+ e = grape_error_for(warden.env, self)error_message = I18n.t("api_v3.errors.code_401_wrong_credentials")- api_error = ::API::Errors::Unauthenticated.new error_message- representer = ::API::V3::Errors::ErrorRepresenter.new api_error+ api_error = ::API::Errors::Unauthenticated.new(error_message)+ representer = ::API::V3::Errors::ErrorRepresenter.new(api_error)+ status = opts[:message] == "insufficient_scope" ? 403 : 401- e.error! representer.to_json, 401, warden.headers+ e.error!(representer.to_json, status, warden.headers)endversion "v3", using: :path dodiff --git a/lib_static/open_project/authentication.rb b/lib_static/open_project/authentication.rbindex 65f2fbb40e0..3dc9d2e19d3 100644--- a/lib_static/open_project/authentication.rb+++ b/lib_static/open_project/authentication.rb@@ -261,18 +261,25 @@ module OpenProjectdef response_header(default_auth_scheme: self.default_auth_scheme,scope: nil,- request_headers: {}+ request_headers: {},+ error: nil,+ error_description: nil)- scheme = pick_auth_scheme auth_schemes(scope), default_auth_scheme, request_headers-- "#{scheme} realm=\"#{scope_realm(scope)}\""+ scheme = pick_auth_scheme(auth_schemes(scope),+ default_auth_scheme,+ request_headers)++ header = %{#{scheme} realm="#{scope_realm(scope)}"}+ header << %{ error="#{error}"} if error+ header << %{ error_description="#{error_description}"} if error && error_description+ headerenddef auth_schemes(scope)strategies = Array(Manager.scope_config(scope).strategies)Manager.auth_schemes- .select { |_, info| scope.nil? or not (info.strategies & strategies).empty? }+ .select { |_, info| scope.nil? or not !info.strategies.intersect?(strategies) }.keysendenddiff --git a/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rb b/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rbindex 7e7fcbf06d6..11cd7c23ecc 100644--- a/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rb+++ b/lib_static/open_project/authentication/strategies/warden/doorkeeper_oauth.rb@@ -4,65 +4,52 @@ module OpenProjectmodule Authenticationmodule Strategiesmodule Warden- ##- # Allows testing authentication via doorkeeper OAuth2 token- #class DoorkeeperOAuth < ::Warden::Strategies::Base+ include FailWithHeader+def valid?- ::Doorkeeper::OAuth::Token- .from_request(decorated_request, *Doorkeeper.configuration.access_token_methods)- .present?+ access_token = ::Doorkeeper::OAuth::Token+ .from_request(decorated_request, *Doorkeeper.configuration.access_token_methods)+ access_token.present? &&+ begin+ JWT.decode(access_token, nil, false)+ false+ rescue JWT::DecodeError+ true+ endenddef authenticate!- token = ::Doorkeeper::OAuth::Token.authenticate(decorated_request,- *Doorkeeper.configuration.access_token_methods)- return fail!("invalid token") unless token&.acceptable?(scope)-- if token.resource_owner_id.nil?- authenticate_client_credentials(token)+ 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: "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_idelse- authenticate_user(token.resource_owner_id)+ authenticate_user(access_token.resource_owner_id)endendprivate- ##- # We allow applications to designate a user to be used for client credentials.- # When using client credentials flow, find this user and try to authenticate- def authenticate_client_credentials(token)- if client_credential_user = find_credential_app_user(token.application_id)- authenticate_user client_credential_user- else- success! User.anonymous- end- end-- ##- # Find a credentials-enabled application with the given ID- # and return its allowed application user, if there is one.- # Avoid going through token.application.client_credentials_user_id for performance- # (this is going to be called on every request with CC flows!)- def find_credential_app_user(app_id)- ::Doorkeeper::Application- .where(id: app_id)- .where.not(client_credentials_user_id: nil)- .pluck(:client_credentials_user_id)- .first- end-def authenticate_user(id)user = User.find_by(id:)if usersuccess!(user)else- fail!("No such user")+ fail_with_header!(error: "invalid_token")endenddef decorated_request- ::Doorkeeper::Grape::AuthorizationDecorator.new(request)+ @decorated_request ||= ::Doorkeeper::Grape::AuthorizationDecorator.new(request)endendenddiff --git a/lib_static/open_project/authentication/strategies/warden/fail_with_header.rb b/lib_static/open_project/authentication/strategies/warden/fail_with_header.rbnew file mode 100644index 00000000000..8f8427ada1c--- /dev/null+++ b/lib_static/open_project/authentication/strategies/warden/fail_with_header.rb@@ -0,0 +1,20 @@+module OpenProject+ module Authentication+ module Strategies+ module Warden+ module FailWithHeader+ def fail_with_header!(error:, error_description: nil)+ headers(+ "WWW-Authenticate" => OpenProject::Authentication::WWWAuthenticate.response_header(+ default_auth_scheme: "Bearer",+ error:,+ error_description:+ )+ )+ fail!(error)+ end+ end+ end+ end+ end+enddiff --git a/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb b/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rbnew file mode 100644index 00000000000..19a5a1a7595--- /dev/null+++ b/lib_static/open_project/authentication/strategies/warden/jwt_oidc.rb@@ -0,0 +1,82 @@+module OpenProject+ module Authentication+ module Strategies+ module Warden+ class JwtOidc < ::Warden::Strategies::Base+ include FailWithHeader++ SUPPORTED_ALG = %w[+ RS256+ RS384+ RS512+ ].freeze++ def valid?+ @access_token = ::Doorkeeper::OAuth::Token.from_bearer_authorization(+ ::Doorkeeper::Grape::AuthorizationDecorator.new(request)+ )+ @access_token.present? &&+ begin+ @unverified_payload, @unverified_header = JWT.decode(@access_token, nil, false)+ @unverified_header.present? && @unverified_payload.present?+ rescue JWT::DecodeError+ false+ end+ end++ def authenticate!+ issuer = @unverified_payload["iss"]+ provider = OpenProject::OpenIDConnect.providers.find { |p| p.configuration[:issuer] == issuer } if issuer.present?+ if provider.present?+ client_id = provider.configuration.fetch(:identifier)+ alg = @unverified_header.fetch("alg")+ key =+ if SUPPORTED_ALG.include?(alg)+ kid = @unverified_header.fetch("kid")+ jwks_uri = provider.configuration[:jwks_uri]+ JSON::JWK::Set::Fetcher.fetch(jwks_uri, kid:).to_key+ else+ fail_with_header!(error: "invalid_token", error_description: "Token signature algorithm is not supported")+ return+ end+ begin+ verified_payload, = JWT.decode(+ @access_token,+ key,+ true,+ {+ algorithm: alg,+ verify_iss: true,+ verify_aud: true,+ iss: issuer,+ aud: client_id,+ required_claims: ["sub", "iss", "aud"]+ }+ )+ rescue JWT::ExpiredSignature+ fail_with_header!(error: "invalid_token", error_description: "The access token expired")+ return+ rescue JWT::ImmatureSignature+ # happens when nbf time is less than current+ fail_with_header!(error: "invalid_token", error_description: "The access token is used too early")++ return+ rescue JWT::InvalidIssuerError+ fail_with_header!(error: "invalid_token", error_description: "The access token issuer is wrong")+ return+ rescue JWT::InvalidAudError+ fail_with_header!(error: "invalid_token", error_description: "The access token audience claim is wrong")+ return+ end++ user = User.find_by(identity_url: "#{provider.name}:#{verified_payload['sub']}")+ success!(user) if user+ else+ fail_with_header!(error: "invalid_token", error_description: "The access token issuer is unknown")+ end+ end+ end+ end+ end+ end+enddiff --git a/modules/openid_connect/lib/open_project/openid_connect/engine.rb b/modules/openid_connect/lib/open_project/openid_connect/engine.rbindex 4591f5ed970..68bdfe4174b 100644--- a/modules/openid_connect/lib/open_project/openid_connect/engine.rb+++ b/modules/openid_connect/lib/open_project/openid_connect/engine.rb@@ -29,9 +29,15 @@ module OpenProject::OpenIDConnectregister_auth_providers doOmniAuth::OpenIDConnect::Providers.configure custom_options: %i[- display_name? icon? sso? issuer?- check_session_iframe? end_session_endpoint?- limit_self_registration? use_graph_api?+ display_name?+ icon?+ sso?+ issuer?+ check_session_iframe?+ end_session_endpoint?+ jwks_uri?+ limit_self_registration?+ use_graph_api?]strategy :openid_connect dodiff --git a/spec/requests/api/v3/authentication_spec.rb b/spec/requests/api/v3/authentication_spec.rbindex ca642ee1d4c..6702e9253c8 100644--- a/spec/requests/api/v3/authentication_spec.rb+++ b/spec/requests/api/v3/authentication_spec.rb@@ -28,15 +28,23 @@require "spec_helper"-RSpec.describe API::V3 do+RSpec.describe "API V3 Authentication" dolet(:resource) { "/api/v3/projects" }let(:user) { create(:user) }+ let(:error_response_body) do+ {+ "_type" => "Error",+ "errorIdentifier" => "urn:openproject-org:api:v3:errors:Unauthenticated",+ "message" => expected_message+ }+ enddescribe "oauth" dolet(:oauth_access_token) { "" }+ let(:expected_message) { "You did not provide the correct credentials." }before do- login_as user+ userheader "Authorization", "Bearer #{oauth_access_token}"@@ -57,28 +65,68 @@ RSpec.describe API::V3 doit "returns unauthorized" doexpect(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)endend- context "with an expired access token" do+ context "with a revoked access token" dolet(:token) { create(:oauth_access_token, resource_owner: user, revoked_at: DateTime.now) }let(:oauth_access_token) { token.plaintext_token }it "returns unauthorized" doexpect(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)endend- end- describe "basic auth" do- let(:response_401) do- {- "_type" => "Error",- "errorIdentifier" => "urn:openproject-org:api:v3:errors:Unauthenticated",- "message" => expected_message- }+ context "with an expired access token" do+ let(:token) { create(:oauth_access_token, resource_owner: user) }+ let(:oauth_access_token) { token.plaintext_token }++ around do |ex|+ Timecop.freeze(Time.current + (token.expires_in + 5).seconds) do+ ex.run+ end+ end++ 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++ context "with wrong scope" do+ let(:token) { create(:oauth_access_token, resource_owner: user, scopes: "unknown_scope") }+ let(:oauth_access_token) { token.plaintext_token }++ it "returns forbidden" do+ expect(last_response).to have_http_status :forbidden+ expect(last_response.header["WWW-Authenticate"]).to eq('Bearer realm="OpenProject API" error="insufficient_scope"')+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ endend+ context "with not found user" do+ let(:token) { create(:oauth_access_token, resource_owner: user) }+ let(:oauth_access_token) { token.plaintext_token }++ around do |ex|+ user.destroy+ ex.run+ end++ 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++ describe "basic auth" dolet(:expected_message) { "You need to be authenticated to access this resource." }strategies = OpenProject::Authentication::Strategies::Warden@@ -113,12 +161,11 @@ RSpec.describe API::V3 doendit "returns the correct JSON response" do- expect(JSON.parse(last_response.body)).to eq response_401+ expect(JSON.parse(last_response.body)).to eq error_response_bodyendit "returns the WWW-Authenticate header" do- expect(last_response.header["WWW-Authenticate"])- .to include 'Basic realm="OpenProject API"'+ expect(last_response.header["WWW-Authenticate"]).to include 'Basic realm="OpenProject API"'endend@@ -135,7 +182,7 @@ RSpec.describe API::V3 doendit "returns the correct JSON response" do- expect(JSON.parse(last_response.body)).to eq response_401+ expect(JSON.parse(last_response.body)).to eq error_response_bodyendit "returns the correct content type header" do@@ -160,7 +207,7 @@ RSpec.describe API::V3 doendit "returns the correct JSON response" do- expect(JSON.parse(last_response.body)).to eq response_401+ expect(JSON.parse(last_response.body)).to eq error_response_bodyendit "returns the correct content type header" do@@ -187,7 +234,7 @@ RSpec.describe API::V3 doendit "returns the correct JSON response" do- expect(JSON.parse(last_response.body)).to eq response_401+ expect(JSON.parse(last_response.body)).to eq error_response_bodyendit "returns the correct content type header" do@@ -215,8 +262,7 @@ RSpec.describe API::V3 docontext "with login required" dobefore do- allow(Setting).to receive(:login_required).and_return(true)- allow(Setting).to receive(:login_required?).and_return(true)+ allow(Setting).to receive_messages(login_required: true, login_required?: true)endcontext "with global basic auth configured" do@@ -254,8 +300,7 @@ RSpec.describe API::V3 docontext "when enabled", with_config: { apiv3_enable_basic_auth: true } docontext "without login required" dobefore do- allow(Setting).to receive(:login_required).and_return(false)- allow(Setting).to receive(:login_required?).and_return(false)+ allow(Setting).to receive_messages(login_required: false, login_required?: false)endcontext "with global and user basic auth enabled" do@@ -320,4 +365,120 @@ RSpec.describe API::V3 doendendend++ describe(+ "OIDC",+ :webmock,+ with_settings: {+ plugin_openproject_openid_connect: {+ "providers" => {+ "keycloak" => {+ "display_name" => "Keycloak",+ "identifier" => "https://openproject.local",+ "secret" => "9AWjVC3A4U1HLrZuSP4xiwHfw6zmgECn",+ "host" => "keycloak.local",+ "issuer" => "https://keycloak.local/realms/master",+ "authorization_endpoint" => "/realms/master/protocol/openid-connect/auth",+ "token_endpoint" => "/realms/master/protocol/openid-connect/token",+ "userinfo_endpoint" => "/realms/master/protocol/openid-connect/userinfo",+ "end_session_endpoint" => "https://keycloak.local/realms/master/protocol/openid-connect/logout",+ "jwks_uri" => "https://keycloak.local/realms/master/protocol/openid-connect/certs"+ }+ }+ }+ }+ ) do+ let(:rsa_signed_access_token_without_aud) do+ "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI5N0FteXZvUzhCRkZSZm01ODVHUGdBMTZHMUgyVjIyRWR4eHVBWVV1b0trIn0.eyJleHAiOjE3MjEyODM0MzAsImlhdCI6MTcyMTI4MzM3MCwianRpIjoiYzUyNmI0MzUtOTkxZi00NzRhLWFkMWItYzM3MTQ1NmQxZmQwIiwiaXNzIjoiaHR0cHM6Ly9rZXljbG9hay5sb2NhbC9yZWFsbXMvbWFzdGVyIiwiYXVkIjpbIm1hc3Rlci1yZWFsbSIsImFjY291bnQiXSwic3ViIjoiYjcwZTJmYmYtZWE2OC00MjBjLWE3YTUtMGEyODdjYjY4OWM2IiwidHlwIjoiQmVhcmVyIiwiYXpwIjoiaHR0cHM6Ly9vcGVucHJvamVjdC5sb2NhbCIsInNlc3Npb25fc3RhdGUiOiJlYjIzNTI0MC0wYjQ3LTQ4ZmEtOGIzZS1mM2IzMTBkMzUyZTMiLCJhY3IiOiIxIiwiYWxsb3dlZC1vcmlnaW5zIjpbIm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]) }+ let(:token_sub) { JWT.decode(token, nil, false)[0]["sub"] }+ let(:expected_message) { "You did not provide the correct credentials." }++ before do+ create(:user, identity_url: "keycloak:#{token_sub}")+ stub_request(:get, "https://keycloak.local/realms/master/protocol/openid-connect/certs")+ .with(+ headers: {+ "Accept" => "*/*",+ "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",+ "User-Agent" => "JSON::JWK::Set::Fetcher 2.7.2"+ }+ )+ .to_return(status: 200, body: '{"keys":[{"kid":"CANAG6lJUPKqKDoWxxXL5wAHf2U18BAzm_LJm7RPTGk","kty":"RSA","alg":"RSA-OAEP","use":"enc","n":"nqJexS6n-SxKSDUxXp_dsNwDW6cZ4Rtgqq9ut_lp1CNSph5wTnLG3aQQsTEvx5o3-SZ-pHjJ0gtEpg7clAz-w-YQyZoAXkFtQqmZJxsmdS4K0yILxO3WUNdJQlutjmq-Ri50Senn5IV7yEYWLo8St1qzUqWZhp0HKudyty24triC9UJTK03W3_Tr5c1X8vKL8duAjvLB7p_sYUOrnLq5pD5lqwxVSAiN8qS5zVNZMrhGV5aN1vN_vue_tw8c2SVOCLLTrUh3441rYaeo-UwQZF7ZTm30xflqAIfe8qMoB20wtWYAXR0D5iqkkdEH4XanCYVm5vdUFIPPvXZhRDWoNQ","e":"AQAB","x5c":["MIICmzCCAYMCBgGQupeGPzANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZtYXN0ZXIwHhcNMjQwNzE2MDgwODMwWhcNMzQwNzE2MDgxMDEwWjARMQ8wDQYDVQQDDAZtYXN0ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCeol7FLqf5LEpINTFen92w3ANbpxnhG2Cqr263+WnUI1KmHnBOcsbdpBCxMS/Hmjf5Jn6keMnSC0SmDtyUDP7D5hDJmgBeQW1CqZknGyZ1LgrTIgvE7dZQ10lCW62Oar5GLnRJ6efkhXvIRhYujxK3WrNSpZmGnQcq53K3Lbi2uIL1QlMrT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FTANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZtYXN0ZXIwHhcNMjQwNzE2MDgwODMwWhcNMzQwNzE2MDgxMDEwWjARMQ8wDQYDVQQDDAZtYXN0ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCMwHavsEbhAnMucDb9+AbWbGWHZFf8xLHSVzaWs8hUUZgG4C7yKvBEtJ7FflpB0P8C2fEsPiD2OIbpUQPCZaikR7R+v5AyXQiIRLm0m5AyuOFqM6TOvzFNi7rOX6ScvS2732JFuYo1jMRy/RMCBj/3mfiAPm0+Fi9jiBe4esUqpiQlA+xKChOGUfuEC1/OwO3rKnc/aVBQRIaDenNz8n80qdCSSCBw0HToeXPgOZwfGFZcrnU/VX1wTuqHg+uGwn5I4fmCtw30+05mLp/ZWVZ0V3KKxP5h6E2SV9WoG10Iubs5yCGGXQdKAu5X710cJ3AEtSkAGx2x3QvctEJSBF+NAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAIoBCsOO0bXiVspoXkqdOts4+3sULbbp5aEwQscmLX017Zvv5jxdkZxUYk8L08lNB+WlC1ES4VlmtE06D0cWYErGpArJzVBKgYSA3CkA9veBEugHviMqfwg3suNc8S+GtaRBvpbVZtXydjjqA8GZ4eKhPoJLHHCX6X2Ad33Cdt0/ftucjTqAKVzzzgWZejy+ZKP6ybAqYJ+EZoPUXlyWT3uwcpGEJ3nzOYYGTfxOSmAwnH2v5Z/JWr9ex5o/+QBuBhFcg0z8NcHa3Z0E6ZC9GGxV7XztBqYicO+nONHTLCctoJmyXvLM4j8qIG2UQgPIiwIL0Jkz6xQAYyXvsb+LhM8="],"x5t":"BFrni6MoX-CJwtMT4vzij1HBSTI","x5t#S256":"-Ge3y4JRezxhGTDfbkNoz7prkokzYtbKQ9ardPtfcz4"}]}', headers: {})++ header "Authorization", "Bearer #{token}"+ end++ context "when token is issued by provider not configured in OP" do+ let(:token) do+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJpc3N1ZXIuY29tIn0.C9gEPaqdNSEZ4dZHz0z51VCylScIEqRLnwMCkNXuz6g"+ end++ it do+ get resource+ expect(last_response).to have_http_status :unauthorized+ expect(last_response.header["WWW-Authenticate"]).to eq("Bearer realm=\"OpenProject API\" error=\"invalid_token\" error_description=\"The access token issuer is unknown\"")+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end+ end++ context "when token is issued by provider configured in OP" do+ context "when token signature algorithm is not supported" do+ let(:token) do+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJodHRwczovL2tleWNsb2FrLmxvY2FsL3JlYWxtcy9tYXN0ZXIifQ.Pwod8ZJqq3jWsbnrGw4ZU1-aLS2bSicb8PgiF78JHUc"+ end++ it do+ get resource+ expect(last_response).to have_http_status :unauthorized+ expect(last_response.header["WWW-Authenticate"]).to eq("Bearer realm=\"OpenProject API\" error=\"invalid_token\" error_description=\"Token signature algorithm is not supported\"")+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end+ end++ context "when access token has not expired yet" do+ context "when aud does not contain client_id" do+ let(:token) { rsa_signed_access_token_without_aud }++ it do+ Timecop.freeze(token_exp - 20.seconds) do+ get resource+ end+ expect(last_response).to have_http_status :unauthorized+ expect(last_response.header["WWW-Authenticate"]).to eq('Bearer realm="OpenProject API" error="invalid_token" error_description="The access token audience claim is wrong"')+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end+ end++ context "when aud contains client_id" do+ let(:token) { rsa_signed_access_token_with_aud }++ it do+ Timecop.freeze(token_exp - 20.seconds) do+ get resource+ end+ expect(last_response).to have_http_status :ok+ end+ end+ end++ context "when access token has expired already" do+ let(:token) { rsa_signed_access_token_without_aud }++ it do+ Timecop.freeze(token_exp + 20.seconds) do+ get resource+ end++ expect(last_response).to have_http_status :unauthorized+ expect(last_response.header["WWW-Authenticate"]).to eq('Bearer realm="OpenProject API" error="invalid_token" error_description="The access token expired"')+ expect(JSON.parse(last_response.body)).to eq(error_response_body)+ end+ end+ end+ endenddiff --git a/spec/support/wait_for.rb b/spec/support/wait_for.rbindex 6a2a93f3f8a..8fcde59104f 100644--- a/spec/support/wait_for.rb+++ b/spec/support/wait_for.rb@@ -25,8 +25,7 @@ module RSpec## Examples:# wait_for(ticker.tape).to eq("··-·")- # wait_for(ticker.tape).to eq("··-· ---")- # wait_for(ticker.tape).to eq("··-· --- ---", timeout: 5)+ # wait_for { ticker.tape }.to eq("··-· ---")def wait_for(value = Target::UndefinedValue, &block)Target.for(value, block)end@@ -36,6 +35,10 @@ module RSpec# @param timeout [Numeric] time in seconds to wait up for assertions to pass# @param delay [Numeric] time in seconds elapsing between two checks of an# assertion+ # Examples:+ # with_wait(timeout: 10, delay: 1) do+ # wait_for { ticker.tape }.to eq("··-· ---")+ # enddef with_wait(timeout: nil, delay: nil)original_timeout = RSpec.configuration.wait_timeoutoriginal_delay = RSpec.configuration.wait_delay