Skip to content

Commit 63e2157

Browse files
simonbairdclaude
andcommitted
Check if already signed in on sso callback
Avoid trashing an existing login unexpectedly. Co-authored-by: Claude Code <noreply@anthropic.com>
1 parent 7e54b19 commit 63e2157

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

rails/app/controllers/sso_controller.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ def authorize
2525
# GET /sso/callback?token=xyz
2626
# Runs on custom domain. Verifies token, creates session, redirects.
2727
def callback
28-
# Clear any stale session before creating a new one (also prevents session fixation)
29-
reset_session
3028
data = SsoToken.verify(params[:token], domain: request.host)
3129
unless data
3230
render plain: "Invalid or expired token", status: :forbidden
@@ -39,7 +37,12 @@ def callback
3937
return
4038
end
4139

42-
sign_in(:user, user)
40+
unless current_user == user
41+
# Clear any stale session before creating a new one (also prevents session fixation)
42+
reset_session
43+
sign_in(:user, user)
44+
end
45+
4346
redirect_to safe_return_to(data[:return_to]) || "/"
4447
end
4548

rails/test/controllers/custom_domain_auth_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ class CustomDomainAuthTest < ActionDispatch::IntegrationTest
143143
assert_response :not_found
144144
end
145145

146+
test 'sso callback when already signed in as correct user stays signed in' do
147+
token = SsoToken.generate(user_id: @user.id, domain: 'customtest.example.com')
148+
host! 'customtest.example.com'
149+
150+
# First SSO callback to establish session
151+
get "/sso/callback?token=#{CGI.escape(token)}"
152+
assert_response :redirect
153+
154+
# Second SSO callback should not disrupt existing session
155+
token2 = SsoToken.generate(user_id: @user.id, domain: 'customtest.example.com')
156+
get "/sso/callback?token=#{CGI.escape(token2)}"
157+
assert_redirected_to '/'
158+
159+
# User should still be signed in
160+
get '/'
161+
assert_response :success
162+
end
163+
146164
# -- Subdomain redirect --
147165

148166
test 'tiddlyhost subdomain redirects to custom domain' do

0 commit comments

Comments
 (0)