Skip to content

Commit 4e1b5bc

Browse files
committed
Merge pull request #10 from RadiusNetworks/check-token-expiry
set token expiry in session and check expiry when authenticating
2 parents 40a4515 + fff2b4f commit 4e1b5bc

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

app/controllers/kracken/sessions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def create
1111
current_user = @user
1212
session[:user_id] = @user.id
1313
session[:user_cache_key] = cookies[:_radius_user_cache_key]
14+
session[:token_expires_at] = Time.zone.at(auth_hash[:credentials][:expires_at])
1415
redirect_to return_to_path
1516
end
1617

lib/kracken/controllers/authenticatable.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def authenticate_user
3434
end
3535

3636
def authenticate_user!
37+
check_token_expiry!
3738
unless user_signed_in?
3839
if request.format == :json
3940
render json: {error: '401 Unauthorized'}, status: :unauthorized
@@ -43,6 +44,12 @@ def authenticate_user!
4344
end
4445
end
4546

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+
4653
# We needed a way to update the user information on kracken and
4754
# automatically update all the client apps. Instead of pushing changes
4855
# to all the apps we added a cookie that will act as an indicator that

spec/kracken/controllers/authenticatable_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ class ControllerDouble < BaseControllerDouble
9292
end
9393

9494

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+
95111
context "user cache cookie" do
96112
it "nothing if the cache cookie does not exist" do
97113
allow(controller).to receive(:request).and_return(double(format: nil, fullpath: nil))

spec/requests/authentication_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
module Kracken
44
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+
517
def headers_with_token(token)
618
{ 'HTTP_AUTHORIZATION'=>"Token token=\"#{token}\"" }
719
end
@@ -16,5 +28,10 @@ def headers_with_token(token)
1628
expect(response.status).to be 200
1729
end
1830

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
1936
end
2037
end

0 commit comments

Comments
 (0)