@@ -4,6 +4,7 @@ module Authenticatable
4
4
5
5
def self . included ( base )
6
6
base . instance_exec do
7
+ before_action :handle_user_cache_cookie!
7
8
before_action :authenticate_user!
8
9
helper_method :sign_out_path , :sign_up_path , :sign_in_path ,
9
10
:current_user , :user_signed_in?
@@ -33,9 +34,7 @@ def authenticate_user
33
34
end
34
35
35
36
def authenticate_user!
36
- if user_signed_in?
37
- handle_user_cache_cookie
38
- else
37
+ unless user_signed_in?
39
38
if request . format == :json
40
39
render json : { error : '401 Unauthorized' } , status : :unauthorized
41
40
else
@@ -44,6 +43,43 @@ def authenticate_user!
44
43
end
45
44
end
46
45
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
+
47
83
def current_user = ( u )
48
84
@current_user = u
49
85
end
@@ -91,35 +127,6 @@ def redirect_to_sign_in
91
127
end
92
128
end
93
129
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
-
122
130
end
123
-
124
131
end
125
132
end
0 commit comments