jwt-auth 4.2.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -0
  3. data/Gemfile +3 -0
  4. data/README.md +119 -18
  5. data/bin/build +22 -0
  6. data/bin/release +40 -0
  7. data/jwt-auth.gemspec +18 -15
  8. data/lib/jwt/auth.rb +2 -0
  9. data/lib/jwt/auth/access_token.rb +20 -0
  10. data/lib/jwt/auth/authenticatable.rb +16 -0
  11. data/lib/jwt/auth/authentication.rb +63 -22
  12. data/lib/jwt/auth/configuration.rb +4 -1
  13. data/lib/jwt/auth/refresh_token.rb +20 -0
  14. data/lib/jwt/auth/token.rb +49 -41
  15. data/lib/jwt/auth/version.rb +3 -1
  16. data/spec/controllers/content_controller_spec.rb +95 -0
  17. data/spec/controllers/tokens_controller_spec.rb +140 -0
  18. data/spec/dummy/Rakefile +2 -0
  19. data/spec/dummy/app/channels/application_cable/channel.rb +2 -0
  20. data/spec/dummy/app/channels/application_cable/connection.rb +2 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +6 -1
  22. data/spec/dummy/app/controllers/content_controller.rb +29 -0
  23. data/spec/dummy/app/controllers/tokens_controller.rb +53 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/helpers/authentication_helper.rb +2 -0
  26. data/spec/dummy/app/jobs/application_job.rb +2 -0
  27. data/spec/dummy/app/mailers/application_mailer.rb +3 -1
  28. data/spec/dummy/app/models/application_record.rb +2 -0
  29. data/spec/dummy/app/models/user.rb +3 -6
  30. data/spec/dummy/bin/bundle +2 -0
  31. data/spec/dummy/bin/rails +2 -0
  32. data/spec/dummy/bin/rake +2 -0
  33. data/spec/dummy/bin/setup +2 -0
  34. data/spec/dummy/bin/update +2 -0
  35. data/spec/dummy/bin/yarn +7 -7
  36. data/spec/dummy/config.ru +2 -0
  37. data/spec/dummy/config/application.rb +2 -0
  38. data/spec/dummy/config/boot.rb +3 -1
  39. data/spec/dummy/config/environment.rb +2 -0
  40. data/spec/dummy/config/environments/development.rb +3 -1
  41. data/spec/dummy/config/environments/production.rb +4 -2
  42. data/spec/dummy/config/environments/test.rb +2 -0
  43. data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
  44. data/spec/dummy/config/initializers/assets.rb +2 -0
  45. data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
  46. data/spec/dummy/config/initializers/content_security_policy.rb +2 -0
  47. data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
  48. data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  49. data/spec/dummy/config/initializers/inflections.rb +2 -0
  50. data/spec/dummy/config/initializers/jwt_auth.rb +9 -2
  51. data/spec/dummy/config/initializers/mime_types.rb +2 -0
  52. data/spec/dummy/config/initializers/new_framework_defaults_5_2.rb +2 -0
  53. data/spec/dummy/config/initializers/wrap_parameters.rb +3 -1
  54. data/spec/dummy/config/puma.rb +5 -3
  55. data/spec/dummy/config/routes.rb +5 -4
  56. data/spec/dummy/config/spring.rb +4 -2
  57. data/spec/dummy/db/migrate/20170726110751_create_users.rb +2 -0
  58. data/spec/dummy/db/migrate/20170726110825_add_token_version_to_user.rb +2 -0
  59. data/spec/dummy/db/migrate/20170726112117_add_activated_to_user.rb +2 -0
  60. data/spec/dummy/db/migrate/20190221100103_add_password_to_user.rb +7 -0
  61. data/spec/dummy/db/schema.rb +10 -9
  62. data/spec/jwt/auth/access_token_spec.rb +35 -0
  63. data/spec/jwt/auth/configuration_spec.rb +36 -0
  64. data/spec/jwt/auth/refresh_token_spec.rb +35 -0
  65. data/spec/jwt/auth/token_spec.rb +144 -0
  66. data/spec/models/user_spec.rb +24 -0
  67. data/spec/rails_helper.rb +8 -0
  68. data/spec/spec_helper.rb +51 -53
  69. data/spec/support/database_cleaner.rb +22 -0
  70. data/spec/support/matchers/return_token.rb +33 -0
  71. data/version.yml +1 -0
  72. metadata +119 -54
  73. data/spec/authentication_spec.rb +0 -136
  74. data/spec/configuration_spec.rb +0 -18
  75. data/spec/dummy/app/controllers/authentication_controller.rb +0 -22
  76. 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,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ApplicationHelper
2
4
  end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AuthenticationHelper
2
4
  end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ApplicationJob < ActiveJob::Base
2
4
  end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ApplicationMailer < ActionMailer::Base
2
- default from: 'from@example.com'
4
+ default :from => 'from@example.com'
3
5
  layout 'mailer'
4
6
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ApplicationRecord < ActiveRecord::Base
2
4
  self.abstract_class = true
3
5
  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
- def increment_token_version!
11
- self.token_version += 1
12
- save!
13
- end
10
+ scope :active, -> { where :activated => true }
14
11
  end
@@ -1,3 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __dir__)
3
5
  load Gem.bin_path('bundler', 'bundle')
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  APP_PATH = File.expand_path('../config/application', __dir__)
3
5
  require_relative '../config/boot'
4
6
  require 'rails/commands'
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  require_relative '../config/boot'
3
5
  require 'rake'
4
6
  Rake.application.run
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  require 'fileutils'
3
5
  include FileUtils
4
6
 
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  require 'fileutils'
3
5
  include FileUtils
4
6
 
@@ -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
- begin
5
- exec "yarnpkg", *ARGV
6
- rescue Errno::ENOENT
7
- $stderr.puts "Yarn executable was not detected in the system."
8
- $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is used by Rack-based servers to start the application.
2
4
 
3
5
  require_relative 'config/environment'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'boot'
2
4
 
3
5
  require 'rails/all'
@@ -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
  # Load the Rails application.
2
4
  require_relative 'application'
3
5
 
@@ -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 = [ :request_id ]
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["RAILS_LOG_TO_STDOUT"].present?
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,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
 
@@ -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
  # ActiveSupport::Reloader.to_prepare do
@@ -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
  # Version of your assets, change this if you want to expire all your assets.
@@ -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
  # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
@@ -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
  # Define an application-wide content security policy
@@ -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
  # Specify a serializer for the signed and encrypted cookie jars.
@@ -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
  # Configure sensitive parameters which will be filtered from the log file.
@@ -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
  # Add new inflection rules using the following format. Inflections
@@ -1,8 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  JWT::Auth.configure do |config|
2
4
  ##
3
- # Token lifetime
5
+ # Refresh token lifetime
6
+ #
7
+ config.refresh_token_lifetime = 1.year
8
+
9
+ ##
10
+ # Access token lifetime
4
11
  #
5
- config.token_lifetime = 24.hours
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
  # Add new mime types for use in respond_to blocks:
@@ -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 migration options to ease your Rails 5.2 upgrade.
@@ -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: [:json]
10
+ wrap_parameters :format => [:json]
9
11
  end
10
12
 
11
13
  # To enable root element in JSON for ActiveRecord objects.
@@ -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("RAILS_MAX_THREADS") { 5 }
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("PORT") { 3000 }
14
+ port ENV.fetch('PORT') { 3000 }
13
15
 
14
16
  # Specifies the `environment` that Puma will run in.
15
17
  #
16
- environment ENV.fetch("RAILS_ENV") { "development" }
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
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
- # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
4
+ resource :token, :only => %i[create update]
3
5
 
4
- get '/public' => 'authentication#public'
5
- get '/private' => 'authentication#private'
6
- get '/validate' => 'authentication#validate'
6
+ get '/unauthenticated' => 'content#unauthenticated'
7
+ get '/authenticated' => 'content#authenticated'
7
8
  end
@@ -1,6 +1,8 @@
1
- %w(
1
+ # frozen_string_literal: true
2
+
3
+ %w[
2
4
  .ruby-version
3
5
  .rbenv-vars
4
6
  tmp/restart.txt
5
7
  tmp/caching-dev.txt
6
- ).each { |path| Spring.watch(path) }
8
+ ].each { |path| Spring.watch(path) }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateUsers < ActiveRecord::Migration[5.1]
2
4
  def change
3
5
  create_table :users do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddTokenVersionToUser < ActiveRecord::Migration[5.0]
2
4
  def change
3
5
  add_column :users, :token_version, :integer, :null => false, :default => 1
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddActivatedToUser < ActiveRecord::Migration[5.1]
2
4
  def change
3
5
  add_column :users, :activated, :boolean, :default => false
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddPasswordToUser < ActiveRecord::Migration[5.2]
4
+ def change
5
+ add_column :users, :password, :string
6
+ end
7
+ end