Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add logout URL for header based SSO

Oliver Güntherbb8b6efef618
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index d2e08cdf11c..782b4b44d74 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -60,13 +60,12 @@ class AccountController < ApplicationController
# Log out current user and redirect to welcome page
def logout
+ # Keep attributes from the session
+ # to identify the user
+ previous_session = session.to_h.with_indifferent_access
logout_user
- if Setting.login_required? && omniauth_direct_login?
- flash.now[:notice] = I18n.t :notice_logged_out
- render :exit, locals: { instructions: :after_logout }
- else
- redirect_to home_url
- end
+
+ perform_post_logout previous_session
end
# Enable user to choose a new password
@@ -361,14 +360,6 @@ class AccountController < ApplicationController
end
end
- def logout_user
- if User.current.logged?
- cookies.delete OpenProject::Configuration['autologin_cookie_name']
- Token::AutoLogin.where(user_id: current_user.id).delete_all
- self.logged_user = nil
- end
- end
-
def authenticate_user
if OpenProject::Configuration.disable_password_login?
render_404
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index ab3b3b617c4..a6422e7187c 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -43,6 +43,7 @@ class ApplicationController < ActionController::Base
include Redmine::I18n
include HookHelper
include ErrorsHelper
+ include Accounts::CurrentUser
include ::OpenProject::Authentication::SessionExpiry
include AdditionalUrlHelpers
include OpenProjectErrorHelper
@@ -167,67 +168,6 @@ class ApplicationController < ActionController::Base
OpenProject::Configuration.reload_mailer_configuration!
end
- # The current user is a per-session kind of thing and session stuff is controller responsibility.
- # A globally accessible User.current is a big code smell. When used incorrectly it allows getting
- # the current user outside of a session scope, i.e. in the model layer, from mailers or
- # in the console which doesn't make any sense. For model code that needs to be aware of the
- # current user, i.e. when returning all visible projects for <somebody>, the controller should
- # pass the current user to the model, instead of letting it fetch it by itself through
- # `User.current`. This method acts as a reminder and wants to encourage you to use it.
- # Project.visible_by actually allows the controller to pass in a user but it falls back
- # to `User.current` and there are other places in the session-unaware codebase,
- # that rely on `User.current`.
- def current_user
- User.current
- end
- helper_method :current_user
-
- def user_setup
- # Find the current user
- User.current = find_current_user
- end
-
- # Returns the current user or nil if no user is logged in
- # and starts a session if needed
- def find_current_user
- if session[:user_id]
- # existing session
- User.active.find_by(id: session[:user_id])
- elsif cookies[OpenProject::Configuration['autologin_cookie_name']] && Setting.autologin?
- # auto-login feature starts a new session
- user = User.try_to_autologin(cookies[OpenProject::Configuration['autologin_cookie_name']])
- session[:user_id] = user.id if user
- user
- elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action])
- # RSS key authentication does not start a session
- User.find_by_rss_key(params[:key])
- elsif Setting.rest_api_enabled? && api_request?
- if (key = api_key_from_request) && accept_key_auth_actions.include?(params[:action])
- # Use API key
- User.find_by_api_key(key)
- end
- end
- end
-
- # Sets the logged in user
- def logged_user=(user)
- reset_session
-
- if user&.is_a?(User)
- User.current = user
- Sessions::InitializeSessionService.call(user, session)
- else
- User.current = User.anonymous
- end
- end
-
- # check if login is globally required to access the application
- def check_if_login_required
- # no check needed if user is already logged in
- return true if User.current.logged?
- require_login if Setting.login_required?
- end
-
# Checks if the session cookie is missing.
# This is useful only on a second request
def openproject_cookie_missing?
@@ -270,39 +210,6 @@ class ApplicationController < ActionController::Base
SetLocalizationService.new(User.current, request.env['HTTP_ACCEPT_LANGUAGE']).call
end
- def require_login
- unless User.current.logged?
-
- respond_to do |format|
- format.any(:html, :atom) do
- # Ensure we reset the session to terminate any old session objects
- # but ONLY for html requests to avoid double-resetting sessions
- reset_session
-
- redirect_to main_app.signin_path(back_url: login_back_url)
- end
-
- auth_header = OpenProject::Authentication::WWWAuthenticate.response_header(request_headers: request.headers)
-
- format.any(:xml, :js, :json) do
- head :unauthorized,
- 'X-Reason' => 'login needed',
- 'WWW-Authenticate' => auth_header
- end
-
- format.all { head :not_acceptable }
- end
- return false
- end
- true
- end
-
- def require_admin
- return unless require_login
-
- render_403 unless User.current.admin?
- end
-
def deny_access(not_found: false)
if User.current.logged?
not_found ? render_404 : render_403
diff --git a/app/controllers/concerns/accounts/current_user.rb b/app/controllers/concerns/accounts/current_user.rb
new file mode 100644
index 00000000000..350d1c7fff1
--- /dev/null
+++ b/app/controllers/concerns/accounts/current_user.rb
@@ -0,0 +1,154 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-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.
+#++
+
+##
+# Intended to be used by the ApplicationController to provide login/logout helpers
+module Accounts::CurrentUser
+ extend ActiveSupport::Concern
+
+ included do
+ helper_method :current_user
+ end
+
+ protected
+
+ # The current user is a per-session kind of thing and session stuff is controller responsibility.
+ # A globally accessible User.current is a big code smell. When used incorrectly it allows getting
+ # the current user outside of a session scope, i.e. in the model layer, from mailers or
+ # in the console which doesn't make any sense. For model code that needs to be aware of the
+ # current user, i.e. when returning all visible projects for <somebody>, the controller should
+ # pass the current user to the model, instead of letting it fetch it by itself through
+ # `User.current`. This method acts as a reminder and wants to encourage you to use it.
+ # Project.visible_by actually allows the controller to pass in a user but it falls back
+ # to `User.current` and there are other places in the session-unaware codebase,
+ # that rely on `User.current`.
+ def current_user
+ User.current
+ end
+
+ def user_setup
+ # Find the current user
+ User.current = find_current_user
+ end
+
+ # check if login is globally required to access the application
+ def check_if_login_required
+ # no check needed if user is already logged in
+ return true if current_user.logged?
+
+ require_login if Setting.login_required?
+ end
+
+ # Returns the current user or nil if no user is logged in
+ # and starts a session if needed
+ def find_current_user
+ if session[:user_id]
+ # existing session
+ User.active.find_by(id: session[:user_id])
+ elsif cookies[OpenProject::Configuration['autologin_cookie_name']] && Setting.autologin?
+ # auto-login feature starts a new session
+ user = User.try_to_autologin(cookies[OpenProject::Configuration['autologin_cookie_name']])
+ session[:user_id] = user.id if user
+ user
+ elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action])
+ # RSS key authentication does not start a session
+ User.find_by_rss_key(params[:key])
+ elsif Setting.rest_api_enabled? && api_request?
+ if (key = api_key_from_request) && accept_key_auth_actions.include?(params[:action])
+ # Use API key
+ User.find_by_api_key(key)
+ end
+ end
+ end
+
+ # Sets the logged in user
+ def logged_user=(user)
+ if user&.is_a?(User)
+ login_user(user)
+ else
+ logout_user
+ end
+ end
+
+ # Logout the current user
+ def logout_user
+ ::Users::LogoutService
+ .new(controller: self)
+ .call(current_user)
+ end
+
+ # Redirect the user according to the logout scheme
+ def perform_post_logout(_prev_session)
+ if Setting.login_required? && omniauth_direct_login?
+ flash.now[:notice] = I18n.t :notice_logged_out
+ render :exit, locals: { instructions: :after_logout }
+ else
+ redirect_to home_url
+ end
+ end
+
+ # Login the current user
+ def login_user(user)
+ ::Users::LoginService
+ .new(controller: self)
+ .call(user)
+ end
+
+ def require_login
+ unless current_user.logged?
+
+ respond_to do |format|
+ format.any(:html, :atom) do
+ # Ensure we reset the session to terminate any old session objects
+ # but ONLY for html requests to avoid double-resetting sessions
+ reset_session
+
+ redirect_to main_app.signin_path(back_url: login_back_url)
+ end
+
+ auth_header = OpenProject::Authentication::WWWAuthenticate.response_header(request_headers: request.headers)
+
+ format.any(:xml, :js, :json) do
+ head :unauthorized,
+ 'X-Reason' => 'login needed',
+ 'WWW-Authenticate' => auth_header
+ end
+
+ format.all { head :not_acceptable }
+ end
+ return false
+ end
+ true
+ end
+
+ def require_admin
+ return unless require_login
+
+ render_403 unless current_user.admin?
+ end
+end
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rb
index 6de22ebd6b1..171da19819d 100644
--- a/app/controllers/concerns/auth_source_sso.rb
+++ b/app/controllers/concerns/auth_source_sso.rb
@@ -34,14 +34,18 @@ module AuthSourceSSO
sso_config && sso_config[:header]
end
- def secret
+ def header_secret
sso_config && sso_config[:secret]
end
- def optional?
+ def header_optional?
sso_config && sso_config[:optional]
end
+ def header_slo_url
+ sso_config && sso_config[:logout_url]
+ end
+
def get_validated_login!(value)
login, valid_secret = extract_from_header(value)
@@ -59,9 +63,9 @@ module AuthSourceSSO
end
def extract_from_header(value)
- if self.secret.present?
- valid_secret = value.end_with?(":#{self.secret}")
- login = value.gsub(/:#{Regexp.escape(self.secret)}\z/, '')
+ if header_secret.present?
+ valid_secret = value.end_with?(":#{header_secret}")
+ login = value.gsub(/:#{Regexp.escape(header_secret)}\z/, '')
[login, valid_secret]
else
@@ -132,12 +136,21 @@ module AuthSourceSSO
def handle_sso_success(user)
session[:user_id] = user.id
+ session[:user_from_auth_header] = true
user
end
+ def perform_post_logout(prev_session)
+ if prev_session[:user_from_auth_header] && header_slo_url.present?
+ redirect_to header_slo_url
+ else
+ super
+ end
+ end
+
def handle_sso_failure!(session_args = {})
- return if optional?
+ return if header_optional?
session[:auth_source_sso_failure] = session_args.merge(
back_url: request.base_url + request.original_fullpath,
diff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rb
new file mode 100644
index 00000000000..0517926f268
--- /dev/null
+++ b/app/services/users/login_service.rb
@@ -0,0 +1,48 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-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.
+#++
+
+module Users
+ class LoginService
+ attr_accessor :controller
+
+ def initialize(controller:)
+ self.controller = controller
+ end
+
+ def call(user)
+ controller.reset_session
+
+ User.current = user
+
+ ::Sessions::InitializeSessionService.call(user, controller.session)
+
+ ServiceResult.new(result: user)
+ end
+ end
+end
diff --git a/app/services/users/logout_service.rb b/app/services/users/logout_service.rb
new file mode 100644
index 00000000000..410592769ff
--- /dev/null
+++ b/app/services/users/logout_service.rb
@@ -0,0 +1,60 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-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.
+#++
+
+module Users
+ class LogoutService
+ attr_accessor :controller
+
+ def initialize(controller:)
+ self.controller = controller
+ end
+
+ def call(user)
+ controller.reset_session
+
+ if user.logged?
+ remove_cookies! controller.send(:cookies)
+ remove_tokens! user
+ end
+
+ User.current = User.anonymous
+ ServiceResult.new(result: User.anonymous)
+ end
+
+ private
+
+ def remove_tokens!(user)
+ Token::AutoLogin.where(user_id: user.id).delete_all
+ end
+
+ def remove_cookies!(cookies)
+ cookies.delete OpenProject::Configuration.autologin_cookie_name
+ end
+ end
+end
diff --git a/docs/system-admin-guide/authentication/kerberos/README.md b/docs/system-admin-guide/authentication/kerberos/README.md
index 3b558ff64f5..1310880decf 100644
--- a/docs/system-admin-guide/authentication/kerberos/README.md
+++ b/docs/system-admin-guide/authentication/kerberos/README.md
@@ -110,20 +110,23 @@ production:
auth_source_sso:
# The header name is configured here
header: X-Authenticated-User
+
# The secret is configurable here
# You can comment it out to disable if your outer server
# fully controls this header value and you trust it.
secret: MyPassword
+
# Uncomment to make the header optional.
# optional: true
-```
-
+ # Specify a logout URL that gets redirected
+ # after the OpenProject internal logout flow
+ # logout_url: https://sso.example.com/logout
+```
Be sure to choose the correct indentation and base key. The `auth_source_sso` key should be indented two spaces (and all other keys accordingly) and the configuration should belong to the `production` group.
-
The configuration can be provided in one of three ways:
* `configuration.yml` file (1.1)
diff --git a/spec/controllers/concerns/auth_source_slo_spec.rb b/spec/controllers/concerns/auth_source_slo_spec.rb
new file mode 100644
index 00000000000..8249186dd8d
--- /dev/null
+++ b/spec/controllers/concerns/auth_source_slo_spec.rb
@@ -0,0 +1,80 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-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'
+
+##
+describe AccountController, 'Auth header logout', type: :controller do
+ render_views
+
+ let!(:auth_source) { DummyAuthSource.create name: "Dummy LDAP" }
+ let!(:user) { FactoryBot.create :user, login: login, auth_source_id: auth_source.id }
+ let(:login) { "h.wurst" }
+
+ before do
+ if sso_config
+ allow(OpenProject::Configuration)
+ .to receive(:auth_source_sso)
+ .and_return(sso_config)
+ end
+ end
+
+ describe 'logout' do
+ context 'when a logout URL is present' do
+ let(:sso_config) do
+ {
+ logout_url: 'https://example.org/foo?logout=true'
+ }
+ end
+
+ context 'and the user came from auth source' do
+ before do
+ login_as user
+ session[:user_from_auth_header] = true
+ get :logout
+ end
+
+ it 'is redirected to the logout URL' do
+ expect(response).to redirect_to 'https://example.org/foo?logout=true'
+ end
+ end
+
+ context 'and the user did not come from auth source' do
+ before do
+ login_as user
+ session[:user_from_auth_header] = nil
+ get :logout
+ end
+
+ it 'is redirected to the home URL' do
+ expect(response).to redirect_to home_url
+ end
+ end
+ end
+ end
+end
diff --git a/spec/controllers/concerns/auth_source_sso_spec.rb b/spec/controllers/concerns/auth_source_sso_spec.rb
index e3739c199df..ac6ddf18b05 100644
--- a/spec/controllers/concerns/auth_source_sso_spec.rb
+++ b/spec/controllers/concerns/auth_source_sso_spec.rb
@@ -44,39 +44,10 @@ describe MyController, type: :controller do
let(:header) { "X-Remote-User" }
let(:secret) { "42" }
- let(:auth_source) { DummyAuthSource.create name: "Dummy LDAP" }
- let(:user) { FactoryBot.create :user, login: login, auth_source_id: auth_source.id }
+ let!(:auth_source) { DummyAuthSource.create name: "Dummy LDAP" }
+ let!(:user) { FactoryBot.create :user, login: login, auth_source_id: auth_source.id }
let(:login) { "h.wurst" }
- before do
- if sso_config
- allow(OpenProject::Configuration)
- .to receive(:auth_source_sso)
- .and_return(sso_config)
- end
-
- auth_source # create auth source
- user # create user
-
- separator = secret ? ':' : ''
- request.headers[header] = "#{login}#{separator}#{secret}"
-
- get :account
- end
-
- it "should log in given user" do
- expect(response.body.squish).to have_content("Username h.wurst")
- end
-
-
- context 'when the secret being null' do
- let(:secret) { nil }
-
- it "should log in given user" do
- expect(response.body.squish).to have_content("Username h.wurst")
- end
- end
-
shared_examples "auth source sso failure" do
def attrs(user)
user.attributes.slice(:login, :mail, :auth_source_id)
@@ -110,30 +81,60 @@ describe MyController, type: :controller do
end
end
- context "with no auth source sso configured" do
- let(:sso_config) { nil }
-
- it "should redirect to login" do
- expect(response).to redirect_to("/login?back_url=http%3A%2F%2Ftest.host%2Fmy%2Faccount")
+ before do
+ if sso_config
+ allow(OpenProject::Configuration)
+ .to receive(:auth_source_sso)
+ .and_return(sso_config)
end
+
+ separator = secret ? ':' : ''
+ request.headers[header] = "#{login}#{separator}#{secret}"
end
- context "with a non-active user user" do
- let(:user) { FactoryBot.create :user, login: login, auth_source_id: auth_source.id, status: 2 }
+ describe 'login' do
+ before do
+ get :account
+ end
- it_should_behave_like "auth source sso failure"
- end
+ it "should log in given user" do
+ expect(response.body.squish).to have_content("Username h.wurst")
+ end
- context "with an invalid user" do
- let(:auth_source) { DummyAuthSource.create name: "Onthefly LDAP", onthefly_register: true }
+ context 'when the secret being null' do
+ let(:secret) { nil }
+
+ it "should log in given user" do
+ expect(response.body.squish).to have_content("Username h.wurst")
+ end
+ end
- let!(:duplicate) { FactoryBot.create :user, mail: "[email protected]" }
- let(:login) { "dummy_dupuser" }
- let(:user) do
- FactoryBot.build :user, login: login, mail: duplicate.mail, auth_source_id: auth_source.id
+ context "with no auth source sso configured" do
+ let(:sso_config) { nil }
+
+ it "should redirect to login" do
+ expect(response).to redirect_to("/login?back_url=http%3A%2F%2Ftest.host%2Fmy%2Faccount")
+ end
+ end
+
+ context "with a non-active user user" do
+ let(:user) { FactoryBot.create :user, login: login, auth_source_id: auth_source.id, status: 2 }
+
+ it_should_behave_like "auth source sso failure"
end
- it_should_behave_like "auth source sso failure"
+ context "with an invalid user" do
+ let(:auth_source) { DummyAuthSource.create name: "Onthefly LDAP", onthefly_register: true }
+
+ let!(:duplicate) { FactoryBot.create :user, mail: "[email protected]" }
+ let(:login) { "dummy_dupuser" }
+
+ let(:user) do
+ FactoryBot.build :user, login: login, mail: duplicate.mail, auth_source_id: auth_source.id
+ end
+
+ it_should_behave_like "auth source sso failure"
+ end
end
end