jwt-auth 4.2.0 → 5.0.0
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/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/README.md +119 -18
- data/bin/build +22 -0
- data/bin/release +40 -0
- data/jwt-auth.gemspec +18 -15
- data/lib/jwt/auth.rb +2 -0
- data/lib/jwt/auth/access_token.rb +20 -0
- data/lib/jwt/auth/authenticatable.rb +16 -0
- data/lib/jwt/auth/authentication.rb +63 -22
- data/lib/jwt/auth/configuration.rb +4 -1
- data/lib/jwt/auth/refresh_token.rb +20 -0
- data/lib/jwt/auth/token.rb +49 -41
- data/lib/jwt/auth/version.rb +3 -1
- data/spec/controllers/content_controller_spec.rb +95 -0
- data/spec/controllers/tokens_controller_spec.rb +140 -0
- data/spec/dummy/Rakefile +2 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +2 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +2 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -1
- data/spec/dummy/app/controllers/content_controller.rb +29 -0
- data/spec/dummy/app/controllers/tokens_controller.rb +53 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/authentication_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -1
- data/spec/dummy/app/models/application_record.rb +2 -0
- data/spec/dummy/app/models/user.rb +3 -6
- data/spec/dummy/bin/bundle +2 -0
- data/spec/dummy/bin/rails +2 -0
- data/spec/dummy/bin/rake +2 -0
- data/spec/dummy/bin/setup +2 -0
- data/spec/dummy/bin/update +2 -0
- data/spec/dummy/bin/yarn +7 -7
- data/spec/dummy/config.ru +2 -0
- data/spec/dummy/config/application.rb +2 -0
- data/spec/dummy/config/boot.rb +3 -1
- data/spec/dummy/config/environment.rb +2 -0
- data/spec/dummy/config/environments/development.rb +3 -1
- data/spec/dummy/config/environments/production.rb +4 -2
- data/spec/dummy/config/environments/test.rb +2 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
- data/spec/dummy/config/initializers/assets.rb +2 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +2 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
- data/spec/dummy/config/initializers/inflections.rb +2 -0
- data/spec/dummy/config/initializers/jwt_auth.rb +9 -2
- data/spec/dummy/config/initializers/mime_types.rb +2 -0
- data/spec/dummy/config/initializers/new_framework_defaults_5_2.rb +2 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +3 -1
- data/spec/dummy/config/puma.rb +5 -3
- data/spec/dummy/config/routes.rb +5 -4
- data/spec/dummy/config/spring.rb +4 -2
- data/spec/dummy/db/migrate/20170726110751_create_users.rb +2 -0
- data/spec/dummy/db/migrate/20170726110825_add_token_version_to_user.rb +2 -0
- data/spec/dummy/db/migrate/20170726112117_add_activated_to_user.rb +2 -0
- data/spec/dummy/db/migrate/20190221100103_add_password_to_user.rb +7 -0
- data/spec/dummy/db/schema.rb +10 -9
- data/spec/jwt/auth/access_token_spec.rb +35 -0
- data/spec/jwt/auth/configuration_spec.rb +36 -0
- data/spec/jwt/auth/refresh_token_spec.rb +35 -0
- data/spec/jwt/auth/token_spec.rb +144 -0
- data/spec/models/user_spec.rb +24 -0
- data/spec/rails_helper.rb +8 -0
- data/spec/spec_helper.rb +51 -53
- data/spec/support/database_cleaner.rb +22 -0
- data/spec/support/matchers/return_token.rb +33 -0
- data/version.yml +1 -0
- metadata +119 -54
- data/spec/authentication_spec.rb +0 -136
- data/spec/configuration_spec.rb +0 -18
- data/spec/dummy/app/controllers/authentication_controller.rb +0 -22
- data/spec/token_spec.rb +0 -125
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ContentController < ApplicationController
|
4
|
+
# Validate access token on all actions
|
5
|
+
before_action :validate_access_token
|
6
|
+
|
7
|
+
# Require token for protected actions
|
8
|
+
before_action :require_token, :only => :authenticated
|
9
|
+
|
10
|
+
##
|
11
|
+
# GET /unauthenticated
|
12
|
+
#
|
13
|
+
# This endpoint is not protected, performing a request without a token, or with a valid token will succeed
|
14
|
+
# Performing a request with an invalid token will raise an UnauthorizedError
|
15
|
+
#
|
16
|
+
def unauthenticated
|
17
|
+
head :no_content
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# GET /unauthenticated
|
22
|
+
#
|
23
|
+
# This endpoint is protected, performing a request with a valid access token will succeed
|
24
|
+
# Performing a request without a token, with an invalid token or with a refresh token will raise an UnauthorizedError
|
25
|
+
#
|
26
|
+
def authenticated
|
27
|
+
head :no_content
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class TokensController < ApplicationController
|
4
|
+
# Validate refresh token on refresh action
|
5
|
+
before_action :validate_refresh_token, :only => :update
|
6
|
+
|
7
|
+
# Require token only on refresh action
|
8
|
+
before_action :require_token, :only => :update
|
9
|
+
|
10
|
+
##
|
11
|
+
# POST /token
|
12
|
+
#
|
13
|
+
# Sign in the user
|
14
|
+
#
|
15
|
+
# ::request::
|
16
|
+
#
|
17
|
+
# @body email, password
|
18
|
+
#
|
19
|
+
# ::response::
|
20
|
+
#
|
21
|
+
# @header Authorization A long lived refresh token
|
22
|
+
#
|
23
|
+
def create
|
24
|
+
@user = User.active.find_by :email => params[:email], :password => params[:password]
|
25
|
+
raise JWT::Auth::UnauthorizedError unless @user
|
26
|
+
|
27
|
+
# Return a long-lived refresh token
|
28
|
+
set_refresh_token @user
|
29
|
+
|
30
|
+
head :no_content
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
#
|
35
|
+
# PATCH /token
|
36
|
+
#
|
37
|
+
# Refresh access token
|
38
|
+
#
|
39
|
+
# ::request::
|
40
|
+
#
|
41
|
+
# @header Authorization Refresh token
|
42
|
+
#
|
43
|
+
# ::response::
|
44
|
+
#
|
45
|
+
# @header Authorization Access token
|
46
|
+
#
|
47
|
+
def update
|
48
|
+
# Return a short-lived access token
|
49
|
+
set_access_token
|
50
|
+
|
51
|
+
head :no_content
|
52
|
+
end
|
53
|
+
end
|
@@ -1,14 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class User < ApplicationRecord
|
2
4
|
include JWT::Auth::Authenticatable
|
3
5
|
|
4
|
-
validates :token_version, :presence => true
|
5
|
-
|
6
6
|
def self.find_by_token(params)
|
7
7
|
find_by params.merge :activated => true
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
self.token_version += 1
|
12
|
-
save!
|
13
|
-
end
|
10
|
+
scope :active, -> { where :activated => true }
|
14
11
|
end
|
data/spec/dummy/bin/bundle
CHANGED
data/spec/dummy/bin/rails
CHANGED
data/spec/dummy/bin/rake
CHANGED
data/spec/dummy/bin/setup
CHANGED
data/spec/dummy/bin/update
CHANGED
data/spec/dummy/bin/yarn
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
APP_ROOT = File.expand_path('..', __dir__)
|
3
5
|
Dir.chdir(APP_ROOT) do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
exit 1
|
10
|
-
end
|
6
|
+
exec 'yarnpkg', *ARGV
|
7
|
+
rescue Errno::ENOENT
|
8
|
+
warn 'Yarn executable was not detected in the system.'
|
9
|
+
warn 'Download Yarn at https://yarnpkg.com/en/docs/install'
|
10
|
+
exit 1
|
11
11
|
end
|
data/spec/dummy/config.ru
CHANGED
data/spec/dummy/config/boot.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __dir__)
|
2
4
|
|
3
5
|
require 'bundler/setup' # Set up gems listed in the Gemfile.
|
4
|
-
#require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
|
6
|
+
# require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Rails.application.configure do
|
2
4
|
# Settings specified here will take precedence over those in config/application.rb.
|
3
5
|
|
@@ -57,5 +59,5 @@ Rails.application.configure do
|
|
57
59
|
|
58
60
|
# Use an evented file watcher to asynchronously detect changes in source code,
|
59
61
|
# routes, locales, etc. This feature depends on the listen gem.
|
60
|
-
#config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
62
|
+
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
61
63
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Rails.application.configure do
|
2
4
|
# Settings specified here will take precedence over those in config/application.rb.
|
3
5
|
|
@@ -54,7 +56,7 @@ Rails.application.configure do
|
|
54
56
|
config.log_level = :debug
|
55
57
|
|
56
58
|
# Prepend all log lines with the following tags.
|
57
|
-
config.log_tags = [
|
59
|
+
config.log_tags = [:request_id]
|
58
60
|
|
59
61
|
# Use a different cache store in production.
|
60
62
|
# config.cache_store = :mem_cache_store
|
@@ -83,7 +85,7 @@ Rails.application.configure do
|
|
83
85
|
# require 'syslog/logger'
|
84
86
|
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
|
85
87
|
|
86
|
-
if ENV[
|
88
|
+
if ENV['RAILS_LOG_TO_STDOUT'].present?
|
87
89
|
logger = ActiveSupport::Logger.new(STDOUT)
|
88
90
|
logger.formatter = config.log_formatter
|
89
91
|
config.logger = ActiveSupport::TaggedLogging.new(logger)
|
@@ -1,8 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
JWT::Auth.configure do |config|
|
2
4
|
##
|
3
|
-
#
|
5
|
+
# Refresh token lifetime
|
6
|
+
#
|
7
|
+
config.refresh_token_lifetime = 1.year
|
8
|
+
|
9
|
+
##
|
10
|
+
# Access token lifetime
|
4
11
|
#
|
5
|
-
config.
|
12
|
+
config.access_token_lifetime = 2.hours
|
6
13
|
|
7
14
|
##
|
8
15
|
# JWT secret
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Be sure to restart your server when you modify this file.
|
2
4
|
|
3
5
|
# This file contains settings for ActionController::ParamsWrapper which
|
@@ -5,7 +7,7 @@
|
|
5
7
|
|
6
8
|
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
|
7
9
|
ActiveSupport.on_load(:action_controller) do
|
8
|
-
wrap_parameters format
|
10
|
+
wrap_parameters :format => [:json]
|
9
11
|
end
|
10
12
|
|
11
13
|
# To enable root element in JSON for ActiveRecord objects.
|
data/spec/dummy/config/puma.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Puma can serve each request in a thread from an internal thread pool.
|
2
4
|
# The `threads` method setting takes two numbers: a minimum and maximum.
|
3
5
|
# Any libraries that use thread pools should be configured to match
|
4
6
|
# the maximum value specified for Puma. Default is set to 5 threads for minimum
|
5
7
|
# and maximum; this matches the default thread size of Active Record.
|
6
8
|
#
|
7
|
-
threads_count = ENV.fetch(
|
9
|
+
threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
|
8
10
|
threads threads_count, threads_count
|
9
11
|
|
10
12
|
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
|
11
13
|
#
|
12
|
-
port ENV.fetch(
|
14
|
+
port ENV.fetch('PORT') { 3000 }
|
13
15
|
|
14
16
|
# Specifies the `environment` that Puma will run in.
|
15
17
|
#
|
16
|
-
environment ENV.fetch(
|
18
|
+
environment ENV.fetch('RAILS_ENV') { 'development' }
|
17
19
|
|
18
20
|
# Specifies the number of `workers` to boot in clustered mode.
|
19
21
|
# Workers are forked webserver processes. If using threads and workers together
|
data/spec/dummy/config/routes.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Rails.application.routes.draw do
|
2
|
-
|
4
|
+
resource :token, :only => %i[create update]
|
3
5
|
|
4
|
-
get '/
|
5
|
-
get '/
|
6
|
-
get '/validate' => 'authentication#validate'
|
6
|
+
get '/unauthenticated' => 'content#unauthenticated'
|
7
|
+
get '/authenticated' => 'content#authenticated'
|
7
8
|
end
|
data/spec/dummy/config/spring.rb
CHANGED