Skip to content

Commit 373a5f3

Browse files
committed
Use sub, exp, iat as payload
`JWT::ExpiredSignature: Signature has expired` is expected when a token is expired. Review comment is: Sorcery#70 (comment)
1 parent d66f47a commit 373a5f3

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

lib/sorcery/controller/submodules/jwt_auth.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ module InstanceMethods
1111
def jwt_auth(*credentials)
1212
user = user_class.authenticate(*credentials)
1313
if user
14-
user_params = Config.jwt_user_params.each_with_object({}) do |val, acc|
15-
acc[val] = user.public_send(val)
16-
end
14+
now = Time.current
15+
default_payload = {
16+
sub: user.id,
17+
exp: (now + 3.days).to_i,
18+
iat: now.to_i
19+
}
1720

18-
payload = user_params.merge Config.jwt_payload
21+
payload = default_payload.merge Config.jwt_payload
1922

20-
{ Config.jwt_user_data_key => user_params,
23+
{ Config.jwt_user_data_key => default_payload,
2124
Config.jwt_auth_token_key => jwt_encode(payload) }
2225
end
2326
end
2427

2528
# To be used as a before_action.
2629
def jwt_require_auth
30+
binding.pry
2731
@current_user = Config.jwt_set_user ? User.find(jwt_user_id) : jwt_user_data
2832
rescue JWT::DecodeError => e
2933
jwt_not_authenticated(message: e.message) && return
@@ -57,7 +61,7 @@ def jwt_user_data(token = jwt_from_header)
5761
# Return user id from user data if id present.
5862
# Else return nil
5963
def jwt_user_id
60-
jwt_user_data.try(:[], :id)
64+
jwt_user_data[:sub]
6165
end
6266

6367
# This method called if user not authenticated

spec/controllers/controller_jwt_auth_spec.rb

+29-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
let!(:user) { double('user', id: 42) }
55
before(:each) do
66
request.env['HTTP_ACCEPT'] = "application/json" if ::Rails.version < '5.0.0'
7+
Timecop.freeze(Time.new(2019, 01, 14, 19, 00, 00))
78
end
89

910
describe 'with jwt auth features' do
1011
let(:user_email) { '[email protected]' }
1112
let(:user_password) { 'testpass' }
12-
let(:auth_token) { 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDJ9.TIAi77DJvww5hA1DHOWfoMmWWsjEmDWMa3pJbZreTJc' }
13+
let(:auth_token) { 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQyLCJleHAiOjE1NDc3MTkyMDAsImlhdCI6MTU0NzQ2MDAwMH0.QM5mTkYiDwI-10cEOq4b_bfrwe99BRuef6pnIB-jqIk' }
1314
let(:response_data) do
1415
{
15-
user_data: { id: user.id },
16+
user_data: {
17+
sub: user.id,
18+
exp: 1547719200,
19+
iat: 1547460000
20+
},
1621
auth_token: auth_token
1722
}
1823
end
@@ -88,7 +93,29 @@
8893
expect(JSON.parse(response.body)["error"]["message"]).not_to be nil
8994
end
9095
end
96+
97+
context "token is expired" do
98+
before do
99+
Timecop.freeze(Time.new(2099, 01, 14, 19, 00, 00))
100+
request.headers.merge! Authorization: auth_token
101+
end
102+
103+
it "does return 401" do
104+
get :some_action_jwt, format: :json
105+
106+
expect(response.status).to eq(401)
107+
expect(JSON.parse(response.body)["error"]["message"]).not_to be nil
108+
end
109+
110+
after do
111+
Timecop.return
112+
end
113+
end
91114
end
92115
end
93116
end
117+
118+
after do
119+
Timecop.return
120+
end
94121
end

0 commit comments

Comments
 (0)