tinytokenauth-rails 0.1.7 → 0.1.8
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/lib/tinytokenauth/authorizable.rb +5 -61
- 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: 16db7cdd60c7295a08eb0ca07fbcc229289cff1a0f867d5147c98fd1c10929e7
|
4
|
+
data.tar.gz: 483600dc44ab04a814b04f21ef9cf0fe24b09908327d46c6a29b12a5f480bc78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 482c5de56d33c9135b2ee74b1a7fa0bc1d5aa43386d9a6ef6dc30acfaed128ad3ef4dc6274e17f6dda82a8370f22bca64566da6d8f62efb3c91c94003695c8d8
|
7
|
+
data.tar.gz: a7e8813692d44a322b74e37b5562f5e6a9228d1a82b2ec724cef16004615b29856e385029a9d3c7ba05614b71fb0a51b9e1939825a7807aa6ac59b56b3875715
|
@@ -19,8 +19,7 @@ module Tinytokenauth
|
|
19
19
|
token = header.split(' ').last if header
|
20
20
|
|
21
21
|
begin
|
22
|
-
@decoded = JsonWebToken.decode(token)
|
23
|
-
# @current_user = User.find(@decoded[:user_id])
|
22
|
+
@decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
|
24
23
|
@current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
|
25
24
|
rescue ActiveRecord::RecordNotFound => e
|
26
25
|
render json: { errors: e.message }, status: :unauthorized
|
@@ -29,44 +28,10 @@ module Tinytokenauth
|
|
29
28
|
end
|
30
29
|
end
|
31
30
|
|
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
31
|
def require_current_user(&block)
|
65
32
|
token = cookies[Tinytokenauth.configuration.cookie_name]
|
66
|
-
# p "token from cookie: #{token}"
|
67
33
|
begin
|
68
|
-
@decoded = JsonWebToken.decode(token)
|
69
|
-
# @current_user = User.find(@decoded[:user_id])
|
34
|
+
@decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
|
70
35
|
@current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
|
71
36
|
@exp = @decoded[:exp]
|
72
37
|
if Tinytokenauth.configuration.token_auto_renew_hours &&
|
@@ -82,27 +47,6 @@ module Tinytokenauth
|
|
82
47
|
end
|
83
48
|
end
|
84
49
|
|
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
50
|
def set_current_user
|
107
51
|
begin
|
108
52
|
require_current_user
|
@@ -116,9 +60,9 @@ module Tinytokenauth
|
|
116
60
|
|
117
61
|
def sign_in(user)
|
118
62
|
@current_user = user
|
119
|
-
jwt = JsonWebToken.encode(
|
120
|
-
|
121
|
-
|
63
|
+
jwt = JsonWebToken.encode(Tinytokenauth.configuration.token_validity_hours.hours.from_now,
|
64
|
+
Tinytokenauth.configuration.token_secret,
|
65
|
+
user_id: user.id,)
|
122
66
|
cookies[Tinytokenauth.configuration.cookie_name] = jwt
|
123
67
|
end
|
124
68
|
|
@@ -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
|