has_jwt_token 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3889be0fdd07a34e02b65d48948ab746f26482698767ac83ba8c680bd13272cf
4
+ data.tar.gz: d528aa140d822a2c73c3263d8e7d950cff7e84e6203bb2b48a22f3be654d07f5
5
+ SHA512:
6
+ metadata.gz: c8e1d855921667fe5b374c0b74666ea4fbbe66471f7d59ddef9b40f82a7bd7f1f11d9c01f403ce72e10389c8217a1752044688b71e926984745bb7338444f7a4
7
+ data.tar.gz: 6f0f3801e6cceac529f238dc316a4876791e13a0e2b1c7a6d29bae1fc33a95a19336eaa94e3f90d4044ceaa586c03c53b44531787986fac2ea86a17687ae213d
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /.vscode/
14
+ .ruby-gemset
15
+ coverage
16
+
17
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,24 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 2.7
8
+
9
+ Metrics/BlockLength:
10
+ Enabled: true
11
+ Exclude:
12
+ - has_jwt_token.gemspec
13
+ - spec/**/*.rb
14
+
15
+ Naming/PredicateName:
16
+ Enabled: true
17
+ AllowedMethods:
18
+ - has_jwt_token
19
+
20
+ RSpec/NestedGroups:
21
+ Enabled: false
22
+
23
+ Style/Documentation:
24
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.7.1
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at j.pucinskas@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in has_jwt_token.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,204 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_jwt_token (0.1.0)
5
+ bcrypt
6
+ jwt
7
+ rails (>= 5.0, < 7.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (6.0.3.4)
13
+ actionpack (= 6.0.3.4)
14
+ nio4r (~> 2.0)
15
+ 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)
22
+ 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)
27
+ mail (~> 2.5, >= 2.5.4)
28
+ 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)
33
+ rack-test (>= 0.6.3)
34
+ rails-dom-testing (~> 2.0)
35
+ 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)
41
+ nokogiri (>= 1.8.5)
42
+ actionview (6.0.3.4)
43
+ activesupport (= 6.0.3.4)
44
+ builder (~> 3.1)
45
+ erubi (~> 1.4)
46
+ rails-dom-testing (~> 2.0)
47
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
+ activejob (6.0.3.4)
49
+ activesupport (= 6.0.3.4)
50
+ 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)
62
+ 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)
69
+ builder (3.2.4)
70
+ coderay (1.1.3)
71
+ concurrent-ruby (1.1.7)
72
+ 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
+ activesupport (>= 5.0.0)
78
+ globalid (0.4.2)
79
+ activesupport (>= 4.2.0)
80
+ i18n (1.8.5)
81
+ concurrent-ruby (~> 1.0)
82
+ jwt (2.2.2)
83
+ loofah (2.7.0)
84
+ crass (~> 1.0.2)
85
+ nokogiri (>= 1.5.9)
86
+ mail (2.7.1)
87
+ mini_mime (>= 0.1.1)
88
+ marcel (0.3.3)
89
+ mimemagic (~> 0.3.2)
90
+ 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)
100
+ ast (~> 2.4.1)
101
+ pry (0.13.1)
102
+ coderay (~> 1.1)
103
+ method_source (~> 1.0)
104
+ rack (2.2.3)
105
+ rack-test (1.1.0)
106
+ 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)
121
+ sprockets-rails (>= 2.0.0)
122
+ rails-dom-testing (2.0.3)
123
+ activesupport (>= 4.2.0)
124
+ nokogiri (>= 1.6)
125
+ rails-html-sanitizer (1.3.0)
126
+ loofah (~> 2.3)
127
+ railties (6.0.3.4)
128
+ actionpack (= 6.0.3.4)
129
+ activesupport (= 6.0.3.4)
130
+ method_source
131
+ rake (>= 0.8.7)
132
+ thor (>= 0.20.3, < 2.0)
133
+ rainbow (3.0.0)
134
+ 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)
144
+ diff-lcs (>= 1.2.0, < 2.0)
145
+ rspec-support (~> 3.9.0)
146
+ rspec-mocks (3.9.1)
147
+ diff-lcs (>= 1.2.0, < 2.0)
148
+ rspec-support (~> 3.9.0)
149
+ rspec-support (3.9.4)
150
+ rubocop (0.93.1)
151
+ parallel (~> 1.10)
152
+ parser (>= 2.7.1.5)
153
+ rainbow (>= 2.2.2, < 4.0)
154
+ regexp_parser (>= 1.8)
155
+ rexml
156
+ rubocop-ast (>= 0.6.0)
157
+ 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)
163
+ 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)
169
+ docile (~> 1.1)
170
+ simplecov-html (~> 0.11)
171
+ simplecov-html (0.12.3)
172
+ sprockets (4.0.2)
173
+ concurrent-ruby (~> 1.0)
174
+ rack (> 1, < 3)
175
+ sprockets-rails (3.2.2)
176
+ actionpack (>= 4.0)
177
+ activesupport (>= 4.0)
178
+ 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)
185
+ websocket-extensions (>= 0.1.0)
186
+ websocket-extensions (0.1.5)
187
+ zeitwerk (2.4.0)
188
+
189
+ PLATFORMS
190
+ ruby
191
+
192
+ DEPENDENCIES
193
+ factory_bot
194
+ has_jwt_token!
195
+ pry
196
+ rake (~> 12.0)
197
+ rspec (~> 3.0)
198
+ rubocop
199
+ rubocop-performance
200
+ rubocop-rspec
201
+ simplecov
202
+
203
+ BUNDLED WITH
204
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jokūbas Pučinskas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # HasJwtToken
2
+
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
+
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.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'has_jwt_token'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install has_jwt_token
22
+
23
+ ## Usage
24
+ To get started add `HasJwtToken::Model` into your model and configure gem using `has_jwt_token`.
25
+
26
+ ```ruby
27
+ class User
28
+ include HasJwtToken::Model
29
+
30
+ attr_accessor :name, :password_digest
31
+
32
+ has_jwt_token do |jwt|
33
+ jwt.algorithm 'HS256'
34
+ jwt.payload_attribute :name
35
+ jwt.secret 'secret'
36
+
37
+ jwt.expiration_time -> { Time.now.to_i + 60 }
38
+ jwt.not_before_time -> { Time.now.to_i }
39
+ jwt.issued_at -> { Time.now.to_i }
40
+ jwt.jwt_id -> { SecureRandom.hex }
41
+ jwt.issuer :dummy_app
42
+ jwt.audience :client_app
43
+ jwt.subject :dummy_app
44
+ end
45
+ end
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ 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).
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ 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).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'has_jwt_token'
6
+
7
+ require_relative '../spec/fixtures/dummy_user' if ENV['GEM_ENV'] == 'test'
8
+
9
+ require 'pry'
10
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/has_jwt_token/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'has_jwt_token'
7
+ spec.version = HasJwtToken::VERSION
8
+ spec.authors = ['Jokūbas Pučinskas']
9
+ spec.email = ['j.pucinskas@gmail.com']
10
+
11
+ spec.summary = 'Fast running JWT implentation for Rails apps!'
12
+ spec.description = 'HasJwtToken provides JWT authetication'\
13
+ 'for models which are kean to use `has_secure_password`'\
14
+ 'in Rails app. It allows find resource by some identificator'\
15
+ 'and password of by JWT token itself.'
16
+ spec.homepage = 'https://github.com/pucinsk'
17
+ spec.license = 'MIT'
18
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
19
+
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'
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_dependency 'bcrypt'
34
+ spec.add_dependency 'jwt'
35
+ spec.add_dependency 'rails', '>= 5.0', '< 7.0'
36
+
37
+ spec.add_development_dependency 'factory_bot'
38
+ spec.add_development_dependency 'pry'
39
+ spec.add_development_dependency 'rubocop'
40
+ spec.add_development_dependency 'rubocop-performance'
41
+ spec.add_development_dependency 'rubocop-rspec'
42
+ spec.add_development_dependency 'simplecov'
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/delegation'
4
+ require 'has_jwt_token/version'
5
+ require 'has_jwt_token/model'
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authenticatable
4
+ def authenticate(password)
5
+ super(password).tap do |authenticated|
6
+ @token = authenticated && encode || nil
7
+ end
8
+ end
9
+
10
+ def authenticate_with_jwt(jwt_token)
11
+ authenticated = decode!(jwt_token)
12
+
13
+ if authenticated
14
+ @token = encode
15
+ self
16
+ else
17
+ @token = nil
18
+ end
19
+ rescue JWT::DecodeError
20
+ nil
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'has_jwt_token/jwt_configuration'
4
+
5
+ module HasJwtToken
6
+ module HasJwtModelConfiguration
7
+ def has_jwt_token
8
+ @has_jwt_token ||= JwtConfiguration.new(self)
9
+ yield(@has_jwt_token) if block_given?
10
+ @has_jwt_token
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasJwtToken
4
+ class JwtConfiguration
5
+ CLAIMS = {
6
+ expiration_time: :exp,
7
+ not_before_time: :nbf,
8
+ issuer: :iss,
9
+ audience: :aud,
10
+ jwt_id: :jti,
11
+ issued_at: :iat,
12
+ subject: :sub
13
+ }.freeze
14
+
15
+ attr_accessor :defined_claims
16
+ attr_reader :model
17
+ attr_writer :payload_attribute
18
+
19
+ def initialize(model)
20
+ @model = model
21
+ @payload_attribute = []
22
+ @defined_claims = []
23
+ end
24
+
25
+ def algorithm(value = nil)
26
+ return @algorithm unless value
27
+
28
+ @algorithm = value
29
+ end
30
+
31
+ def payload_attribute(value = nil)
32
+ return @payload_attribute unless value
33
+
34
+ payload_attribute << value
35
+ end
36
+
37
+ CLAIMS.each do |claim_name, _|
38
+ define_method(claim_name) do |value = nil|
39
+ if value
40
+ defined_claims << claim_name unless defined_claims.include?(claim_name)
41
+ instance_variable_set("@#{claim_name}".to_sym, value)
42
+ return value
43
+ end
44
+
45
+ claim_value = instance_variable_get("@#{claim_name}".to_sym)
46
+ claim_value.is_a?(Proc) ? claim_value.call : claim_value
47
+ end
48
+ end
49
+
50
+ def secret(value = nil)
51
+ return @secret unless value
52
+
53
+ @secret = value
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+
5
+ module HasJwtToken
6
+ class JwtProxy
7
+ attr_reader :algorithm, :payload, :secret
8
+
9
+ def initialize(algorithm: nil, payload: nil, secret: nil)
10
+ @algorithm = algorithm
11
+ @payload = payload
12
+ @secret = secret
13
+ end
14
+
15
+ def encode
16
+ JWT.encode(payload, secret, algorithm)
17
+ end
18
+
19
+ def decode(token)
20
+ JWT.decode(token, secret, false, algorithm: algorithm)[0]
21
+ end
22
+
23
+ def decode!(token)
24
+ JWT.decode(token, secret, true, algorithm: algorithm)[0]
25
+ end
26
+
27
+ def valid?(token)
28
+ decode!(token) && true
29
+ rescue JWT::DecodeError
30
+ false
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'has_jwt_token/jwt_proxy'
4
+
5
+ module HasJwtToken
6
+ module JwtTokenable
7
+ delegate :algorithm, :secret, :payload_attribute,
8
+ :defined_claims, to: :has_jwt_token
9
+
10
+ def encode
11
+ with_jwt_configuration(&:encode)
12
+ end
13
+
14
+ def decode(token)
15
+ with_jwt_configuration { |jwt| jwt.decode(token) }
16
+ end
17
+
18
+ def decode!(token)
19
+ with_jwt_configuration { |jwt| jwt.decode!(token) }
20
+ end
21
+
22
+ private
23
+
24
+ def has_jwt_token
25
+ self.class.has_jwt_token
26
+ end
27
+
28
+ def payload
29
+ @payload ||= model_payload.merge(claims_payload)
30
+ end
31
+
32
+ def model_payload
33
+ payload_attribute.each_with_object({}) do |attribute, memo|
34
+ memo[attribute] = public_send(attribute)
35
+ end
36
+ end
37
+
38
+ def claims_payload
39
+ defined_claims.each_with_object({}) do |claim_name, memo|
40
+ claim_key = HasJwtToken::JwtConfiguration::CLAIMS[claim_name]
41
+ memo[claim_key] = has_jwt_token.public_send(claim_name)
42
+ end
43
+ end
44
+
45
+ def with_jwt_configuration
46
+ yield(jwt_proxy)
47
+ end
48
+
49
+ def jwt_proxy
50
+ @jwt_proxy ||= JwtProxy.new(
51
+ algorithm: algorithm,
52
+ payload: payload,
53
+ secret: secret
54
+ )
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+ require 'bcrypt'
5
+ require 'has_jwt_token/authenticatable'
6
+ require 'has_jwt_token/has_jwt_model_configuration'
7
+ require 'has_jwt_token/jwt_tokenable'
8
+
9
+ module HasJwtToken
10
+ module Model
11
+ attr_reader :token
12
+
13
+ def self.included(base)
14
+ base.include(ActiveModel::SecurePassword)
15
+ base.has_secure_password
16
+ base.include(Authenticatable)
17
+ base.extend(HasJwtModelConfiguration)
18
+ base.include(JwtTokenable)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasJwtToken
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_jwt_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jokūbas Pučinskas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bcrypt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '7.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '5.0'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '7.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: factory_bot
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-performance
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rubocop-rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: simplecov
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ description: HasJwtToken provides JWT autheticationfor models which are kean to use
146
+ `has_secure_password`in Rails app. It allows find resource by some identificatorand
147
+ password of by JWT token itself.
148
+ email:
149
+ - j.pucinskas@gmail.com
150
+ executables: []
151
+ extensions: []
152
+ extra_rdoc_files: []
153
+ files:
154
+ - ".gitignore"
155
+ - ".rspec"
156
+ - ".rubocop.yml"
157
+ - ".ruby-version"
158
+ - ".travis.yml"
159
+ - CODE_OF_CONDUCT.md
160
+ - Gemfile
161
+ - Gemfile.lock
162
+ - LICENSE.txt
163
+ - README.md
164
+ - Rakefile
165
+ - bin/console
166
+ - bin/setup
167
+ - has_jwt_token.gemspec
168
+ - lib/has_jwt_token.rb
169
+ - lib/has_jwt_token/authenticatable.rb
170
+ - lib/has_jwt_token/has_jwt_model_configuration.rb
171
+ - lib/has_jwt_token/jwt_configuration.rb
172
+ - lib/has_jwt_token/jwt_proxy.rb
173
+ - lib/has_jwt_token/jwt_tokenable.rb
174
+ - lib/has_jwt_token/model.rb
175
+ - lib/has_jwt_token/version.rb
176
+ homepage: https://github.com/pucinsk
177
+ licenses:
178
+ - MIT
179
+ metadata:
180
+ homepage_uri: https://github.com/pucinsk
181
+ source_code_uri: https://github.com/pucinsk
182
+ changelog_uri: https://github.com/pucinsk
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: 2.7.0
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubygems_version: 3.1.2
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: Fast running JWT implentation for Rails apps!
202
+ test_files: []