Skip to content

Commit 1dac3d6

Browse files
Merge branch 'master' into issue-28_store-lat/lng-as-strings
2 parents 9d4b2b9 + 8bd55a9 commit 1dac3d6

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
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

config/initializers/util.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require 'rsa_util.rb'
2-
require 'str_util.rb'
2+
require 'str_util.rb'
3+
require 'jwt_util.rb'

lib/jwt_util.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module JWTUtil
2+
include RSAUtil
3+
4+
def self::encode(user)
5+
JWT.encode(
6+
{
7+
iss: 'probably digitalocean or some shit',
8+
iat: Time.now.to_i,
9+
sub: user.id + '.' + Time.now.to_s
10+
},
11+
OpenSSL::PKey::RSA.new(RSAUtil::Keys::priv, ENV['PASSPHRASE']),
12+
'RS256'
13+
)
14+
end
15+
16+
def self::decode(token)
17+
begin
18+
JWT.decode(
19+
token,
20+
OpenSSL::PKey::RSA.new(RSAUtil::Keys::pub, ENV['PASSPHRASE']),
21+
true,
22+
{ algorithm: 'RS256' }
23+
)
24+
rescue JWT::DecodeError
25+
return nil
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)