tinytokenauth-rails 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/tinytokenauth/authorizable.rb +8 -63
- data/lib/tinytokenauth/json_web_token.rb +2 -9
- data/lib/tinytokenauth/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66680bf32b4e6d3afa62c34a808ff3fcd9dcf95dcc5b80fc695930772cd492eb
|
4
|
+
data.tar.gz: ba4a8953327622f5c5f3fc99afd006f55d2aff301a55f84f261956d0821d2c4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ad22f16e163112c36ab7642dd7e2f32911e7fc593c68cf63ae48ca5a7dcbc62fb5becc45459fab0fecbac1d90e3a10d4c5ea7376c5d235949cdbba5c963e458
|
7
|
+
data.tar.gz: 15b53b180f1e071fb8ae9f7c1c8078f1ab822e6757484b36551275514be6149aa057ae65afa78e78e1402c1973e6e989a54a14afc30cc398cc9ac08b309e6020
|
data/Gemfile.lock
CHANGED
@@ -19,9 +19,9 @@ module Tinytokenauth
|
|
19
19
|
token = header.split(' ').last if header
|
20
20
|
|
21
21
|
begin
|
22
|
-
@decoded = JsonWebToken.decode(token)
|
23
|
-
# @current_user =
|
24
|
-
@current_user =
|
22
|
+
@decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
|
23
|
+
# @current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
|
24
|
+
@current_user = User.find @decoded[:user_id]
|
25
25
|
rescue ActiveRecord::RecordNotFound => e
|
26
26
|
render json: { errors: e.message }, status: :unauthorized
|
27
27
|
rescue JWT::DecodeError => e
|
@@ -29,45 +29,11 @@ module Tinytokenauth
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
# def require_current_user(klass = User)
|
33
|
-
# token = cookies['klap-auth']
|
34
|
-
# # p "token from cookie: #{token}"
|
35
|
-
#
|
36
|
-
# begin
|
37
|
-
# @decoded = JsonWebToken.decode(token)
|
38
|
-
# # @current_user = User.find(@decoded[:user_id])
|
39
|
-
# @current_user = klass.send 'find', @decoded[:user_id]
|
40
|
-
# @exp = @decoded[:exp]
|
41
|
-
# # if @exp < 24.hours.from_now.to_i # Always refresh token
|
42
|
-
# if @exp < 4.hours.from_now.to_i # Always refresh token
|
43
|
-
# sign_in @current_user
|
44
|
-
# end
|
45
|
-
# rescue ActiveRecord::RecordNotFound => e
|
46
|
-
# # TODO: evaluate if we should always forward
|
47
|
-
# redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
|
48
|
-
# rescue JWT::DecodeError => e
|
49
|
-
# # TODO: evaluate if we should always forward
|
50
|
-
# # render json: { errors: e.message }, status: :unauthorized
|
51
|
-
# redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
|
52
|
-
# end
|
53
|
-
# end
|
54
|
-
|
55
|
-
# def require_current_user2(klass = User, &block)
|
56
|
-
# current_user = set_current_user(klass)
|
57
|
-
# if block_given? && current_user.nil?
|
58
|
-
# block.call
|
59
|
-
# else
|
60
|
-
# raise MissingArgumentError
|
61
|
-
# end
|
62
|
-
# end
|
63
|
-
|
64
32
|
def require_current_user(&block)
|
65
33
|
token = cookies[Tinytokenauth.configuration.cookie_name]
|
66
|
-
# p "token from cookie: #{token}"
|
67
34
|
begin
|
68
|
-
@decoded = JsonWebToken.decode(token)
|
69
|
-
|
70
|
-
@current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
|
35
|
+
@decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
|
36
|
+
@current_user = User.find @decoded[:user_id]
|
71
37
|
@exp = @decoded[:exp]
|
72
38
|
if Tinytokenauth.configuration.token_auto_renew_hours &&
|
73
39
|
@exp < Tinytokenauth.configuration.token_auto_renew_hours.hours.from_now.to_i
|
@@ -82,27 +48,6 @@ module Tinytokenauth
|
|
82
48
|
end
|
83
49
|
end
|
84
50
|
|
85
|
-
# def set_current_user(klass = User)
|
86
|
-
# token = cookies[Authorizable.configuration.cookie_name]
|
87
|
-
# begin
|
88
|
-
# @decoded = JsonWebToken.decode(token)
|
89
|
-
# # @current_user = User.find(@decoded[:user_id])
|
90
|
-
# @current_user = klass.send 'find', @decoded[:user_id]
|
91
|
-
# @exp = @decoded[:exp]
|
92
|
-
# # if @exp < 24.hours.from_now.to_i # Always refresh token
|
93
|
-
# if @exp < 4.hours.from_now.to_i # Always refresh token
|
94
|
-
# # token = JsonWebToken.encode(user_id: @current_user.id)
|
95
|
-
# # cookies['klap-auth'] = token
|
96
|
-
# sign_in @current_user
|
97
|
-
# end
|
98
|
-
# rescue ActiveRecord::RecordNotFound
|
99
|
-
# # Ignored
|
100
|
-
# rescue JWT::DecodeError
|
101
|
-
# # Ignored
|
102
|
-
# end
|
103
|
-
# @current_user
|
104
|
-
# end
|
105
|
-
|
106
51
|
def set_current_user
|
107
52
|
begin
|
108
53
|
require_current_user
|
@@ -116,9 +61,9 @@ module Tinytokenauth
|
|
116
61
|
|
117
62
|
def sign_in(user)
|
118
63
|
@current_user = user
|
119
|
-
jwt = JsonWebToken.encode(
|
120
|
-
|
121
|
-
|
64
|
+
jwt = JsonWebToken.encode(Tinytokenauth.configuration.token_validity_hours.hours.from_now,
|
65
|
+
Tinytokenauth.configuration.token_secret,
|
66
|
+
user_id: user.id,)
|
122
67
|
cookies[Tinytokenauth.configuration.cookie_name] = jwt
|
123
68
|
end
|
124
69
|
|
@@ -2,19 +2,12 @@ require 'jwt'
|
|
2
2
|
|
3
3
|
module Tinytokenauth
|
4
4
|
class JsonWebToken
|
5
|
-
|
6
|
-
def self.encode(payload, options = {})
|
7
|
-
exp = options[:exp]
|
8
|
-
secret = options[:secret]
|
9
|
-
puts exp
|
10
|
-
puts exp.to_i
|
11
|
-
puts payload
|
5
|
+
def self.encode(exp, secret, payload)
|
12
6
|
payload[:exp] = exp.to_i
|
13
7
|
JWT.encode(payload, secret)
|
14
8
|
end
|
15
9
|
|
16
|
-
def self.decode(
|
17
|
-
secret = options[:secret]
|
10
|
+
def self.decode(secret, token)
|
18
11
|
decoded = JWT.decode(token, secret)[0]
|
19
12
|
HashWithIndifferentAccess.new decoded
|
20
13
|
end
|