Skip to content

Commit 76faafb

Browse files
committed
Merge pull request #6 from RadiusNetworks/cache-cookie
Handle the user cache cookie on public pages
2 parents 675d370 + 2843a04 commit 76faafb

File tree

5 files changed

+77
-56
lines changed

5 files changed

+77
-56
lines changed

Gemfile.lock

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
kracken (0.0.9)
4+
kracken (0.0.10)
55
faraday (~> 0.8)
66
omniauth (~> 1.0)
77
omniauth-oauth2 (~> 1.1)
@@ -55,21 +55,20 @@ GEM
5555
erubis (2.7.0)
5656
faraday (0.9.1)
5757
multipart-post (>= 1.2, < 3)
58-
globalid (0.3.3)
58+
globalid (0.3.5)
5959
activesupport (>= 4.1.0)
60-
hashie (3.4.0)
61-
hike (1.2.3)
60+
hashie (3.4.1)
6261
i18n (0.7.0)
6362
json (1.8.2)
6463
jwt (1.4.1)
65-
loofah (2.0.1)
64+
loofah (2.0.2)
6665
nokogiri (>= 1.5.9)
6766
mail (2.6.3)
6867
mime-types (>= 1.16, < 3)
6968
method_source (0.8.2)
70-
mime-types (2.4.3)
69+
mime-types (2.5)
7170
mini_portile (0.6.2)
72-
minitest (5.5.1)
71+
minitest (5.6.1)
7372
multi_json (1.11.0)
7473
multi_xml (0.5.5)
7574
multipart-post (2.0.0)
@@ -84,9 +83,7 @@ GEM
8483
omniauth (1.2.2)
8584
hashie (>= 1.2, < 4)
8685
rack (~> 1.0)
87-
omniauth-oauth2 (1.2.0)
88-
faraday (>= 0.8, < 0.10)
89-
multi_json (~> 1.3)
86+
omniauth-oauth2 (1.3.0)
9087
oauth2 (~> 1.0)
9188
omniauth (~> 1.2)
9289
pry (0.10.1)
@@ -95,7 +92,7 @@ GEM
9592
slop (~> 3.4)
9693
pry-nav (0.2.4)
9794
pry (>= 0.9.10, < 0.11.0)
98-
rack (1.6.0)
95+
rack (1.6.1)
9996
rack-test (0.6.3)
10097
rack (>= 1.0)
10198
rails (4.2.1)
@@ -142,18 +139,14 @@ GEM
142139
rspec-support (3.2.2)
143140
safe_yaml (1.0.4)
144141
slop (3.6.0)
145-
sprockets (2.12.3)
146-
hike (~> 1.2)
147-
multi_json (~> 1.0)
142+
sprockets (3.0.3)
148143
rack (~> 1.0)
149-
tilt (~> 1.1, != 1.3.0)
150144
sprockets-rails (2.2.4)
151145
actionpack (>= 3.0)
152146
activesupport (>= 3.0)
153147
sprockets (>= 2.8, < 4.0)
154148
thor (0.19.1)
155149
thread_safe (0.3.5)
156-
tilt (1.4.1)
157150
tzinfo (1.2.2)
158151
thread_safe (~> 0.1)
159152
webmock (1.20.4)

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)