|
| 1 | +# OpenID Connect Authentication for Redmine |
| 2 | +# Copyright (C) 2020-2021 Contargo GmbH & Co. KG |
| 3 | +# |
| 4 | +# This program is free software; you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation; either version 2 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License along |
| 15 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + |
| 18 | +require 'repost' |
| 19 | + |
| 20 | +module RedmineOidc |
| 21 | + module SudoModeControllerPatch |
| 22 | + def require_sudo_mode(*param_names) |
| 23 | + return super unless RedmineOidc.settings.enabled |
| 24 | + return super unless RedmineOidc.settings.sudo_mode_reauthenticate |
| 25 | + |
| 26 | + return true if Redmine::SudoMode.active? |
| 27 | + |
| 28 | + if OidcSession.spawn(session).auth_time > Redmine::SudoMode.timeout.ago.to_i |
| 29 | + logger.info "Activating sudo mode for user #{User.current.login}" |
| 30 | + Redmine::SudoMode.active! |
| 31 | + return true |
| 32 | + end |
| 33 | + |
| 34 | + if param_names.blank? |
| 35 | + # This list was copied from the original `require_sudo_mode`, but without `_method`. |
| 36 | + param_names = params.keys - %w(id action controller authenticity_token utf8) |
| 37 | + end |
| 38 | + |
| 39 | + back_url = url_for(**params.slice(:controller, :action, :id, :project_id).to_unsafe_hash) |
| 40 | + |
| 41 | + session[:oidc_sudo_deferred] = { |
| 42 | + back_url: back_url, |
| 43 | + params: params.slice(*param_names).to_unsafe_hash, |
| 44 | + options: { |
| 45 | + method: request.method_symbol, |
| 46 | + authenticity_token: :auto, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + logger.info "Reauthenticating #{User.current.login} for sudo mode" |
| 51 | + redirect_to oidc_login_path(back_url: back_url, reauth: true) |
| 52 | + |
| 53 | + false |
| 54 | + end |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +unless Redmine::SudoMode::Controller.included_modules.include?(RedmineOidc::SudoModeControllerPatch) |
| 59 | + Redmine::SudoMode::Controller.prepend(RedmineOidc::SudoModeControllerPatch) |
| 60 | +end |
0 commit comments