devise-jwt 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
+ SHA1:
3
+ metadata.gz: 5059317e23ed849c9d62cea8954a293b30ee4d76
4
+ data.tar.gz: 56dad0b86b27e5ded58b046e45e3cbed7bc236ee
5
+ SHA512:
6
+ metadata.gz: 5c339448845a43f83a50e70e3737c923c7d202abdcf94c37a37fb12cbdd5ad1477624b200340dfbcf8c9febc6deb5f91ca9aeae23b3970740377e0d538666453
7
+ data.tar.gz: 1eb58364cad0088955fcc2582c79778afc7b9d397b6c1c0366eb84a666f49fa9b1276ac49fe8dc1fd785d170fe2c1ec42b702caa1b32e76376696a717bd1c16a
@@ -0,0 +1,17 @@
1
+ engines:
2
+ duplication:
3
+ enabled: true
4
+ config:
5
+ languages:
6
+ - ruby
7
+ fixme:
8
+ enabled: true
9
+ rubocop:
10
+ enabled: true
11
+ reek:
12
+ enabled: true
13
+ ratings:
14
+ paths:
15
+ - "**.rb"
16
+ exclude_paths:
17
+ - spec/
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .overcommit_gems.rb.lock
11
+ *.log
12
+ *sqlite3-journal
@@ -0,0 +1,55 @@
1
+ #
2
+ # Select version of overcommit and the other tools from Gemfile
3
+ #
4
+ gemfile: .overcommit_gems.rb
5
+
6
+ #
7
+ # Hooks that are run against every commit message after a user has written it.
8
+ #
9
+ CommitMsg:
10
+ ALL:
11
+ required: true
12
+ exclude: &default_excludes
13
+ - Gemfile
14
+ - devise-jwt.gemspec
15
+ - spec/fixtures/rails_app/**/*
16
+ - README.md
17
+
18
+ HardTabs:
19
+ enabled: true
20
+
21
+ SingleLineSubject:
22
+ enabled: true
23
+
24
+ #
25
+ # Hooks that are run after `git commit` is executed, before the commit message
26
+ # editor is displayed.
27
+ #
28
+ PreCommit:
29
+ ALL:
30
+ required: true
31
+ exclude: *default_excludes
32
+
33
+ BundleAudit:
34
+ enabled: true
35
+
36
+ BundleCheck:
37
+ enabled: true
38
+
39
+ LocalPathsInGemfile:
40
+ enabled: true
41
+
42
+ ExecutePermissions:
43
+ enabled: true
44
+ exclude:
45
+ - *default_excludes
46
+ - bin/*
47
+
48
+ Reek:
49
+ enabled: true
50
+
51
+ RuboCop:
52
+ enabled: true
53
+
54
+ TrailingWhitespace:
55
+ enabled: true
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'overcommit', '~> 0.36'
6
+
7
+ # Patch-level verification for Bundled apps
8
+ gem 'bundler-audit', '~> 0.5'
9
+
10
+ # Ruby code smell reporter
11
+ gem 'reek', '~> 4.5'
12
+
13
+ # Ruby code style checking
14
+ gem 'rubocop', '~> 0.47'
15
+ gem 'rubocop-rspec', '~> 1.10'
data/.reek ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,10 @@
1
+ require: rubocop-rspec
2
+ AllCops:
3
+ TargetRubyVersion: 2.3
4
+ RSpec/NestedGroups:
5
+ Max: 3
6
+ RSpec/MessageExpectation:
7
+ EnforcedStyle: 'expect'
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - "spec/**/*.rb"
@@ -0,0 +1,21 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.6
5
+ - 2.3.3
6
+ - 2.4.0
7
+ before_install:
8
+ - gem update --system --no-doc
9
+ - bundle install --gemfile=.overcommit_gems.rb
10
+ before_script:
11
+ - git config --global user.email 'travis@travis.ci'
12
+ - git config --global user.name 'Travis CI'
13
+ script:
14
+ - bundle exec rspec
15
+ - bundle exec codeclimate-test-reporter
16
+ - overcommit --sign
17
+ - overcommit --run
18
+ addons:
19
+ code_climate:
20
+ repo_token:
21
+ secure: OWVschEUE+pVEQFR9dgtCUVmf2GJF3e7RTvX9M3CS7fhLyWqDlHdkCxmxCGJQDBVXR59ReCni2sbbRgkfqekkggLEG3gvHl2uof9+PVVRXTbDDDydwDGHLvcT8ZVJOHVgqgX899j6eBSsqdtSB7rMEdMOYBYDw35/yd844LkaW+sOWUbx0SJSo0V0QtJe3DC7vTAo/EEGTQs7WRGcXa6Pg9RlWiK7ThVoEbXQ4GDdjS5mWsQer4QcdnOfiuBL8mC3XAX+wJyBIqAVetAmhFKtwH+pKYMqRbcI/iBSaHslGowLr+tQW/tLO9XZsz95C6037XUse6BUJ5U9mYsgcnebslVJnuQ4dyXTXwQ2e1Fzy4iYr9Iz2Yhr3EjchXOvXh0D2BDPLCZMF98W/hsPZ4hPq/FhfjdfJXHcuqU6k7Ozr6UTwMmuNMvR7af2hXGaralTPOH8SVh/RtxpWsAocNA4n1b7dhdpMLKDNZ7GxdunDo5zJ03HUH4d0SDexnx3xSpfR/q+FJDHNxfD7KVAsgQYnrNjde/DnYszV2E3EAUVWF71ZaNQIvDyS1gyBjjvkSGRGL5tY7z+sdaRfE68toidgl2eR08vt/eYo3DIIzwPvI5N/BTv/Xm8vQ+jfwxCF6Ezr1TJTZW4auoMBAVqrr4HoO6T7VHTOeUD0Bukp7KBfQ=
@@ -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 marc@lamarciana.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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,9 @@
1
+ FROM ruby:2.3.1
2
+ ENV APP_HOME /app/
3
+ ENV LIB_DIR lib/devise/jwt/
4
+ RUN apt-get update -qq && apt-get install -y build-essential libpq-dev libxml2-dev libxslt1-dev nodejs
5
+ RUN mkdir -p $APP_HOME/$LIB_DIR
6
+ WORKDIR $APP_HOME
7
+ COPY Gemfile *gemspec $APP_HOME
8
+ COPY $LIB_DIR/version.rb $APP_HOME/$LIB_DIR
9
+ RUN bundle install
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in devise-jwt.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Marc Busqué
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,283 @@
1
+ # Devise::JWT
2
+
3
+ [![Build Status](https://travis-ci.org/waiting-for-dev/devise-jwt.svg?branch=master)](https://travis-ci.org/waiting-for-dev/devise-jwt)
4
+ [![Code Climate](https://codeclimate.com/github/waiting-for-dev/devise-jwt/badges/gpa.svg)](https://codeclimate.com/github/waiting-for-dev/devise-jwt)
5
+ [![Test Coverage](https://codeclimate.com/github/waiting-for-dev/devise-jwt/badges/coverage.svg)](https://codeclimate.com/github/waiting-for-dev/devise-jwt/coverage)
6
+
7
+ `devise-jwt` is a [devise](https://github.com/plataformatec/devise) extension which uses [JWT](https://jwt.io/) tokens for user authentication. It follows [secure by default](https://en.wikipedia.org/wiki/Secure_by_default) principle.
8
+
9
+ You can read about which security concerns this library takes into account and about JWT generic secure usage in the following series of posts:
10
+
11
+ - [Stand Up for JWT Revocation](http://waiting-for-dev.github.io/blog/2017/01/23/stand_up_for_jwt_revocation/)
12
+ - [JWT Recovation Strategies](http://waiting-for-dev.github.io/blog/2017/01/24/jwt_revocation_strategies/)
13
+ - [JWT Secure Usage](http://waiting-for-dev.github.io/blog/2017/01/25/jwt_secure_usage/)
14
+ - [A secure JWT authentication implementation for Rack and Rails](http://waiting-for-dev.github.io/blog/2017/01/26/a_secure_jwt_authentication_for_rack_and_rails)
15
+
16
+ `devise-jwt` is just a thin layer on top of [`warden-jwt_auth`](https://github.com/waiting-for-dev/warden-jwt_auth) that configures it to be used out of the box with devise and Rails.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'devise-jwt', '~> 0.1.0'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install devise-jwt
33
+
34
+ ## Usage
35
+
36
+ ### Secret key configuration
37
+
38
+ First of all, you have to configure the secret key that will be used to sign generated tokens. You can do it in the devise initializer:
39
+
40
+ ```ruby
41
+ Devise.setup do |config|
42
+ # ...
43
+ config.jwt do |jwt|
44
+ jwt.secret = ENV['DEVISE_JWT_SECRET_KEY']
45
+ end
46
+ end
47
+ ```
48
+
49
+ **Important:** You are encouraged to use a secret different than your application `secret_key_base`. It is quite possible that some other component of your system is already using it. If several components share the same secret key, chances that a vulnerability in one of them has a wider impact increase. In rails, generating new secrets is as easy as `bundle exec rake secret`. Also, never share your secrets pushing it to a remote repository, you are better off using an environment variable like in the example.
50
+
51
+ Currently, HS256 algorithm is the one in use.
52
+
53
+ ### Model configuration
54
+
55
+ You have to tell which user models you want to be able to authenticate with JWT tokens. For them, the authentication process will be like this:
56
+
57
+ - A user authenticates trough devise create session request (for example, using the standard `:database_authenticatable` module).
58
+ - If the authentication succeeds, a JWT token is dispatched to the client in the `Authorization` response header, with format `Bearer #{token}`
59
+ - The client can use this token to authenticate following requests for the same user, providing it in the `Authorization` request header, also with format `Bearer #{token}`
60
+ - When the client visits devise destroy session request, the token is revoked.
61
+
62
+ As you see, unlike other JWT authentication libraries, it is expected that tokens will be revoked by the server. I wrote about [why I think JWT revocation is needed and useful](http://waiting-for-dev.github.io/blog/2017/01/23/stand_up_for_jwt_revocation/).
63
+
64
+ An example configuration:
65
+
66
+ ```ruby
67
+ class User < ApplicationRecord
68
+ devise :database_authenticatable,
69
+ :jwt_authenticatable, jwt_revocation_strategy: Blacklist
70
+ end
71
+ ```
72
+
73
+ If you need to add something to the JWT payload, you can do it defining a `jwt_payload` method in the user model. It must return a `Hash`. For instance:
74
+
75
+ ```ruby
76
+ def jwt_payload
77
+ { 'foo' => 'bar' }
78
+ end
79
+ ```
80
+
81
+ ### Revocation strategies
82
+
83
+ `devise-jwt` comes with two revocation strategies out of the box. They are implementations of what is discussed in the blog post [JWT Recovation Strategies](http://waiting-for-dev.github.io/blog/2017/01/24/jwt_revocation_strategies/), where I also talk about their pros and cons.
84
+
85
+ #### JTIMatcher
86
+
87
+ Here, the model class acts itself as the revocation strategy. It needs a new string column with name `jti` to be added to the user. `jti` stands for JWT ID, and it is a standard claim meant to uniquely identify a token.
88
+
89
+ It works like the following:
90
+
91
+ - At the same time that a token is dispatched for a user, the `jti` claim is persisted to the `jti` column.
92
+ - At every authenticated action, the incoming token `jti` claim is matched against the `jti` column for that user. The authentication only succeeds if they are the same.
93
+ - When the user requests to sign out its `jti` column changes, so that provided token won't be valid anymore.
94
+
95
+ In order to use it, you need to add the `jti` column to the user model. So, you have to set something like the following in a migration:
96
+
97
+ ```ruby
98
+ def change
99
+ add_column :users, :jti, :string, null: false
100
+ add_index :users, :jti, unique: true
101
+ # If you already have user records, you will need to initialize its `jti` column before setting it to not nullable. Your migration will look this way:
102
+ # add_column :users, :jti, :string
103
+ # User.all.each { |user| user.update_column(:jti, SecureRandom.uuid) }
104
+ # change_column_null :users, :jti, false
105
+ # add_index :users, :jti, unique: true
106
+ end
107
+ ```
108
+
109
+ **Important:** You are encouraged to set a unique index in the `jti` column. This way we can be sure at the database level that there aren't two valid tokens with same `jti` at the same time.
110
+
111
+ Then, you have to add the strategy to the model class and configure it accordingly:
112
+
113
+ ```ruby
114
+ class User < ApplicationRecord
115
+ include Devise::JWT::RevocationStrategies::JTIMatcher
116
+
117
+ devise :database_authenticatable,
118
+ jwt_revocation_strategy: self
119
+ end
120
+ ```
121
+
122
+ Be aware that this strategy makes uses of `jwt_payload` method in the user model, so if you need to use it don't forget to call `super`:
123
+
124
+ ```ruby
125
+ def jwt_payload
126
+ super.merge('foo' => 'bar')
127
+ end
128
+ ```
129
+
130
+ #### Blacklist
131
+
132
+ In this strategy, a database table is used as a blacklist of revoked JWT tokens. The `jti` claim, which uniquely identifies a token, is persisted.
133
+
134
+ In order to use it, you need to create the blacklist table in a migration:
135
+
136
+ ```ruby
137
+ def change
138
+ create_table :jwt_blacklist do |t|
139
+ t.string :jti, null: false
140
+ end
141
+ add_index :jwt_blacklist, :jti
142
+ end
143
+ ```
144
+
145
+ For performance reasons, it is better if the `jti` column is an index.
146
+
147
+ Then, you need to create the corresponding model and include the strategy:
148
+
149
+ ```ruby
150
+ class JWTBlacklist < ApplicationRecord
151
+ include Devise::JWT::RevocationStrategies::Blacklist
152
+
153
+ self.table_name = 'jwt_blacklist'
154
+ end
155
+ ```
156
+
157
+ Last, configure the user model to use it:
158
+
159
+ ```ruby
160
+ class User < ApplicationRecord
161
+ devise :database_authenticatable,
162
+ jwt_revocation_strategy: JWTBlacklist
163
+ end
164
+ ```
165
+
166
+ #### Null strategy
167
+
168
+ A [null object pattern](https://en.wikipedia.org/wiki/Null_Object_pattern) strategy, which does not revoke tokens, is provided out of the box just in case you are absolutely sure you don't need token revocation. It is recommended **not to use it**.
169
+
170
+ ```ruby
171
+ class User < ApplicationRecord
172
+ devise :database_authenticatable,
173
+ :jwt_authenticatable, jwt_revocation_strategy: Devise::JWT::RevocationStrategies::Null
174
+ end
175
+ ```
176
+
177
+ #### Custom strategies
178
+
179
+ You can also implement your own strategies. They just need to implement two methods: `jwt_revoked?` and `revoke_jwt`, both of them accepting as parameters the JWT payload and the user record, in this order.
180
+
181
+ For instance:
182
+
183
+ ```ruby
184
+ module MyCustomStrategy
185
+ def self.jwt_revoked?(payload, user)
186
+ # Does something to check whether the JWT token is revoked for given user
187
+ end
188
+
189
+ def self.revoke_jwt(payload, user)
190
+ # Does something to revoke the JWT token for given user
191
+ end
192
+ end
193
+
194
+ class User < ApplicationRecord
195
+ devise :database_authenticatable,
196
+ :jwt_authenticatable, jwt_revocation_strategy: MyCustomStrategy
197
+ end
198
+ ```
199
+
200
+ ### Configuration reference
201
+
202
+ This library can be configured calling `jwt` on devise config object:
203
+
204
+ ```ruby
205
+ Devise.setup do |config|
206
+ config.jwt do |jwt|
207
+ # ...
208
+ end
209
+ end
210
+ ```
211
+ #### secret
212
+
213
+ Secret key used to sign generated JWT tokens. You must set it.
214
+
215
+ #### expiration_time
216
+
217
+ Number of seconds while a JWT is valid after its generation. After that, it won't be valid anymore, even if it hasn't been revoked.
218
+
219
+ Defaults to 3600 (1 hour).
220
+
221
+ #### dispatch_requests
222
+
223
+ Besides the create session one, additional requests where JWT tokens should be dispatched.
224
+
225
+ It must be a bidimensional array, each item being an array of two elements: the request method and a regular expression that must match the request path.
226
+
227
+ For example:
228
+
229
+ ```ruby
230
+ jwt.dispatch_requests = [
231
+ ['POST', %r{^/dispatch_path_1$}],
232
+ ['GET', %r{^/dispatch_path_2$}],
233
+ ]
234
+ ```
235
+
236
+ **Important**: You are encouraged to delimit your regular expression with `^` and `$` to avoid unintentional matches.
237
+
238
+ #### revocation_requests
239
+
240
+ Besides the destroy session one, additional requests where JWT tokens should be revoked.
241
+
242
+ It must be a bidimensional array, each item being an array of two elements: the request method and a regular expression that must match the request path.
243
+
244
+ For example:
245
+
246
+ ```ruby
247
+ jwt.revocation_requests = [
248
+ ['DELETE', %r{^/revocation_path_1$}],
249
+ ['GET', %r{^/revocation_path_2$}],
250
+ ]
251
+ ```
252
+
253
+ **Important**: You are encouraged to delimit your regular expression with `^` and `$` to avoid unintentional matches.
254
+
255
+ ## Development
256
+
257
+ There are docker and docker-compose files configured to create a development environment for this gem. So, if you use Docker you only need to run:
258
+
259
+ `docker-compose up -d`
260
+
261
+ An then, for example:
262
+
263
+ `docker-compose exec app rspec`
264
+
265
+ This gem uses [overcommit](https://github.com/brigade/overcommit) to execute some code review engines. If you submit a pull request, it will be executed in the CI process. In order to set it up, you need to do:
266
+
267
+ ```ruby
268
+ bundle install --gemfile=.overcommit_gems.rb
269
+ overcommit --sign
270
+ overcommit --run # To test if it works
271
+ ```
272
+
273
+ ## Contributing
274
+
275
+ Bug reports and pull requests are welcome on GitHub at https://github.com/waiting-for-dev/devise-jwt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
276
+
277
+ ## Release Policy
278
+
279
+ `devise-jwt` follows the principles of [semantic versioning](http://semver.org/).
280
+
281
+ ## License
282
+
283
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "devise/jwt"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.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,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'devise/jwt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "devise-jwt"
8
+ spec.version = Devise::JWT::VERSION
9
+ spec.authors = ["Marc Busqué"]
10
+ spec.email = ["marc@lamarciana.com"]
11
+
12
+ spec.summary = %q{JWT authentication for devise}
13
+ spec.description = %q{JWT authentication for devise with configurable token revocation strategies}
14
+ spec.homepage = "https://github.com/waiting-for-dev/devise-jwt"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'devise', '~> 4.0'
25
+ spec.add_dependency 'warden-jwt_auth', '~> 0.1.0'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.12"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "pry-byebug", "~> 3.4"
31
+ # Needed to test the rails fixture application
32
+ spec.add_development_dependency 'rails', '~> 5.0'
33
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
34
+ spec.add_development_dependency 'rspec-rails', '~> 3.5'
35
+ # Test reporting
36
+ spec.add_development_dependency 'simplecov', '~> 0.13'
37
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
38
+ end
@@ -0,0 +1,7 @@
1
+ version: '2'
2
+ services:
3
+ app:
4
+ build: .
5
+ command: tail -f Gemfile
6
+ volumes:
7
+ - .:/app
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'devise'
4
+ require 'active_support/core_ext/module/attribute_accessors'
5
+ require 'warden/jwt_auth'
6
+ require 'devise/jwt/version'
7
+ require 'devise/jwt/defaults_generator'
8
+ require 'devise/jwt/railtie'
9
+ require 'devise/jwt/models'
10
+ require 'devise/jwt/revocation_strategies'
11
+
12
+ # Authentication library
13
+ module Devise
14
+ # Yields to Warden::JWTAuth.config
15
+ #
16
+ # @see Warden::JWTAuth
17
+ def self.jwt
18
+ yield(Warden::JWTAuth.config)
19
+ end
20
+
21
+ add_module(:jwt_authenticatable, strategy: :jwt)
22
+
23
+ # JWT extension for devise
24
+ module JWT
25
+ end
26
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Devise
4
+ module JWT
5
+ # Generate defaults to be used in the configuration for the Devise
6
+ # installation in a Rails app
7
+ #
8
+ # @see Warden::JWTAuth
9
+ class DefaultsGenerator
10
+ attr_reader :routes, :devise_mappings
11
+
12
+ def initialize
13
+ @routes = Rails.application.routes
14
+ @devise_mappings = Devise.mappings
15
+ end
16
+
17
+ def mappings
18
+ @mappings ||= devise_mappings.each_with_object({}) do |tuple, hash|
19
+ scope, mapping = tuple
20
+ modules = mapping.modules
21
+ next unless modules.include?(:jwt_authenticatable)
22
+ hash[scope] = mapping.to
23
+ end
24
+ end
25
+
26
+ def dispatch_requests
27
+ scopes.each_with_object([]) do |scope, array|
28
+ named_route = "#{scope}_session"
29
+ array << request_for(named_route)
30
+ end
31
+ end
32
+
33
+ def revocation_requests
34
+ scopes.each_with_object([]) do |scope, array|
35
+ named_route = "destroy_#{scope}_session"
36
+ array << request_for(named_route)
37
+ end
38
+ end
39
+
40
+ def revocation_strategies
41
+ mappings.each_with_object({}) do |tuple, hash|
42
+ scope, model = tuple
43
+ hash[scope] = model.jwt_revocation_strategy
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def scopes
50
+ mappings.keys
51
+ end
52
+
53
+ def request_for(named_route)
54
+ named_path = "#{named_route}_path"
55
+ route = routes.named_routes[named_route]
56
+ method = route.verb
57
+ path = /^#{routes.url_helpers.send(named_path)}$/
58
+ [method, path]
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'devise/jwt/models/jwt_authenticatable'
4
+
5
+ module Devise
6
+ # Devise models
7
+ #
8
+ # @see Devise::Models
9
+ module Models
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module Devise
6
+ module Models
7
+ # Model that will be authenticatable with the JWT strategy
8
+ #
9
+ # @see [Warden::JWTAuth::Interfaces::UserRepository]
10
+ # @see [Warden::JWTAuth::Interfaces::User]
11
+ module JwtAuthenticatable
12
+ extend ActiveSupport::Concern
13
+
14
+ class_methods do
15
+ Devise::Models.config(self, :jwt_revocation_strategy)
16
+ end
17
+
18
+ included do
19
+ def self.find_for_jwt_authentication(sub)
20
+ find(sub)
21
+ end
22
+ end
23
+
24
+ def jwt_subject
25
+ id
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module Devise
6
+ module JWT
7
+ # Pluck to rails
8
+ class Railtie < Rails::Railtie
9
+ initializer 'devise-jwt-middleware' do |app|
10
+ app.middleware.use Warden::JWTAuth::Middleware
11
+
12
+ config.after_initialize do
13
+ Rails.application.reload_routes!
14
+
15
+ Warden::JWTAuth.configure do |config|
16
+ defaults = DefaultsGenerator.new
17
+
18
+ config.mappings = defaults.mappings
19
+ config.dispatch_requests.push(*defaults.dispatch_requests)
20
+ config.revocation_requests.push(*defaults.revocation_requests)
21
+ config.revocation_strategies = defaults.revocation_strategies
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'devise/jwt/revocation_strategies/jti_matcher'
4
+ require 'devise/jwt/revocation_strategies/blacklist'
5
+ require 'devise/jwt/revocation_strategies/null'
6
+
7
+ module Devise
8
+ module JWT
9
+ # Pre-build revocation strategies
10
+ #
11
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy
12
+ module RevocationStrategies
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module Devise
6
+ module JWT
7
+ module RevocationStrategies
8
+ # This strategy must be included in an ActiveRecord model, and requires
9
+ # that it has a `jti` column.
10
+ #
11
+ # In order to tell whether a token is revoked, it just checks whether
12
+ # `jti` is in the table. On revocation, creates a new record with it.
13
+ module Blacklist
14
+ extend ActiveSupport::Concern
15
+
16
+ included do
17
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy#jwt_revoked?
18
+ def self.jwt_revoked?(payload, _user)
19
+ exists?(jti: payload['jti'])
20
+ end
21
+
22
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy#revoke_jwt
23
+ def self.revoke_jwt(payload, _user)
24
+ create(jti: payload['jti'])
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'securerandom'
5
+
6
+ module Devise
7
+ module JWT
8
+ module RevocationStrategies
9
+ # This strategy must be included in the user model, and requires that it
10
+ # has a `jti` column. It adds the value of the `jti` column as the `jti`
11
+ # claim in dispatched tokens.
12
+ #
13
+ # In order to tell whether a token is revoked, it just compares both `jti`
14
+ # values. On revocation, it changes column value so that the token is no
15
+ # longer valid.
16
+ module JTIMatcher
17
+ extend ActiveSupport::Concern
18
+
19
+ included do
20
+ before_create :initialize_jti
21
+
22
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy#jwt_revoked?
23
+ def self.jwt_revoked?(payload, user)
24
+ payload['jti'] != user.jti
25
+ end
26
+
27
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy#revoke_jwt
28
+ def self.revoke_jwt(_payload, user)
29
+ user.update_column(:jti, generate_jti)
30
+ end
31
+
32
+ # Generates a random and unique string to be used as jti
33
+ def self.generate_jti
34
+ SecureRandom.uuid
35
+ end
36
+ end
37
+
38
+ # Warden::JWTAuth::Interfaces::User#jwt_payload
39
+ def jwt_payload
40
+ { 'jti' => jti }
41
+ end
42
+
43
+ private
44
+
45
+ def initialize_jti
46
+ self.jti = self.class.generate_jti
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module Devise
6
+ module JWT
7
+ module RevocationStrategies
8
+ # This strategy is just a null object pattern strategy, so it does not
9
+ # revoke anything
10
+ module Null
11
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy#jwt_revoked?
12
+ def self.jwt_revoked?(_payload, _user)
13
+ false
14
+ end
15
+
16
+ # @see Warden::JWTAuth::Interfaces::RevocationStrategy#revoke_jwt
17
+ def self.revoke_jwt(_payload, _user); end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Devise
4
+ module JWT
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise-jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc Busqué
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: devise
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: warden-jwt_auth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.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.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-rails
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.5'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.13'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.13'
153
+ - !ruby/object:Gem::Dependency
154
+ name: codeclimate-test-reporter
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ description: JWT authentication for devise with configurable token revocation strategies
168
+ email:
169
+ - marc@lamarciana.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".codeclimate.yml"
175
+ - ".gitignore"
176
+ - ".overcommit.yml"
177
+ - ".overcommit_gems.rb"
178
+ - ".reek"
179
+ - ".rspec"
180
+ - ".rubocop.yml"
181
+ - ".travis.yml"
182
+ - CODE_OF_CONDUCT.md
183
+ - Dockerfile
184
+ - Gemfile
185
+ - LICENSE.txt
186
+ - README.md
187
+ - Rakefile
188
+ - bin/console
189
+ - bin/setup
190
+ - devise-jwt.gemspec
191
+ - docker-compose.yml
192
+ - lib/devise/jwt.rb
193
+ - lib/devise/jwt/defaults_generator.rb
194
+ - lib/devise/jwt/models.rb
195
+ - lib/devise/jwt/models/jwt_authenticatable.rb
196
+ - lib/devise/jwt/railtie.rb
197
+ - lib/devise/jwt/revocation_strategies.rb
198
+ - lib/devise/jwt/revocation_strategies/blacklist.rb
199
+ - lib/devise/jwt/revocation_strategies/jti_matcher.rb
200
+ - lib/devise/jwt/revocation_strategies/null.rb
201
+ - lib/devise/jwt/version.rb
202
+ homepage: https://github.com/waiting-for-dev/devise-jwt
203
+ licenses:
204
+ - MIT
205
+ metadata: {}
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubyforge_project:
222
+ rubygems_version: 2.6.6
223
+ signing_key:
224
+ specification_version: 4
225
+ summary: JWT authentication for devise
226
+ test_files: []