jwt-auth 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e47915c8619339d0e1250b38f8479515222cbf5e66b7528244c30959de854db
4
- data.tar.gz: 60cff593393734473602826ee53b6ce3ff654bd74d1488f6a32fb2344429d330
3
+ metadata.gz: cb9d83836fe9d226380f942eaa215d51fb3ad36d05ea5ead0aaae1fbb270931a
4
+ data.tar.gz: ec4fcf71b3bea2a226e806ad26c979695fd2eaec8ee506fc078147b2145659d6
5
5
  SHA512:
6
- metadata.gz: c48ba9b85c080ebe882e88f7e2b768da6bd40ad239b390d55c88de07cdfd7a6ff835c25d7fcca5b71086879c4f83427db81b5f9a32ccb9925a532b63c24dc0c2
7
- data.tar.gz: 15ec6923bf0dfdf46e65f2f88435d8ceb43c99d04fca97ede1813e8033008a4f456d2ba9fd918825a3dfe9c43ca8ec522066feb8e3b7b5882c0d3e9bac536add
6
+ metadata.gz: d881ce27f177aa441846d4d7cd8aa9d5518e1c543caa7a5df06f31f5a6b95d4daf13d29a2e2321e767e5ed9574908859cd055fd2458c4bb9c2bd5f44576e1334
7
+ data.tar.gz: 5c94d698a8d3797416b8bb7a357fbccd203fca8fa82b92bf340fafd03e17783467b4ccdc646f66bf0aeef74153f384464c68ae667d440af37b7ad648adc3b9ce
data/README.md CHANGED
@@ -84,7 +84,13 @@ Set callbacks on routes:
84
84
  ```ruby
85
85
  class MyController < ApplicationController
86
86
  # Authenticates user from request header
87
- before_action :authenticate_user
87
+ # The callback raises an UnauthorizedError on missing or invalid token
88
+ before_action :authenticate_user, :except => %i[create]
89
+
90
+ # Validate token if there is a token present
91
+ # The callback raises an UnauthorizedError only if there is a token present, and it is invalid
92
+ # This prevents users from using an expired token on an unauthenticated route and getting a HTTP 2xx
93
+ before_action :validate_token
88
94
 
89
95
  # Renew token and set response header
90
96
  after_action :renew_token
@@ -22,6 +22,13 @@ module JWT
22
22
  raise JWT::Auth::UnauthorizedError unless jwt && jwt.valid?
23
23
  end
24
24
 
25
+ ##
26
+ # Validate a token (authenticate a request iff there is a token)
27
+ #
28
+ def validate_token
29
+ authenticate_user if jwt
30
+ end
31
+
25
32
  ##
26
33
  # Add JWT header to response
27
34
  #
@@ -2,6 +2,6 @@
2
2
 
3
3
  module JWT
4
4
  module Auth
5
- VERSION = '4.1.0'
5
+ VERSION = '4.2.0'
6
6
  end
7
7
  end
@@ -92,4 +92,45 @@ RSpec.describe AuthenticationController, :type => :request do
92
92
  end
93
93
  end
94
94
  end
95
+
96
+ describe 'GET /validate' do
97
+ context 'activated user' do
98
+ it 'is accessible without token' do
99
+ get '/validate'
100
+
101
+ expect(response.status).to eq 204
102
+ end
103
+
104
+ it 'is accessible with token' do
105
+ get '/validate', :headers => headers
106
+
107
+ expect(response.status).to eq 204
108
+ end
109
+
110
+ it 'renews the token' do
111
+ get '/validate', :headers => headers
112
+
113
+ jwt = response.headers['Authorization'].scan(/Bearer (.*)$/).flatten.last
114
+ token = JWT::Auth::Token.from_token jwt
115
+
116
+ expect(token).to be_valid
117
+ end
118
+ end
119
+
120
+ context 'disabled user' do
121
+ let(:user) { User.new }
122
+
123
+ it 'is accessible without token' do
124
+ get '/validate'
125
+
126
+ expect(response.status).to eq 204
127
+ end
128
+
129
+ it 'is not accessible with token' do
130
+ get '/validate', :headers => headers
131
+
132
+ expect(response.status).to eq 401
133
+ end
134
+ end
135
+ end
95
136
  end
@@ -2,14 +2,21 @@ class AuthenticationController < ApplicationController
2
2
  # Authenticates user from request header
3
3
  before_action :authenticate_user, :only => :private
4
4
 
5
+ # Validate token
6
+ before_action :validate_token, :only => :validate
7
+
5
8
  # Renew token and set response header
6
9
  after_action :renew_token
7
10
 
11
+ def public
12
+ head :no_content
13
+ end
14
+
8
15
  def private
9
16
  head :no_content
10
17
  end
11
18
 
12
- def public
19
+ def validate
13
20
  head :no_content
14
21
  end
15
22
  end
@@ -3,4 +3,5 @@ Rails.application.routes.draw do
3
3
 
4
4
  get '/public' => 'authentication#public'
5
5
  get '/private' => 'authentication#private'
6
+ get '/validate' => 'authentication#validate'
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jwt-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dejonckheere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-28 00:00:00.000000000 Z
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt