File tree Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ def create
11
11
current_user = @user
12
12
session [ :user_id ] = @user . id
13
13
session [ :user_cache_key ] = cookies [ :_radius_user_cache_key ]
14
+ session [ :token_expires_at ] = Time . zone . at ( auth_hash [ :credentials ] [ :expires_at ] )
14
15
redirect_to return_to_path
15
16
end
16
17
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ def authenticate_user
34
34
end
35
35
36
36
def authenticate_user!
37
+ check_token_expiry!
37
38
unless user_signed_in?
38
39
if request . format == :json
39
40
render json : { error : '401 Unauthorized' } , status : :unauthorized
@@ -43,6 +44,12 @@ def authenticate_user!
43
44
end
44
45
end
45
46
47
+ def check_token_expiry!
48
+ if session [ :token_expires_at ] . nil? || session [ :token_expires_at ] < Time . zone . now
49
+ session . delete :user_id
50
+ end
51
+ end
52
+
46
53
# We needed a way to update the user information on kracken and
47
54
# automatically update all the client apps. Instead of pushing changes
48
55
# to all the apps we added a cookie that will act as an indicator that
Original file line number Diff line number Diff line change @@ -92,6 +92,22 @@ class ControllerDouble < BaseControllerDouble
92
92
end
93
93
94
94
95
+ it "redirects to sign-in when token has expired" do
96
+ allow ( controller ) . to receive ( :request ) . and_return ( double ( format : nil , fullpath : nil ) )
97
+ allow ( controller ) . to receive ( :redirect_to )
98
+ controller . session [ :token_expires_at ] = Time . zone . now - 5 . minutes
99
+ controller . authenticate_user!
100
+ expect ( controller ) . to have_received ( :redirect_to )
101
+ end
102
+
103
+ it "authenticates user when token has not expired" do
104
+ allow ( controller ) . to receive ( :request ) . and_return ( double ( format : nil , fullpath : nil ) )
105
+ allow ( controller ) . to receive ( :redirect_to )
106
+ controller . session [ :token_expires_at ] = Time . zone . now + 5 . minutes
107
+ controller . authenticate_user!
108
+ expect ( controller ) . to_not have_received ( :redirect_to )
109
+ end
110
+
95
111
context "user cache cookie" do
96
112
it "nothing if the cache cookie does not exist" do
97
113
allow ( controller ) . to receive ( :request ) . and_return ( double ( format : nil , fullpath : nil ) )
Original file line number Diff line number Diff line change 2
2
3
3
module Kracken
4
4
RSpec . describe "authenticatable resource requests" , type : :request do
5
+ let ( :token_expiry ) { 1441650437 }
6
+ let ( :auth_hash ) {
7
+ {
8
+ "credentials" => {
9
+ "token" => "8675c978497731f60ecdea6787c4316b" ,
10
+ "refresh_token" => "7340329b1e0d7a6749bdfb2ca1597360" ,
11
+ "expires_at" => token_expiry ,
12
+ "expires" => true
13
+ }
14
+ }
15
+ }
16
+
5
17
def headers_with_token ( token )
6
18
{ 'HTTP_AUTHORIZATION' => "Token token=\" #{ token } \" " }
7
19
end
@@ -16,5 +28,10 @@ def headers_with_token(token)
16
28
expect ( response . status ) . to be 200
17
29
end
18
30
31
+ it "sets :token_expires_at in the session" do
32
+ OmniAuth . config . mock_auth [ :radius ] = OmniAuth ::AuthHash . new ( auth_hash )
33
+ get "/auth/radius/callback"
34
+ expect ( request . session [ :token_expires_at ] ) . to eq ( Time . zone . at ( token_expiry ) )
35
+ end
19
36
end
20
37
end
You can’t perform that action at this time.
0 commit comments