tinytokenauth-rails 0.1.6 → 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/Gemfile.lock +1 -1
- data/lib/tinytokenauth/authorizable.rb +21 -75
- 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
|
data/Gemfile.lock
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
module Tinytokenauth
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
3
|
+
class << self
|
4
|
+
def configuration
|
5
|
+
@configuration ||= Configuration.new
|
6
|
+
end
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
8
|
+
def configure
|
9
|
+
yield(configuration)
|
12
10
|
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Authorizable
|
14
|
+
|
13
15
|
|
14
16
|
def authorize_with_header
|
15
17
|
token = ''
|
@@ -17,9 +19,8 @@ module Tinytokenauth
|
|
17
19
|
token = header.split(' ').last if header
|
18
20
|
|
19
21
|
begin
|
20
|
-
@decoded = JsonWebToken.decode(token)
|
21
|
-
|
22
|
-
@current_user = Authorizable.configuration.user_class.send 'find', @decoded[:user_id]
|
22
|
+
@decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
|
23
|
+
@current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
|
23
24
|
rescue ActiveRecord::RecordNotFound => e
|
24
25
|
render json: { errors: e.message }, status: :unauthorized
|
25
26
|
rescue JWT::DecodeError => e
|
@@ -27,48 +28,14 @@ module Tinytokenauth
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
# def require_current_user(klass = User)
|
31
|
-
# token = cookies['klap-auth']
|
32
|
-
# # p "token from cookie: #{token}"
|
33
|
-
#
|
34
|
-
# begin
|
35
|
-
# @decoded = JsonWebToken.decode(token)
|
36
|
-
# # @current_user = User.find(@decoded[:user_id])
|
37
|
-
# @current_user = klass.send 'find', @decoded[:user_id]
|
38
|
-
# @exp = @decoded[:exp]
|
39
|
-
# # if @exp < 24.hours.from_now.to_i # Always refresh token
|
40
|
-
# if @exp < 4.hours.from_now.to_i # Always refresh token
|
41
|
-
# sign_in @current_user
|
42
|
-
# end
|
43
|
-
# rescue ActiveRecord::RecordNotFound => e
|
44
|
-
# # TODO: evaluate if we should always forward
|
45
|
-
# redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
|
46
|
-
# rescue JWT::DecodeError => e
|
47
|
-
# # TODO: evaluate if we should always forward
|
48
|
-
# # render json: { errors: e.message }, status: :unauthorized
|
49
|
-
# redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
|
50
|
-
# end
|
51
|
-
# end
|
52
|
-
|
53
|
-
# def require_current_user2(klass = User, &block)
|
54
|
-
# current_user = set_current_user(klass)
|
55
|
-
# if block_given? && current_user.nil?
|
56
|
-
# block.call
|
57
|
-
# else
|
58
|
-
# raise MissingArgumentError
|
59
|
-
# end
|
60
|
-
# end
|
61
|
-
|
62
31
|
def require_current_user(&block)
|
63
|
-
token = cookies[
|
64
|
-
# p "token from cookie: #{token}"
|
32
|
+
token = cookies[Tinytokenauth.configuration.cookie_name]
|
65
33
|
begin
|
66
|
-
@decoded = JsonWebToken.decode(token)
|
67
|
-
|
68
|
-
@current_user = Authorizable.configuration.user_class.send 'find', @decoded[:user_id]
|
34
|
+
@decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
|
35
|
+
@current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
|
69
36
|
@exp = @decoded[:exp]
|
70
|
-
if
|
71
|
-
@exp <
|
37
|
+
if Tinytokenauth.configuration.token_auto_renew_hours &&
|
38
|
+
@exp < Tinytokenauth.configuration.token_auto_renew_hours.hours.from_now.to_i
|
72
39
|
sign_in @current_user
|
73
40
|
end
|
74
41
|
rescue ActiveRecord::RecordNotFound, JWT::DecodeError => e
|
@@ -80,27 +47,6 @@ module Tinytokenauth
|
|
80
47
|
end
|
81
48
|
end
|
82
49
|
|
83
|
-
# def set_current_user(klass = User)
|
84
|
-
# token = cookies[Authorizable.configuration.cookie_name]
|
85
|
-
# begin
|
86
|
-
# @decoded = JsonWebToken.decode(token)
|
87
|
-
# # @current_user = User.find(@decoded[:user_id])
|
88
|
-
# @current_user = klass.send 'find', @decoded[:user_id]
|
89
|
-
# @exp = @decoded[:exp]
|
90
|
-
# # if @exp < 24.hours.from_now.to_i # Always refresh token
|
91
|
-
# if @exp < 4.hours.from_now.to_i # Always refresh token
|
92
|
-
# # token = JsonWebToken.encode(user_id: @current_user.id)
|
93
|
-
# # cookies['klap-auth'] = token
|
94
|
-
# sign_in @current_user
|
95
|
-
# end
|
96
|
-
# rescue ActiveRecord::RecordNotFound
|
97
|
-
# # Ignored
|
98
|
-
# rescue JWT::DecodeError
|
99
|
-
# # Ignored
|
100
|
-
# end
|
101
|
-
# @current_user
|
102
|
-
# end
|
103
|
-
|
104
50
|
def set_current_user
|
105
51
|
begin
|
106
52
|
require_current_user
|
@@ -114,10 +60,10 @@ module Tinytokenauth
|
|
114
60
|
|
115
61
|
def sign_in(user)
|
116
62
|
@current_user = user
|
117
|
-
jwt = JsonWebToken.encode(
|
118
|
-
|
119
|
-
|
120
|
-
cookies[
|
63
|
+
jwt = JsonWebToken.encode(Tinytokenauth.configuration.token_validity_hours.hours.from_now,
|
64
|
+
Tinytokenauth.configuration.token_secret,
|
65
|
+
user_id: user.id,)
|
66
|
+
cookies[Tinytokenauth.configuration.cookie_name] = jwt
|
121
67
|
end
|
122
68
|
|
123
69
|
def current_user
|
@@ -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
|