Skip to content

Commit 90e17b9

Browse files
use JWTUtil instead of calling JWT.encode/decode
1 parent 041cf22 commit 90e17b9

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

app/controllers/application_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class ApplicationController < ActionController::API
22
before_action :user_checks
33

44
include RSAUtil
5+
include JWTUtil
56

67
def user_checks
78
@token = get_current_token
@@ -11,13 +12,14 @@ def user_checks
1112
def get_current_user
1213
if @token
1314
begin
14-
decoded_token = JWT.decode(@token, OpenSSL::PKey::RSA::new(RSAUtil::Keys::priv, ENV['PASSPHRASE']), true, { algorithm: 'RS256' })
15+
decoded_token = JWTUtil::decode(@token)
1516
rescue JWT::DecodeError
1617
end
1718

1819
if decoded_token == nil
1920
render json: { error: 'Invalid token' }, status: :unauthorized
2021
else
22+
print decoded_token
2123
subject = decoded_token[0]['sub']
2224

2325
token = Token.find_by(user_id: subject.split('.')[0])
@@ -59,7 +61,7 @@ def is_valid_token(token)
5961

6062
token.sub!('Bearer ','')
6163
begin
62-
JWT.decode(token, OpenSSL::PKey::RSA::new(RSAUtil::Keys::pub, ENV['PASSPHRASE']), true, { algorithm: 'RS256' })
64+
JWTUtil::decode(token)
6365
return true
6466
rescue JWT::DecodeError
6567
Rails.logger.warn 'Error decoding the JWT: ' + e.to_s

app/models/user.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class User < ApplicationRecord
88
after_create :generate_token, :create_confirm_email_key
99

1010
include RSAUtil
11+
include JWTUtil
1112

1213
def hike_events
1314
HikeEvent.where(user_id: self.id)
@@ -23,27 +24,11 @@ def remove_token
2324

2425
def generate_token (overwrite = false)
2526
if !self.token.nil? && overwrite
26-
self.token.token = JWT.encode(
27-
{
28-
iss: 'probably digitalocean or some shit',
29-
iat: Time.now.to_i,
30-
sub: self.id
31-
},
32-
OpenSSL::PKey::RSA.new(RSAUtil::Keys::priv, ENV['PASSPHRASE']),
33-
'RS256'
34-
)
27+
self.token.token = JWTUtil::encode(self)
3528
self.token.save
3629
elsif self.token.nil?
3730
self.token = Token.new(
38-
token: JWT.encode(
39-
{
40-
iss: 'probably digitalocean or some shit',
41-
iat: Time.now.to_i,
42-
sub: self.id + Time.now.to_s
43-
},
44-
OpenSSL::PKey::RSA.new(RSAUtil::Keys::priv, ENV['PASSPHRASE']),
45-
'RS256'
46-
),
31+
token: JWTUtil::encode(self),
4732
user_id: self.id
4833
)
4934
self.token.save

0 commit comments

Comments
 (0)