Skip to content

Commit 6710373

Browse files
Make warden available across entire application
This will be required to use warden for our SCIM endpoints as well, which are implemented in rails controllers. Since Rails controllers do not support mounting rack middlewares partially (the way that e.g. Grape does), the mounting of warden needed to be moved. This was not super straight-forward, because of load order issues: * Requiring a Rails middleware must be done before initialization finished * Our warden config was so far done _after_ initialization * static_routes were defined in lib, which is automatically reloaded, but auto-reloading code is not allowed during initialization * lib_static which is autoloaded_once is fine during init, this is also where the rest of warden authentication is defined Additionally warden was configured to not handle HTTP 401 responses generated by the upstream app itself. Warden will only be responsible for its own authentication failures and it's still possible to invoke the warden failure app by throwing the :warden symbol, but the application keeps its capability of responding with custom 401 responses.
1 parent d870d1e commit 6710373

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

config/initializers/warden.rb

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
Rails.application.config.after_initialize do
2-
namespace = OpenProject::Authentication::Strategies::Warden
1+
# frozen_string_literal: true
32

4-
strategies = [
5-
[:basic_auth_failure, namespace::BasicAuthFailure, "Basic"],
6-
[:global_basic_auth, namespace::GlobalBasicAuth, "Basic"],
7-
[:user_basic_auth, namespace::UserBasicAuth, "Basic"],
8-
[:oauth, namespace::DoorkeeperOAuth, "Bearer"],
9-
[:anonymous_fallback, namespace::AnonymousFallback, "Basic"],
10-
[:jwt_oidc, namespace::JwtOidc, "Bearer"],
11-
[:session, namespace::Session, "Session"]
12-
]
3+
namespace = OpenProject::Authentication::Strategies::Warden
134

14-
strategies.each do |name, clazz, auth_scheme|
15-
OpenProject::Authentication.add_strategy(name, clazz, auth_scheme)
16-
end
5+
strategies = [
6+
[:basic_auth_failure, namespace::BasicAuthFailure, "Basic"],
7+
[:global_basic_auth, namespace::GlobalBasicAuth, "Basic"],
8+
[:user_basic_auth, namespace::UserBasicAuth, "Basic"],
9+
[:oauth, namespace::DoorkeeperOAuth, "Bearer"],
10+
[:anonymous_fallback, namespace::AnonymousFallback, "Basic"],
11+
[:jwt_oidc, namespace::JwtOidc, "Bearer"],
12+
[:session, namespace::Session, "Session"]
13+
]
1714

18-
OpenProject::Authentication.update_strategies(OpenProject::Authentication::Scope::API_V3, { store: false }) do |_|
19-
%i[global_basic_auth
20-
user_basic_auth
21-
basic_auth_failure
22-
oauth
23-
jwt_oidc
24-
session
25-
anonymous_fallback]
26-
end
15+
strategies.each do |name, clazz, auth_scheme|
16+
OpenProject::Authentication.add_strategy(name, clazz, auth_scheme)
17+
end
18+
19+
OpenProject::Authentication.update_strategies(OpenProject::Authentication::Scope::API_V3, { store: false }) do |_|
20+
%i[global_basic_auth
21+
user_basic_auth
22+
basic_auth_failure
23+
oauth
24+
jwt_oidc
25+
session
26+
anonymous_fallback]
27+
end
28+
29+
Rails.application.configure do |app|
30+
app.config.middleware.use OpenProject::Authentication::Manager, intercept_401: false
2731
end

lib/api/root_api.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ class RootAPI < Grape::API
4444

4545
content_type :json, "application/json; charset=utf-8"
4646

47-
use OpenProject::Authentication::Manager
48-
4947
helpers API::Caching::Helpers
5048
module Helpers
5149
include ::API::Helpers::RaiseQueryErrors

modules/bim/lib/open_project/bim/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class Engine < ::Rails::Engine
213213
Mime::Type.register "application/octet-stream", :bcfzip unless Mime::Type.lookup_by_extension(:bcfzip)
214214
end
215215

216-
config.to_prepare do
216+
config.before_initialize do
217217
Doorkeeper.configuration.scopes.add(:bcf_v2_1)
218218

219219
unless defined? OpenProject::Authentication::Scope::BCF_V2_1

0 commit comments

Comments
 (0)