Skip to content

Commit a1144f7

Browse files
committed
Authentication Validator
1 parent ed87269 commit a1144f7

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module Storages
32+
module Peripherals
33+
module ConnectionValidators
34+
module OneDrive
35+
class AuthenticationValidator < BaseValidatorGroup
36+
def initialize(storage)
37+
super
38+
@user = User.current
39+
end
40+
41+
private
42+
43+
def validate
44+
register_checks(:existing_token, :user_bound_request)
45+
46+
oauth_token
47+
user_bound_request
48+
end
49+
50+
def oauth_token
51+
if OAuthClientToken.where(user: @user, oauth_client: @storage.oauth_client).any?
52+
pass_check(:existing_token)
53+
else
54+
warn_check(:existing_token, message(:oauth_token_missing), halt_validation: true)
55+
end
56+
end
57+
58+
def user_bound_request
59+
Registry["one_drive.queries.user"].call(storage: @storage, auth_strategy:).on_failure do
60+
fail_check(:user_bound_request, message("oauth_request_#{it.result}"))
61+
end
62+
63+
pass_check(:user_bound_request)
64+
end
65+
66+
def auth_strategy = Registry["one_drive.authentication.user_bound"].call(storage: @storage, user: @user)
67+
end
68+
end
69+
end
70+
end
71+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
require "spec_helper"
32+
require_module_spec_helper
33+
34+
module Storages
35+
module Peripherals
36+
module ConnectionValidators
37+
module OneDrive
38+
RSpec.describe AuthenticationValidator, :webmock do
39+
subject(:validator) { described_class.new(storage) }
40+
41+
context "when using OAuth2" do
42+
let(:user) { create(:user) }
43+
let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }
44+
45+
before { User.current = user }
46+
47+
it "passes when the user has a token and the request works", vcr: "one_drive/user_query_success" do
48+
expect(validator.call).to be_success
49+
end
50+
51+
it "returns a warning when there's no token for the current user" do
52+
User.current = create(:user)
53+
result = validator.call
54+
55+
expect(result[:existing_token]).to be_a_warning
56+
expect(result[:existing_token].message).to eq(I18n.t(i18n_key(:oauth_token_missing)))
57+
expect(result[:user_bound_request]).to be_skipped
58+
end
59+
60+
it "returns a failure if the remote call failed" do
61+
Registry.stub("one_drive.queries.user", ->(_) { ServiceResult.failure(result: :unauthorized) })
62+
63+
result = validator.call
64+
expect(result[:user_bound_request]).to be_a_failure
65+
expect(result[:user_bound_request].message).to eq(I18n.t(i18n_key(:oauth_request_unauthorized)))
66+
end
67+
end
68+
69+
private
70+
71+
def i18n_key(key) = "storages.health.connection_validation.#{key}"
72+
end
73+
end
74+
end
75+
end
76+
end

0 commit comments

Comments
 (0)