-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14954 from opf/implementation/53368-add-authentic…
…ation-code-and-tests [#53368] add authentication for storage interaction
- Loading branch information
Showing
25 changed files
with
1,989 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
version: '3.8' | ||
|
||
networks: | ||
network: | ||
testing: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal:true | ||
|
||
#-- 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. | ||
#++ | ||
|
||
module Storages | ||
module Peripherals | ||
module StorageInteraction | ||
class Authentication | ||
using ::Storages::Peripherals::ServiceResultRefinements | ||
|
||
def self.[](strategy) | ||
case strategy.key | ||
when :basic_auth | ||
AuthenticationStrategies::BasicAuth.new | ||
when :oauth_user_token | ||
AuthenticationStrategies::OAuthUserToken.new(strategy.user) | ||
when :oauth_client_credentials | ||
AuthenticationStrategies::OAuthClientCredentials.new | ||
else | ||
raise "Invalid authentication strategy '#{strategy}'" | ||
end | ||
end | ||
|
||
# Checks for the current authorization state of a user on a specific file storage. | ||
# Returns one of three results: | ||
# - :connected If a valid user token is available | ||
# - :failed_authorization If a user token is available, which is invalid and not refreshable | ||
# - :error If an unexpected error occurred | ||
def self.authorization_state(storage:, user:) | ||
auth_strategy = AuthenticationStrategies::OAuthUserToken.strategy.with_user(user) | ||
|
||
::Storages::Peripherals::Registry | ||
.resolve("#{storage.short_provider_type}.queries.auth_check") | ||
.call(storage:, auth_strategy:) | ||
.match( | ||
on_success: ->(result) { result }, | ||
on_failure: ->(error) { error.code == :unauthorized ? :failed_authorization : :error } | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
60 changes: 60 additions & 0 deletions
60
...p/common/storages/peripherals/storage_interaction/authentication_strategies/basic_auth.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal:true | ||
|
||
#-- 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. | ||
#++ | ||
|
||
module Storages | ||
module Peripherals | ||
module StorageInteraction | ||
module AuthenticationStrategies | ||
class BasicAuth | ||
def self.strategy | ||
Strategy.new(:basic_auth) | ||
end | ||
|
||
def call(storage:, http_options: {}) | ||
username = storage.username | ||
password = storage.password | ||
|
||
return build_failure(storage) if username.blank? || password.blank? | ||
|
||
yield OpenProject.httpx.basic_auth(username, password).with(http_options) | ||
end | ||
|
||
private | ||
|
||
def build_failure(storage) | ||
log_message = "Cannot authenticate storage with basic auth. Password or username not configured." | ||
data = ::Storages::StorageErrorData.new(source: self.class, payload: storage) | ||
Failures::Builder.call(code: :error, log_message:, data:) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
44 changes: 44 additions & 0 deletions
44
...app/common/storages/peripherals/storage_interaction/authentication_strategies/failures.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal:true | ||
|
||
#-- 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. | ||
#++ | ||
|
||
module Storages | ||
module Peripherals | ||
module StorageInteraction | ||
module AuthenticationStrategies | ||
module Failures | ||
Builder = ->(code:, log_message:, data:) do | ||
storage_error = ::Storages::StorageError.new(code:, log_message:, data:) | ||
ServiceResult.failure(result: code, errors: storage_error) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.