has_jwt_token 0.2.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1bd6891ae551f486aecfa6de00f54b1318bd0383c7a19db1d590a371181f1db2
4
- data.tar.gz: f1855497a20c4f4713bf58ae2d79480973342ef86fe71d641102888cfbac41db
3
+ metadata.gz: 4a526ec8600ace28c7cd99bb4132599ea57decb0670b7cfe927c4799450cde03
4
+ data.tar.gz: '089bdb90ff41da3da8d106520cf1f13366cf87250b7da886af6820da07c96f2e'
5
5
  SHA512:
6
- metadata.gz: d0ec13c04d6175004eca4dfb51e9f19f620fa022c09c1322b883fcf6e47b49473972a0a82bf27ed8c7c583fdd7d67fb8afe3c57020ffed555edf8b7f1e0464b5
7
- data.tar.gz: ad1c861ab14616785139c30025097e4665d4d2f172914b30b617d67bc7ef758e5fc94f7b62f906df8dfa76c245256858fd4ec0cd00c19529793f6b5cffd33395
6
+ metadata.gz: c042106d4ab0ef4de50d4cf2541b198c06faa8352d0242a40f00ec9ff66465128822ef09ed648322e2c666f29f52e3115a337d260b4ac1ec938a8c3a87bd7f86
7
+ data.tar.gz: 6e391d3af3ae20c65d157f48f58f1ac84b3276235d26c10895acbbb17919d6e72e4e4cd0614d2be27c135a4e9c8a8e1f2b995270be56ee399a9435a76558d3e6
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.1
1
+ 3.1.2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- has_jwt_token (0.2.0)
4
+ has_jwt_token (0.2.3)
5
5
  bcrypt
6
6
  jwt
7
7
  rails (>= 5.0, < 7.0)
@@ -9,182 +9,184 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actioncable (6.0.3.4)
13
- actionpack (= 6.0.3.4)
12
+ actioncable (6.1.6)
13
+ actionpack (= 6.1.6)
14
+ activesupport (= 6.1.6)
14
15
  nio4r (~> 2.0)
15
16
  websocket-driver (>= 0.6.1)
16
- actionmailbox (6.0.3.4)
17
- actionpack (= 6.0.3.4)
18
- activejob (= 6.0.3.4)
19
- activerecord (= 6.0.3.4)
20
- activestorage (= 6.0.3.4)
21
- activesupport (= 6.0.3.4)
17
+ actionmailbox (6.1.6)
18
+ actionpack (= 6.1.6)
19
+ activejob (= 6.1.6)
20
+ activerecord (= 6.1.6)
21
+ activestorage (= 6.1.6)
22
+ activesupport (= 6.1.6)
22
23
  mail (>= 2.7.1)
23
- actionmailer (6.0.3.4)
24
- actionpack (= 6.0.3.4)
25
- actionview (= 6.0.3.4)
26
- activejob (= 6.0.3.4)
24
+ actionmailer (6.1.6)
25
+ actionpack (= 6.1.6)
26
+ actionview (= 6.1.6)
27
+ activejob (= 6.1.6)
28
+ activesupport (= 6.1.6)
27
29
  mail (~> 2.5, >= 2.5.4)
28
30
  rails-dom-testing (~> 2.0)
29
- actionpack (6.0.3.4)
30
- actionview (= 6.0.3.4)
31
- activesupport (= 6.0.3.4)
32
- rack (~> 2.0, >= 2.0.8)
31
+ actionpack (6.1.6)
32
+ actionview (= 6.1.6)
33
+ activesupport (= 6.1.6)
34
+ rack (~> 2.0, >= 2.0.9)
33
35
  rack-test (>= 0.6.3)
34
36
  rails-dom-testing (~> 2.0)
35
37
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
36
- actiontext (6.0.3.4)
37
- actionpack (= 6.0.3.4)
38
- activerecord (= 6.0.3.4)
39
- activestorage (= 6.0.3.4)
40
- activesupport (= 6.0.3.4)
38
+ actiontext (6.1.6)
39
+ actionpack (= 6.1.6)
40
+ activerecord (= 6.1.6)
41
+ activestorage (= 6.1.6)
42
+ activesupport (= 6.1.6)
41
43
  nokogiri (>= 1.8.5)
42
- actionview (6.0.3.4)
43
- activesupport (= 6.0.3.4)
44
+ actionview (6.1.6)
45
+ activesupport (= 6.1.6)
44
46
  builder (~> 3.1)
45
47
  erubi (~> 1.4)
46
48
  rails-dom-testing (~> 2.0)
47
49
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
- activejob (6.0.3.4)
49
- activesupport (= 6.0.3.4)
50
+ activejob (6.1.6)
51
+ activesupport (= 6.1.6)
50
52
  globalid (>= 0.3.6)
51
- activemodel (6.0.3.4)
52
- activesupport (= 6.0.3.4)
53
- activerecord (6.0.3.4)
54
- activemodel (= 6.0.3.4)
55
- activesupport (= 6.0.3.4)
56
- activestorage (6.0.3.4)
57
- actionpack (= 6.0.3.4)
58
- activejob (= 6.0.3.4)
59
- activerecord (= 6.0.3.4)
60
- marcel (~> 0.3.1)
61
- activesupport (6.0.3.4)
53
+ activemodel (6.1.6)
54
+ activesupport (= 6.1.6)
55
+ activerecord (6.1.6)
56
+ activemodel (= 6.1.6)
57
+ activesupport (= 6.1.6)
58
+ activestorage (6.1.6)
59
+ actionpack (= 6.1.6)
60
+ activejob (= 6.1.6)
61
+ activerecord (= 6.1.6)
62
+ activesupport (= 6.1.6)
63
+ marcel (~> 1.0)
64
+ mini_mime (>= 1.1.0)
65
+ activesupport (6.1.6)
62
66
  concurrent-ruby (~> 1.0, >= 1.0.2)
63
- i18n (>= 0.7, < 2)
64
- minitest (~> 5.1)
65
- tzinfo (~> 1.1)
66
- zeitwerk (~> 2.2, >= 2.2.2)
67
- ast (2.4.1)
68
- bcrypt (3.1.16)
67
+ i18n (>= 1.6, < 2)
68
+ minitest (>= 5.1)
69
+ tzinfo (~> 2.0)
70
+ zeitwerk (~> 2.3)
71
+ ast (2.4.2)
72
+ bcrypt (3.1.17)
69
73
  builder (3.2.4)
70
74
  coderay (1.1.3)
71
- concurrent-ruby (1.1.7)
75
+ concurrent-ruby (1.1.10)
72
76
  crass (1.0.6)
73
- diff-lcs (1.4.4)
74
- docile (1.3.2)
75
- erubi (1.9.0)
76
- factory_bot (6.1.0)
77
+ diff-lcs (1.5.0)
78
+ docile (1.4.0)
79
+ erubi (1.10.0)
80
+ factory_bot (6.2.1)
77
81
  activesupport (>= 5.0.0)
78
- globalid (0.4.2)
79
- activesupport (>= 4.2.0)
80
- i18n (1.8.5)
82
+ globalid (1.0.0)
83
+ activesupport (>= 5.0)
84
+ i18n (1.10.0)
81
85
  concurrent-ruby (~> 1.0)
82
- jwt (2.2.2)
83
- loofah (2.7.0)
86
+ jwt (2.3.0)
87
+ loofah (2.18.0)
84
88
  crass (~> 1.0.2)
85
89
  nokogiri (>= 1.5.9)
86
90
  mail (2.7.1)
87
91
  mini_mime (>= 0.1.1)
88
- marcel (0.3.3)
89
- mimemagic (~> 0.3.2)
92
+ marcel (1.0.2)
90
93
  method_source (1.0.0)
91
- mimemagic (0.3.5)
92
- mini_mime (1.0.2)
93
- mini_portile2 (2.4.0)
94
- minitest (5.14.2)
95
- nio4r (2.5.4)
96
- nokogiri (1.10.10)
97
- mini_portile2 (~> 2.4.0)
98
- parallel (1.19.2)
99
- parser (2.7.2.0)
94
+ mini_mime (1.1.2)
95
+ minitest (5.15.0)
96
+ nio4r (2.5.8)
97
+ nokogiri (1.13.6-x86_64-linux)
98
+ racc (~> 1.4)
99
+ parallel (1.22.1)
100
+ parser (3.1.2.0)
100
101
  ast (~> 2.4.1)
101
- pry (0.13.1)
102
+ pry (0.14.1)
102
103
  coderay (~> 1.1)
103
104
  method_source (~> 1.0)
105
+ racc (1.6.0)
104
106
  rack (2.2.3)
105
107
  rack-test (1.1.0)
106
108
  rack (>= 1.0, < 3)
107
- rails (6.0.3.4)
108
- actioncable (= 6.0.3.4)
109
- actionmailbox (= 6.0.3.4)
110
- actionmailer (= 6.0.3.4)
111
- actionpack (= 6.0.3.4)
112
- actiontext (= 6.0.3.4)
113
- actionview (= 6.0.3.4)
114
- activejob (= 6.0.3.4)
115
- activemodel (= 6.0.3.4)
116
- activerecord (= 6.0.3.4)
117
- activestorage (= 6.0.3.4)
118
- activesupport (= 6.0.3.4)
119
- bundler (>= 1.3.0)
120
- railties (= 6.0.3.4)
109
+ rails (6.1.6)
110
+ actioncable (= 6.1.6)
111
+ actionmailbox (= 6.1.6)
112
+ actionmailer (= 6.1.6)
113
+ actionpack (= 6.1.6)
114
+ actiontext (= 6.1.6)
115
+ actionview (= 6.1.6)
116
+ activejob (= 6.1.6)
117
+ activemodel (= 6.1.6)
118
+ activerecord (= 6.1.6)
119
+ activestorage (= 6.1.6)
120
+ activesupport (= 6.1.6)
121
+ bundler (>= 1.15.0)
122
+ railties (= 6.1.6)
121
123
  sprockets-rails (>= 2.0.0)
122
124
  rails-dom-testing (2.0.3)
123
125
  activesupport (>= 4.2.0)
124
126
  nokogiri (>= 1.6)
125
- rails-html-sanitizer (1.3.0)
127
+ rails-html-sanitizer (1.4.2)
126
128
  loofah (~> 2.3)
127
- railties (6.0.3.4)
128
- actionpack (= 6.0.3.4)
129
- activesupport (= 6.0.3.4)
129
+ railties (6.1.6)
130
+ actionpack (= 6.1.6)
131
+ activesupport (= 6.1.6)
130
132
  method_source
131
- rake (>= 0.8.7)
132
- thor (>= 0.20.3, < 2.0)
133
- rainbow (3.0.0)
133
+ rake (>= 12.2)
134
+ thor (~> 1.0)
135
+ rainbow (3.1.1)
134
136
  rake (12.3.3)
135
- regexp_parser (1.8.2)
136
- rexml (3.2.4)
137
- rspec (3.9.0)
138
- rspec-core (~> 3.9.0)
139
- rspec-expectations (~> 3.9.0)
140
- rspec-mocks (~> 3.9.0)
141
- rspec-core (3.9.3)
142
- rspec-support (~> 3.9.3)
143
- rspec-expectations (3.9.3)
137
+ regexp_parser (2.4.0)
138
+ rexml (3.2.5)
139
+ rspec (3.11.0)
140
+ rspec-core (~> 3.11.0)
141
+ rspec-expectations (~> 3.11.0)
142
+ rspec-mocks (~> 3.11.0)
143
+ rspec-core (3.11.0)
144
+ rspec-support (~> 3.11.0)
145
+ rspec-expectations (3.11.0)
144
146
  diff-lcs (>= 1.2.0, < 2.0)
145
- rspec-support (~> 3.9.0)
146
- rspec-mocks (3.9.1)
147
+ rspec-support (~> 3.11.0)
148
+ rspec-mocks (3.11.1)
147
149
  diff-lcs (>= 1.2.0, < 2.0)
148
- rspec-support (~> 3.9.0)
149
- rspec-support (3.9.4)
150
- rubocop (0.93.1)
150
+ rspec-support (~> 3.11.0)
151
+ rspec-support (3.11.0)
152
+ rubocop (1.29.0)
151
153
  parallel (~> 1.10)
152
- parser (>= 2.7.1.5)
154
+ parser (>= 3.1.0.0)
153
155
  rainbow (>= 2.2.2, < 4.0)
154
- regexp_parser (>= 1.8)
155
- rexml
156
- rubocop-ast (>= 0.6.0)
156
+ regexp_parser (>= 1.8, < 3.0)
157
+ rexml (>= 3.2.5, < 4.0)
158
+ rubocop-ast (>= 1.17.0, < 2.0)
157
159
  ruby-progressbar (~> 1.7)
158
- unicode-display_width (>= 1.4.0, < 2.0)
159
- rubocop-ast (1.0.1)
160
- parser (>= 2.7.1.5)
161
- rubocop-performance (1.8.1)
162
- rubocop (>= 0.87.0)
160
+ unicode-display_width (>= 1.4.0, < 3.0)
161
+ rubocop-ast (1.17.0)
162
+ parser (>= 3.1.1.0)
163
+ rubocop-performance (1.13.3)
164
+ rubocop (>= 1.7.0, < 2.0)
163
165
  rubocop-ast (>= 0.4.0)
164
- rubocop-rspec (1.44.1)
165
- rubocop (~> 0.87)
166
- rubocop-ast (>= 0.7.1)
167
- ruby-progressbar (1.10.1)
168
- simplecov (0.19.0)
166
+ rubocop-rspec (2.10.0)
167
+ rubocop (~> 1.19)
168
+ ruby-progressbar (1.11.0)
169
+ simplecov (0.21.2)
169
170
  docile (~> 1.1)
170
171
  simplecov-html (~> 0.11)
172
+ simplecov_json_formatter (~> 0.1)
171
173
  simplecov-html (0.12.3)
172
- sprockets (4.0.2)
174
+ simplecov_json_formatter (0.1.4)
175
+ sprockets (4.0.3)
173
176
  concurrent-ruby (~> 1.0)
174
177
  rack (> 1, < 3)
175
- sprockets-rails (3.2.2)
176
- actionpack (>= 4.0)
177
- activesupport (>= 4.0)
178
+ sprockets-rails (3.4.2)
179
+ actionpack (>= 5.2)
180
+ activesupport (>= 5.2)
178
181
  sprockets (>= 3.0.0)
179
- thor (1.0.1)
180
- thread_safe (0.3.6)
181
- tzinfo (1.2.7)
182
- thread_safe (~> 0.1)
183
- unicode-display_width (1.7.0)
184
- websocket-driver (0.7.3)
182
+ thor (1.2.1)
183
+ tzinfo (2.0.4)
184
+ concurrent-ruby (~> 1.0)
185
+ unicode-display_width (2.1.0)
186
+ websocket-driver (0.7.5)
185
187
  websocket-extensions (>= 0.1.0)
186
188
  websocket-extensions (0.1.5)
187
- zeitwerk (2.4.0)
189
+ zeitwerk (2.5.4)
188
190
 
189
191
  PLATFORMS
190
192
  ruby
@@ -201,4 +203,4 @@ DEPENDENCIES
201
203
  simplecov
202
204
 
203
205
  BUNDLED WITH
204
- 2.1.4
206
+ 2.2.22
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  HasJwtToken provides JWT authetication for models which are kean to use `has_secure_password` in Rails app and wants to use it to grant jwt tokens.
4
4
 
5
- This gem is build on top of [Ruby's JWT](https://github.com/jwt/ruby-jwt) gem and it implements `JWT.encode` and `JWT.decode` methods.
5
+ This gem is build on top of [Ruby's JWT](https://github.com/jwt/ruby-jwt) gem and it implements `JWT.encode` and `JWT.decode` (with validation enabled) methods.
6
6
 
7
7
  ## Installation
8
8
 
@@ -37,6 +37,9 @@ class User
37
37
  jwt.payload :custom_plain_value, 321
38
38
  jwt.secret 'secret'
39
39
 
40
+ jwt.header :header_field, :header_value
41
+ jwt.header :header_field2, -> { 123 }
42
+
40
43
  jwt.expiration_time -> { Time.now.to_i + 60 }
41
44
  jwt.not_before_time -> { Time.now.to_i }
42
45
  jwt.issued_at -> { Time.now.to_i }
@@ -50,7 +53,7 @@ end
50
53
  user = User.last
51
54
 
52
55
  user.authenicate(password) # => user with @token
53
- user.auhtenticate_with_jwt(token) # => user with @token
56
+ User.find_with_jwt(token) # => user with @token
54
57
  ```
55
58
 
56
59
  ## Roadmap
@@ -65,7 +68,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
65
68
 
66
69
  ## Contributing
67
70
 
68
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/has_jwt_token. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/has_jwt_token/blob/master/CODE_OF_CONDUCT.md).
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pucinsk/has_jwt_token. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/has_jwt_token/blob/master/CODE_OF_CONDUCT.md).
69
72
 
70
73
 
71
74
  ## License
@@ -74,4 +77,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
74
77
 
75
78
  ## Code of Conduct
76
79
 
77
- Everyone interacting in the HasJwtToken project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/has_jwt_token/blob/master/CODE_OF_CONDUCT.md).
80
+ Everyone interacting in the HasJwtToken project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pucinsk/has_jwt_token/blob/master/CODE_OF_CONDUCT.md).
@@ -13,13 +13,12 @@ Gem::Specification.new do |spec|
13
13
  'for models which are kean to use `has_secure_password`'\
14
14
  'in Rails app. It allows find resource by some identificator'\
15
15
  'and password of by JWT token itself.'
16
- spec.homepage = 'https://github.com/pucinsk'
16
+ spec.homepage = 'https://github.com/pucinsk/has_jwt_token'
17
17
  spec.license = 'MIT'
18
18
  spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
19
19
 
20
20
  spec.metadata['homepage_uri'] = spec.homepage
21
- spec.metadata['source_code_uri'] = 'https://github.com/pucinsk'
22
- spec.metadata['changelog_uri'] = 'https://github.com/pucinsk'
21
+ spec.metadata['source_code_uri'] = 'https://github.com/pucinsk/has_jwt_token'
23
22
 
24
23
  # Specify which files should be added to the gem when it is released.
25
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -1,22 +1,63 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Authenticatable
4
- def authenticate(password)
5
- super(password).tap do |authenticated|
6
- @token = authenticated && encode || nil
3
+ require 'has_jwt_token/jwt_proxy'
4
+
5
+ module HasJwtToken
6
+ module Authenticatable
7
+ module ClassMethods
8
+ def find_with_jwt(jwt_token)
9
+ payload = decode!(jwt_token)
10
+ find_by(authenticate_by => payload[authenticate_by])
11
+ &.tap { |model| model.token = model.encode }
12
+ rescue JWT::DecodeError
13
+ raise HasJwtToken::InvalidToken, 'Invalid token has been provided.'
14
+ end
15
+
16
+ private
17
+
18
+ def authenticate_by
19
+ @authenticate_by ||= begin
20
+ auth_by_attr = has_jwt_token.authenticate_by.to_s
21
+ return auth_by_attr if column_names.include?(auth_by_attr)
22
+
23
+ raise HasJwtToken::BadConfiguration, "#{auth_by_attr.inspect} must be one of model attributes."
24
+ end
25
+ end
26
+
27
+ def decode!(token)
28
+ HasJwtToken::JwtProxy.decode!(
29
+ token: token,
30
+ algorithm: has_jwt_token.algorithm,
31
+ secret: has_jwt_token.secret
32
+ )
33
+ end
7
34
  end
8
- end
9
35
 
10
- def authenticate_with_jwt(jwt_token)
11
- authenticated = decode!(jwt_token)
36
+ def self.included(base)
37
+ base.extend(ClassMethods)
38
+ end
39
+
40
+ def authenticate(password)
41
+ super(password).tap do |authenticated|
42
+ @token = authenticated && encode || nil
43
+ end
44
+ end
45
+
46
+ def encode
47
+ HasJwtToken::JwtProxy.encode(
48
+ algorithm: algorithm,
49
+ payload: model_payload.merge(claims_payload),
50
+ secret: secret,
51
+ header_fields: header_fields
52
+ )
53
+ end
54
+
55
+ private
56
+
57
+ delegate :algorithm, :secret, :model_payload, :claims_payload, :header_fields, to: :has_jwt_token
12
58
 
13
- if authenticated
14
- @token = encode
15
- self
16
- else
17
- @token = nil
59
+ def has_jwt_token
60
+ @has_jwt_token ||= self.class.has_jwt_token(self)
18
61
  end
19
- rescue JWT::DecodeError
20
- nil
21
62
  end
22
63
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasJwtToken
4
+ HasJwtTokenError = Class.new(StandardError)
5
+
6
+ class InvalidToken < HasJwtTokenError; end
7
+ class BadConfiguration < HasJwtTokenError; end
8
+ end
@@ -7,9 +7,7 @@ module HasJwtToken
7
7
  def has_jwt_token(model = nil)
8
8
  @has_jwt_token ||= JwtConfiguration.new
9
9
  yield(@has_jwt_token) if block_given?
10
- @has_jwt_token.tap do |config|
11
- config.model = model if model
12
- end
10
+ @has_jwt_token.tap { |jwt| jwt.model = model if model }
13
11
  end
14
12
  end
15
13
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module HasJwtToken
4
4
  class JwtConfiguration
5
+ DEFAULT_AUTH_ATTRIBUTE = :id
5
6
  CLAIMS = {
6
7
  expiration_time: :exp,
7
8
  not_before_time: :nbf,
@@ -57,7 +58,8 @@ module HasJwtToken
57
58
  end
58
59
  end
59
60
 
60
- def payload(name = nil, value = nil)
61
+ def payload(name = nil, value: nil, auth_by: false)
62
+ @authenticate_by = name if auth_by
61
63
  @payload[name] = value || ->(model) { model.respond_to?(name) && model.public_send(name) } if name
62
64
  end
63
65
 
@@ -77,5 +79,9 @@ module HasJwtToken
77
79
  val.is_a?(Proc) ? val.call : val
78
80
  end
79
81
  end
82
+
83
+ def authenticate_by
84
+ @authenticate_by || DEFAULT_AUTH_ATTRIBUTE
85
+ end
80
86
  end
81
87
  end
@@ -4,31 +4,14 @@ require 'jwt'
4
4
 
5
5
  module HasJwtToken
6
6
  class JwtProxy
7
- attr_reader :algorithm, :payload, :secret, :header_fields
7
+ class << self
8
+ def encode(payload:, secret: '', algorithm: '', header_fields: {})
9
+ JWT.encode(payload, secret, algorithm, header_fields)
10
+ end
8
11
 
9
- def initialize(algorithm: '', payload: {}, secret: '', header_fields: {})
10
- @algorithm = algorithm
11
- @payload = payload
12
- @secret = secret
13
- @header_fields = header_fields
14
- end
15
-
16
- def encode
17
- JWT.encode(payload, secret, algorithm, header_fields)
18
- end
19
-
20
- def decode(token)
21
- JWT.decode(token, secret, false, algorithm: algorithm)[0]
22
- end
23
-
24
- def decode!(token)
25
- JWT.decode(token, secret, true, algorithm: algorithm)[0]
26
- end
27
-
28
- def valid?(token)
29
- decode!(token) && true
30
- rescue JWT::DecodeError
31
- false
12
+ def decode!(token:, secret: '', algorithm: '')
13
+ JWT.decode(token, secret, true, algorithm: algorithm)[0]
14
+ end
32
15
  end
33
16
  end
34
17
  end
@@ -4,18 +4,16 @@ require 'active_model'
4
4
  require 'bcrypt'
5
5
  require 'has_jwt_token/authenticatable'
6
6
  require 'has_jwt_token/has_jwt_model_configuration'
7
- require 'has_jwt_token/jwt_tokenable'
8
7
 
9
8
  module HasJwtToken
10
9
  module Model
11
- attr_reader :token
10
+ attr_accessor :token
12
11
 
13
12
  def self.included(base)
14
13
  base.include(ActiveModel::SecurePassword)
15
14
  base.has_secure_password
16
15
  base.include(Authenticatable)
17
16
  base.extend(HasJwtModelConfiguration)
18
- base.include(JwtTokenable)
19
17
  end
20
18
  end
21
19
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HasJwtToken
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.3'
5
5
  end
data/lib/has_jwt_token.rb CHANGED
@@ -2,4 +2,5 @@
2
2
 
3
3
  require 'active_support/core_ext/module/delegation'
4
4
  require 'has_jwt_token/version'
5
+ require 'has_jwt_token/errors'
5
6
  require 'has_jwt_token/model'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_jwt_token
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jokūbas Pučinskas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
11
+ date: 2022-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt
@@ -167,19 +167,18 @@ files:
167
167
  - has_jwt_token.gemspec
168
168
  - lib/has_jwt_token.rb
169
169
  - lib/has_jwt_token/authenticatable.rb
170
+ - lib/has_jwt_token/errors.rb
170
171
  - lib/has_jwt_token/has_jwt_model_configuration.rb
171
172
  - lib/has_jwt_token/jwt_configuration.rb
172
173
  - lib/has_jwt_token/jwt_proxy.rb
173
- - lib/has_jwt_token/jwt_tokenable.rb
174
174
  - lib/has_jwt_token/model.rb
175
175
  - lib/has_jwt_token/version.rb
176
- homepage: https://github.com/pucinsk
176
+ homepage: https://github.com/pucinsk/has_jwt_token
177
177
  licenses:
178
178
  - MIT
179
179
  metadata:
180
- homepage_uri: https://github.com/pucinsk
181
- source_code_uri: https://github.com/pucinsk
182
- changelog_uri: https://github.com/pucinsk
180
+ homepage_uri: https://github.com/pucinsk/has_jwt_token
181
+ source_code_uri: https://github.com/pucinsk/has_jwt_token
183
182
  post_install_message:
184
183
  rdoc_options: []
185
184
  require_paths:
@@ -195,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
194
  - !ruby/object:Gem::Version
196
195
  version: '0'
197
196
  requirements: []
198
- rubygems_version: 3.1.2
197
+ rubygems_version: 3.3.7
199
198
  signing_key:
200
199
  specification_version: 4
201
200
  summary: Fast running JWT implentation for Rails apps!
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'has_jwt_token/jwt_proxy'
4
-
5
- module HasJwtToken
6
- module JwtTokenable
7
- delegate :algorithm, :secret, :claims_payload, :header_fields, to: :has_jwt_token
8
-
9
- def encode
10
- with_jwt_configuration(&:encode)
11
- end
12
-
13
- def decode(token)
14
- with_jwt_configuration { |jwt| jwt.decode(token) }
15
- end
16
-
17
- def decode!(token)
18
- with_jwt_configuration { |jwt| jwt.decode!(token) }
19
- end
20
-
21
- def has_jwt_token
22
- self.class.has_jwt_token(self)
23
- end
24
-
25
- private
26
-
27
- def payload
28
- @payload ||= has_jwt_token.model_payload.merge(claims_payload)
29
- end
30
-
31
- def with_jwt_configuration
32
- yield(jwt_proxy)
33
- end
34
-
35
- def jwt_proxy
36
- @jwt_proxy ||= JwtProxy.new(
37
- algorithm: algorithm,
38
- payload: payload,
39
- secret: secret,
40
- header_fields: header_fields
41
- )
42
- end
43
- end
44
- end