diff --git a/config/locales/en.yml b/config/locales/en.yml index 254e3f1072..73bcbd49db 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2688,6 +2688,5 @@ en: By default, OpenProject provides OAuth 2.0 authorization via %{authorization_code_flow_link}. You can optionally enable %{client_credentials_flow_link}, but you must provide a user on whose behalf requests will be performed. authorization_error: "An authorization error has occurred." - authorization_error_client_credentials: "Client credentials flow is not allowed for this application." revoke_my_application_confirmation: "Do you really want to remove this application? This will revoke %{token_count} active for it." my_registered_applications: "Registered OAuth applications" diff --git a/lib/open_project/authentication/strategies/warden/doorkeeper_oauth.rb b/lib/open_project/authentication/strategies/warden/doorkeeper_oauth.rb index 1df218af42..23b2bb4cbd 100644 --- a/lib/open_project/authentication/strategies/warden/doorkeeper_oauth.rb +++ b/lib/open_project/authentication/strategies/warden/doorkeeper_oauth.rb @@ -31,7 +31,7 @@ module OpenProject if client_credential_user = find_credential_app_user(token.application_id) authenticate_user client_credential_user else - fail!(I18n.t('oauth.authorization_error_client_credentials')) + success! User.anonymous end end diff --git a/spec/requests/oauth/client_credentials_flow_spec.rb b/spec/requests/oauth/client_credentials_flow_spec.rb new file mode 100644 index 0000000000..61f3ee980c --- /dev/null +++ b/spec/requests/oauth/client_credentials_flow_spec.rb @@ -0,0 +1,80 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2018 the OpenProject Foundation (OPF) +# +# 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-2017 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 docs/COPYRIGHT.rdoc for more details. +#++ + +require 'spec_helper' +require 'rest-client' + +describe 'OAuth client credentials flow', type: :request do + include Rack::Test::Methods + + let!(:application) { FactoryBot.create(:oauth_application, client_credentials_user_id: user_id, name: 'Cool API app!') } + let(:client_secret) { application.plaintext_secret } + + let(:access_token) { + response = post '/oauth/token', + grant_type: 'client_credentials', + scope: 'api_v3', + client_id: application.uid, + client_secret: client_secret + + expect(response).to be_successful + body = JSON.parse(response.body) + body['access_token'] + } + + subject do + # Perform request with it + headers = { 'HTTP_CONTENT_TYPE' => 'application/json', 'HTTP_AUTHORIZATION' => "Bearer #{access_token}" } + response = get '/api/v3', {}, headers + expect(response).to be_successful + + JSON.parse(response.body) + end + + before do + expect(access_token).to be_present + expect(subject).to be_present + end + + describe 'when application provides client credentials impersonator' do + let(:user) { FactoryBot.create(:user) } + let(:user_id) { user.id } + + it 'allows client credential flow as the user' do + expect(subject.dig('_links', 'user', 'href')).to eq("/api/v3/users/#{user.id}") + end + end + + describe 'when application does not provide client credential impersonator' do + let(:user_id) { nil } + + it 'allows client credential flow as the anonymous user' do + expect(subject.dig('_links', 'user', 'href')).to be_nil + end + end +end