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

Assign autologin session link, and use it to show and delete sessions

Oliver Günther8fee175faf66
diff --git a/app/controllers/my/auto_login_tokens_controller.rb b/app/controllers/my/auto_login_tokens_controller.rb
index c75274c2753..176b4e6bf6a 100644
--- a/app/controllers/my/auto_login_tokens_controller.rb
+++ b/app/controllers/my/auto_login_tokens_controller.rb
@@ -41,6 +41,11 @@ module My
def destroy
@token.destroy
+ # Destroy all linked sessions to it
+ linked_sessions = Sessions::AutologinSessionLink.where(token_id: @token.id)
+ Sessions::UserSession.where(id: linked_sessions.select(:session_id)).delete_all
+ linked_sessions.delete_all
+
flash[:notice] = I18n.t(:notice_successful_delete)
redirect_to my_sessions_path
end
diff --git a/app/controllers/my/sessions_controller.rb b/app/controllers/my/sessions_controller.rb
index aa8fe3a1b91..19b6eefd21a 100644
--- a/app/controllers/my/sessions_controller.rb
+++ b/app/controllers/my/sessions_controller.rb
@@ -44,14 +44,15 @@ module My
menu_item :sessions
def index
- @sessions = ::Sessions::UserSession
- .for_user(current_user)
- .order(updated_at: :desc)
-
@autologin_tokens = ::Token::AutoLogin
.for_user(current_user)
.order(expires_on: :asc)
+ @unmapped_sessions = ::Sessions::UserSession
+ .for_user(current_user)
+ .order(updated_at: :desc)
+ .where.not(id: ::Sessions::AutologinSessionLink.where(token_id: @autologin_tokens.select(:id)).select(:session_id))
+
token = cookies[OpenProject::Configuration["autologin_cookie_name"]]
if token
@current_token = @autologin_tokens.find_by_plaintext_value(token) # rubocop:disable Rails/DynamicFindBy
diff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rb
index 3eaae27309d..427f26e30e4 100644
--- a/app/services/users/login_service.rb
+++ b/app/services/users/login_service.rb
@@ -44,12 +44,14 @@ module Users
def call!
autologin_requested = session.delete(:autologin_requested)
+ autologin_token_id = session.delete(:autologin_token_id)
retain_session_values do
reset_session!
User.current = user
set_autologin_cookie if autologin_requested
+ assign_autologin_session(autologin_token_id) if autologin_token_id.present?
end
successful_login
@@ -61,18 +63,25 @@ module Users
return unless Setting::Autologin.enabled?
# generate a key and set cookie if autologin
- expires_on = Setting.autologin.days.from_now.beginning_of_day
- token = Token::AutoLogin.create(user:, data: session_identification, expires_on:)
+ token = Token::AutoLogin.create(user:, data: token_session_information)
cookie_options = {
value: token.plain_value,
# The autologin expiry is checked on validating the token
# but still expire the cookie to avoid unnecessary retries
- expires: expires_on,
+ expires: token.expires_on,
path: OpenProject::Configuration["autologin_cookie_path"],
secure: OpenProject::Configuration.https?,
httponly: true
}
cookies[OpenProject::Configuration["autologin_cookie_name"]] = cookie_options
+
+ # Tie the current session to this autologin token already
+ assign_autologin_session(token.id)
+ end
+
+ def assign_autologin_session(token_id)
+ session_id = ::Sessions::UserSession.where(session_id: session.id.private_id).pick(:id)
+ ::Sessions::AutologinSessionLink.create(token_id:, session_id:)
end
def successful_login
@@ -122,6 +131,10 @@ module Users
}
end
+ def token_session_information
+ session_identification.merge(session_id: session.id)
+ end
+
def retained_session_values
controller.session.to_h.slice *(default_retained_keys + omniauth_provider_keys)
end
@@ -137,7 +150,7 @@ module Users
end
def default_retained_keys
- %w[omniauth_provider user_from_auth_header]
+ %w[omniauth_provider user_from_auth_header autologin_token_id]
end
##
diff --git a/app/views/my/sessions/index.html.erb b/app/views/my/sessions/index.html.erb
index 54249157fde..07701af1509 100644
--- a/app/views/my/sessions/index.html.erb
+++ b/app/views/my/sessions/index.html.erb
@@ -39,16 +39,8 @@ See COPYRIGHT and LICENSE files for more details.
end
%>
-<%# Collect a “fingerprint” of each token: user_id + platform + browser %>
-<% token_fingerprints = @autologin_tokens.map { |t| [t.user_id, t.data["platform"], t.data["browser"]] } %>
-
-<%# Reject sessions that match any token fingerprint %>
-<% rows = @sessions.reject do |s|
- token_fingerprints.include?([s.user_id, s.data["platform"], s.data["browser"]])
- end + @autologin_tokens %>
-
<%= render ::Users::Sessions::TableComponent.new(
- rows: rows,
+ rows: @autologin_tokens + @unmapped_sessions,
current_session: session,
current_token: @current_token
) %>
diff --git a/spec/services/users/login_service_spec.rb b/spec/services/users/login_service_spec.rb
index ecfe924490b..fb99349e2ea 100644
--- a/spec/services/users/login_service_spec.rb
+++ b/spec/services/users/login_service_spec.rb
@@ -33,7 +33,8 @@ require "spec_helper"
RSpec.describe Users::LoginService, type: :model do
shared_let(:input_user) { create(:user) }
let(:request) { {} }
- let(:session) { {} }
+ let(:session) { ActionController::TestSession.new }
+ let!(:user_session) { create(:user_session, session_id: session.id.private_id) }
let(:browser) do
instance_double(Browser::Safari,
name: "Safari",
@@ -175,11 +176,12 @@ RSpec.describe Users::LoginService, type: :model do
tokens = Token::AutoLogin.where(user: input_user)
expect(tokens.count).to eq 1
expect(tokens.first.user_id).to eq input_user.id
+ expect(tokens.first.expires_on).to be_within(1.minute).of(1.day.from_now)
autologin_cookie = cookies[OpenProject::Configuration["autologin_cookie_name"]]
expect(autologin_cookie).to be_present
expect(autologin_cookie[:value]).to be_present
- expect(autologin_cookie[:expires]).to eq (Time.zone.today + 1.day).beginning_of_day
+ expect(autologin_cookie[:expires]).to be_within(1.minute).of(1.day.from_now)
expect(autologin_cookie[:path]).to eq "/"
expect(autologin_cookie[:httponly]).to be true
expect(autologin_cookie[:secure]).to eq Setting.https?