Skip to content

Commit e753701

Browse files
committed
Handle the user cache cookie on public pages
Any pages that didn't require a current user were ignoring the cache key cookie. This change will look for the existence of that and update or log out the user as appropriate. The logic is a bit murky, but with my local testing it appears to work in all the edge cases, certainly something that could be cleaned up.
1 parent 675d370 commit e753701

File tree

4 files changed

+68
-40
lines changed

4 files changed

+68
-40
lines changed

app/controllers/kracken/sessions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Kracken
22
class SessionsController < ApplicationController
33
skip_before_filter :authenticate_user!, except: [:index]
4+
skip_before_filter :handle_user_cache_cookie!, except: [:index]
45

56
def index
67
end

lib/kracken/controllers/authenticatable.rb

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Authenticatable
44

55
def self.included(base)
66
base.instance_exec do
7+
before_action :handle_user_cache_cookie!
78
before_action :authenticate_user!
89
helper_method :sign_out_path, :sign_up_path, :sign_in_path,
910
:current_user, :user_signed_in?
@@ -33,9 +34,7 @@ def authenticate_user
3334
end
3435

3536
def authenticate_user!
36-
if user_signed_in?
37-
handle_user_cache_cookie
38-
else
37+
unless user_signed_in?
3938
if request.format == :json
4039
render json: {error: '401 Unauthorized'}, status: :unauthorized
4140
else
@@ -44,6 +43,43 @@ def authenticate_user!
4443
end
4544
end
4645

46+
# We needed a way to update the user information on kracken and
47+
# automatically update all the client apps. Instead of pushing changes
48+
# to all the apps we added a cookie that will act as an indicator that
49+
# the user is stale and they need to refresh them.
50+
#
51+
# The refresh is accomplished by redirecting to the normal oauth flow
52+
# which will simply redirect the back if they are already signed in (or
53+
# ask for a user/pass if they are not).
54+
#
55+
# This method will:
56+
#
57+
# - Check for the `_radius_user_cache_key` tld cookie
58+
# - If the key is "none" log them out
59+
# - Compare it to the `user_cache_key` in the session
60+
# - If they don't match, redirect them to the oauth provider and
61+
# delete the cookie
62+
#
63+
def handle_user_cache_cookie!
64+
if cookies[:_radius_user_cache_key]
65+
if cookies[:_radius_user_cache_key] == "none"
66+
# Sign out current user
67+
session.delete :user_id
68+
69+
# Clear that user's cache key
70+
session.delete :user_cache_key
71+
72+
elsif session[:user_cache_key] != cookies[:_radius_user_cache_key]
73+
# Delete the cookie to prevent redirect loops
74+
cookies.delete :_radius_user_cache_key
75+
76+
# Redirect to the account app
77+
redirect_to_sign_in
78+
end
79+
end
80+
end
81+
82+
4783
def current_user=(u)
4884
@current_user = u
4985
end
@@ -91,35 +127,6 @@ def redirect_to_sign_in
91127
end
92128
end
93129

94-
# We needed a way to update the user information on kracken and
95-
# automatically update all the client apps. Instead of pushing changes
96-
# to all the apps we added a cookie that will act as an indicator that
97-
# the user is stale and they need to refresh them.
98-
#
99-
# The refresh is accomplished by redirecting to the normal oauth flow
100-
# which will simply redirect the back if they are already signed in (or
101-
# ask for a user/pass if they are not).
102-
#
103-
# This method will:
104-
#
105-
# - Check for the `_radius_user_cache_key` tld cookie
106-
# - Compare it to the `user_cache_key` in the session
107-
# - If they don't match, redirect them to the oauth provider and
108-
# delete the cookie
109-
#
110-
def handle_user_cache_cookie
111-
if cookies[:_radius_user_cache_key]
112-
if session[:user_cache_key] != cookies[:_radius_user_cache_key]
113-
# Delete the cookie to prevent redirect loops
114-
cookies.delete :_radius_user_cache_key
115-
116-
# Redirect to the account app
117-
redirect_to_sign_in
118-
end
119-
end
120-
end
121-
122130
end
123-
124131
end
125132
end

lib/kracken/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Kracken
2-
VERSION = "0.0.9"
2+
VERSION = "0.0.10"
33
end

spec/kracken/controllers/authenticatable_spec.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,46 @@ class ControllerDouble < BaseControllerDouble
9393

9494

9595
context "user cache cookie" do
96-
it "redirects when the cache cookie is different than the session" do
96+
it "nothing if the cache cookie does not exist" do
9797
allow(controller).to receive(:request).and_return(double(format: nil, fullpath: nil))
98-
allow(controller).to receive(:cookies).and_return({_radius_user_cache_key: "123"})
9998
allow(controller).to receive(:redirect_to)
99+
controller.session[:user_cache_key] = "123"
100100

101-
controller.authenticate_user!
101+
controller.handle_user_cache_cookie!
102102

103-
expect(controller).to have_received(:redirect_to).with("/")
103+
expect(controller).to_not have_received(:redirect_to)
104104
end
105105

106-
it "does not redirect when the cache cookie matches the session" do
106+
it "signs the current user out when the cache cookie is 'none'" do
107107
allow(controller).to receive(:request).and_return(double(format: nil, fullpath: nil))
108108
allow(controller).to receive(:redirect_to)
109-
110109
controller.cookies[:_radius_user_cache_key] = "123"
111110
controller.session[:user_cache_key] = "123"
112111

113-
controller.authenticate_user!
112+
controller.handle_user_cache_cookie!
113+
114+
expect(controller).to_not have_received(:redirect_to)
115+
end
116+
117+
it "redirects when the cache cookie is different than the session" do
118+
allow(controller).to receive(:request).and_return(double(format: nil, fullpath: nil))
119+
allow(controller).to receive(:cookies).and_return({_radius_user_cache_key: "123"})
120+
allow(controller).to receive(:redirect_to)
121+
controller.handle_user_cache_cookie!
122+
123+
expect(controller).to have_received(:redirect_to).with("/")
124+
end
125+
126+
it "does not redirect when the cache cookie matches the session" do
127+
controller.session = spy
128+
allow(controller).to receive(:redirect_to)
129+
controller.cookies[:_radius_user_cache_key] = "none"
130+
131+
controller.handle_user_cache_cookie!
114132

115133
expect(controller).to_not have_received(:redirect_to)
134+
expect(controller.session).to have_received(:delete).with(:user_id)
135+
expect(controller.session).to have_received(:delete).with(:user_cache_key)
116136
end
117137
end
118138
end

0 commit comments

Comments
 (0)