tinytokenauth-rails 0.1.4 → 0.1.5
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 -3
- data/lib/tinytokenauth/authorizable.rb +54 -60
- data/lib/tinytokenauth/configuration.rb +9 -1
- data/lib/tinytokenauth/version.rb +1 -1
- data/lib/tinytokenauth.rb +1 -0
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82d81a75b0bbc6caea554a6bfc8ada102b75d61eb033d69566ab30069a50f01a
|
4
|
+
data.tar.gz: 06c397f8ce6c8d65b4eb1b36096d021f18c8f78f8b7ffa344e497de1924287af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e157d5631eeafcaa5f5155879cfe4c6ea6873b173f17fdd225b07b9d57c3a602824722976f523577da36e875652bbf5d966f72f0034c32c9cdaf3a74fb4b4f7
|
7
|
+
data.tar.gz: bf36b14bdb08c99bbbb6d159fb4a4825f13d5060cc0cb15a47be1cda8fe7f1fc437906b87480590935e84441c2f73e70bec0b9a92f8b55ce0604a43e6dff0890
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
tinytokenauth-rails (0.1.
|
5
|
-
bcrypt (~> 3.1)
|
4
|
+
tinytokenauth-rails (0.1.4)
|
6
5
|
jwt (~> 2.7)
|
7
6
|
rails (>= 6.0)
|
8
7
|
|
@@ -76,7 +75,6 @@ GEM
|
|
76
75
|
tzinfo (~> 2.0)
|
77
76
|
ast (2.4.2)
|
78
77
|
base64 (0.1.1)
|
79
|
-
bcrypt (3.1.19)
|
80
78
|
builder (3.2.4)
|
81
79
|
concurrent-ruby (1.2.2)
|
82
80
|
crass (1.0.6)
|
@@ -1,10 +1,6 @@
|
|
1
1
|
module Tinytokenauth
|
2
2
|
|
3
3
|
module Authorizable
|
4
|
-
# class Configuration
|
5
|
-
# attr_accessor :token_duration
|
6
|
-
# end
|
7
|
-
|
8
4
|
class << self
|
9
5
|
def configuration
|
10
6
|
@configuration ||= Configuration.new
|
@@ -15,16 +11,15 @@ module Tinytokenauth
|
|
15
11
|
end
|
16
12
|
end
|
17
13
|
|
18
|
-
def authorize_with_header
|
14
|
+
def authorize_with_header
|
19
15
|
token = ''
|
20
16
|
header = request.headers['Authorization']
|
21
17
|
token = header.split(' ').last if header
|
22
|
-
# p "token from header: #{token}"
|
23
18
|
|
24
19
|
begin
|
25
20
|
@decoded = JsonWebToken.decode(token)
|
26
21
|
# @current_user = User.find(@decoded[:user_id])
|
27
|
-
@current_user =
|
22
|
+
@current_user = Authorizable.configuration.user_class.send 'find', @decoded[:user_id]
|
28
23
|
rescue ActiveRecord::RecordNotFound => e
|
29
24
|
render json: { errors: e.message }, status: :unauthorized
|
30
25
|
rescue JWT::DecodeError => e
|
@@ -32,33 +27,29 @@ module Tinytokenauth
|
|
32
27
|
end
|
33
28
|
end
|
34
29
|
|
35
|
-
#
|
36
|
-
#
|
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
|
37
51
|
# end
|
38
52
|
|
39
|
-
def require_current_user(klass = User)
|
40
|
-
token = cookies['klap-auth']
|
41
|
-
# p "token from cookie: #{token}"
|
42
|
-
|
43
|
-
begin
|
44
|
-
@decoded = JsonWebToken.decode(token)
|
45
|
-
# @current_user = User.find(@decoded[:user_id])
|
46
|
-
@current_user = klass.send 'find', @decoded[:user_id]
|
47
|
-
@exp = @decoded[:exp]
|
48
|
-
# if @exp < 24.hours.from_now.to_i # Always refresh token
|
49
|
-
if @exp < 4.hours.from_now.to_i # Always refresh token
|
50
|
-
sign_in @current_user
|
51
|
-
end
|
52
|
-
rescue ActiveRecord::RecordNotFound => e
|
53
|
-
# TODO: evaluate if we should always forward
|
54
|
-
redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
|
55
|
-
rescue JWT::DecodeError => e
|
56
|
-
# TODO: evaluate if we should always forward
|
57
|
-
# render json: { errors: e.message }, status: :unauthorized
|
58
|
-
redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
53
|
# def require_current_user2(klass = User, &block)
|
63
54
|
# current_user = set_current_user(klass)
|
64
55
|
# if block_given? && current_user.nil?
|
@@ -68,16 +59,16 @@ module Tinytokenauth
|
|
68
59
|
# end
|
69
60
|
# end
|
70
61
|
|
71
|
-
def
|
72
|
-
token = cookies[
|
62
|
+
def require_current_user(&block)
|
63
|
+
token = cookies[Authorizable.configuration.cookie_name]
|
73
64
|
# p "token from cookie: #{token}"
|
74
65
|
begin
|
75
66
|
@decoded = JsonWebToken.decode(token)
|
76
67
|
# @current_user = User.find(@decoded[:user_id])
|
77
|
-
@current_user =
|
68
|
+
@current_user = Authorizable.configuration.user_class.send 'find', @decoded[:user_id]
|
78
69
|
@exp = @decoded[:exp]
|
79
|
-
|
80
|
-
|
70
|
+
if Authorizable.configuration.token_auto_renew_hours &&
|
71
|
+
@exp < Authorizable.configuration.token_auto_renew_hours.hours.from_now.to_i
|
81
72
|
sign_in @current_user
|
82
73
|
end
|
83
74
|
rescue ActiveRecord::RecordNotFound, JWT::DecodeError => e
|
@@ -89,30 +80,30 @@ module Tinytokenauth
|
|
89
80
|
end
|
90
81
|
end
|
91
82
|
|
92
|
-
def set_current_user(klass = User)
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
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
|
112
103
|
|
113
|
-
def
|
104
|
+
def set_current_user
|
114
105
|
begin
|
115
|
-
|
106
|
+
require_current_user
|
116
107
|
rescue ActiveRecord::RecordNotFound
|
117
108
|
# Ignored
|
118
109
|
rescue JWT::DecodeError
|
@@ -123,7 +114,10 @@ module Tinytokenauth
|
|
123
114
|
|
124
115
|
def sign_in(user)
|
125
116
|
@current_user = user
|
126
|
-
|
117
|
+
jwt = JsonWebToken.encode(user_id: user.id,
|
118
|
+
exp: Authorizable.configuration.token_validity_hours.hours.from_now,
|
119
|
+
secret: Authorizable.configuration.token_secret)
|
120
|
+
cookies[Authorizable.configuration.cookie_name] = jwt
|
127
121
|
end
|
128
122
|
|
129
123
|
def current_user
|
@@ -1,6 +1,14 @@
|
|
1
1
|
module Tinytokenauth
|
2
2
|
|
3
3
|
class Configuration
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :user_class, :token_validity_hours, :token_auto_renew_hours, :token_secret, :cookie_name
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@user_class = User
|
8
|
+
@token_validity_hours = 24
|
9
|
+
@token_auto_renew_hours = 4
|
10
|
+
@token_secret = Rails.application.credentials.secret_key_base
|
11
|
+
@cookie_name = 'ttauth'
|
12
|
+
end
|
5
13
|
end
|
6
14
|
end
|
data/lib/tinytokenauth.rb
CHANGED
@@ -4,6 +4,7 @@ module Tinytokenauth
|
|
4
4
|
require_relative "tinytokenauth/version"
|
5
5
|
require_relative "tinytokenauth/railtie"
|
6
6
|
require_relative 'tinytokenauth/configuration'
|
7
|
+
require_relative 'tinytokenauth/json_web_token'
|
7
8
|
require_relative "tinytokenauth/authorizable"
|
8
9
|
require_relative "tinytokenauth/view_helpers"
|
9
10
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tinytokenauth-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kim Laplume
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.7'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: bcrypt
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '3.1'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.1'
|
55
41
|
description: Soon
|
56
42
|
email:
|
57
43
|
- kim.laplume@protonmail.com
|