Skip to content

Commit b9f1504

Browse files
Modifies cookie cache value check to also check with redis-based value
1 parent ddfcd8f commit b9f1504

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

lib/kracken/controllers/authenticatable.rb

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,19 @@ def check_token_expiry!
7171
# delete the cookie
7272
#
7373
def handle_user_cache_cookie!
74-
if cookies[:_radius_user_cache_key]
75-
if cookies[:_radius_user_cache_key] == "none"
76-
# Sign out current user
77-
session.delete :user_id
78-
79-
# Clear that user's cache key
80-
session.delete :user_cache_key
81-
82-
elsif session[:user_cache_key] != cookies[:_radius_user_cache_key]
83-
# Delete the cookie to prevent redirect loops
84-
cookies.delete :_radius_user_cache_key
85-
86-
# Redirect to the account app
87-
redirect_to_sign_in
74+
if SESSION_REDIS
75+
handle_user_cache_cookie_with_redis
76+
else
77+
if cookies[:_radius_user_cache_key]
78+
if cookies[:_radius_user_cache_key] == "none"
79+
delete_session_data
80+
elsif session[:user_cache_key] != cookies[:_radius_user_cache_key]
81+
clear_cache_cookie_and_sign_out
82+
end
8883
end
8984
end
9085
end
9186

92-
9387
def current_user=(u)
9488
@current_user = u
9589
end
@@ -124,6 +118,38 @@ def user_signed_in?
124118

125119
private
126120

121+
def handle_user_cache_cookie_with_redis
122+
# If the user passes us a cache key cookie:
123+
if cookies[:_radius_user_cache_key]
124+
expected_val = SESSION_REDIS.get(cookies[:_radius_user_cache_key])
125+
126+
# And we do not have that cookie in Redis
127+
if !expected_val
128+
delete_session_data
129+
# Or we have it in Redis, but it may be somebody else's
130+
# - it's not what we expect from their session
131+
elsif expected_val && expected_val != session[:user_cache_key]
132+
clear_cache_cookie_and_sign_out
133+
end
134+
end
135+
end
136+
137+
def delete_session_data
138+
# Sign out current user
139+
session.delete :user_id
140+
141+
# Clear that user's cache key
142+
session.delete :user_cache_key
143+
end
144+
145+
def clear_cache_cookie_and_sign_out
146+
# Delete the cookie to prevent redirect loops
147+
cookies.delete :_radius_user_cache_key
148+
149+
# Redirect to the account app
150+
redirect_to_sign_in
151+
end
152+
127153
def user_class
128154
Kracken.config.user_class
129155
end

0 commit comments

Comments
 (0)