jwt_api_auth 0.0.1.pre.1 → 0.0.1.pre.6

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: ab929b57a3c2918471b598db0fd2fe686c1954fe71aa278607019d0f89f7e88b
4
- data.tar.gz: f1ba367598e833ab7ec53acea362fe2e21e92d9e8be18cf43ab4a9a0169abfbe
3
+ metadata.gz: 2d8037fc592eeb8985d15ec714a79a760a72070833fe199743305896af28c702
4
+ data.tar.gz: 03a845d6521ccecf8d91040ab1fcb7434efea54109d25b3c640c310bcd804574
5
5
  SHA512:
6
- metadata.gz: 72ff5a40fc5712b7c72e7da4d301614b6e54dd857427c3d15548ceadb0bcbd7ecd69c6937e5475707d81eb3fa2e1cec6dd0dbb909fb6efa442a3d9aa9d533110
7
- data.tar.gz: 27db13da989ec2fa8c361f2dddb59d58ad2a6d256e5e7897b5ca747f823a23a36b6c1ca145db160bba5c0070bd78ef23703770eaec8cddb566469b667ac344f2
6
+ metadata.gz: 4b1438eb75ae7ed255d706eaf4dadc15d4e1cc33776d810f876cdca6ccd4cfc5c41448381aec35b908bfe04fddf9959f720b147c5b30b4d7354fee49f67c2a79
7
+ data.tar.gz: d2fef5f4aee3a28517ba36ef5443594c0562bf7a77440fd5199324e4b26f08e259971081556d04381261fa81f073a895b6fe41b840ece729032ba7cfc77e74c3
@@ -2,24 +2,12 @@
2
2
 
3
3
  module JwtApiAuth
4
4
  class ApplicationController < ActionController::Base
5
- skip_before_action :verify_authenticity_token
6
- before_action :authenticate_user
5
+ include JwtApiAuth::Authentication
7
6
 
8
- rescue_from JWT::DecodeError do
9
- head :unauthorized
10
- end
7
+ skip_before_action :verify_authenticity_token
11
8
 
12
9
  rescue_from ActiveRecord::RecordNotFound do
13
10
  head :not_found
14
11
  end
15
-
16
- private
17
-
18
- def authenticate_user
19
- token = request.headers['Authorization']&.split('Bearer ')&.last
20
- JWT.decode token, JwtApiAuth.token_secret.call, true, { algorithm: 'HS256' }
21
-
22
- head :unauthorized unless token
23
- end
24
12
  end
25
13
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_dependency 'jwt_api_auth/application_controller'
4
- require_dependency 'jwt'
5
4
 
6
5
  module JwtApiAuth
7
6
  class AuthenticationController < ApplicationController
@@ -55,11 +54,14 @@ module JwtApiAuth
55
54
  end
56
55
 
57
56
  def payload
58
- { sub: resource.id }
57
+ {
58
+ sub: resource.id,
59
+ aud: JwtApiAuth.token_audience
60
+ }.delete_if { |_key, value| value.blank? }
59
61
  end
60
62
 
61
63
  def token
62
- JWT.encode payload, JwtApiAuth.token_secret.call, 'HS256'
64
+ ::JWT.encode payload, JwtApiAuth.token_secret.call, 'HS256'
63
65
  end
64
66
  end
65
67
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'jwt'
3
4
  require 'jwt_api_auth/engine'
4
5
  require 'jwt_api_auth/helpers'
6
+ require 'jwt_api_auth/authentication'
5
7
 
6
8
  module JwtApiAuth
7
9
  ActiveSupport.on_load(:action_controller) do
@@ -22,4 +24,11 @@ module JwtApiAuth
22
24
 
23
25
  mattr_accessor :refresh_token_model
24
26
  self.refresh_token_model = :refresh_token
27
+
28
+ mattr_accessor :token_audience
29
+ self.token_audience = nil
30
+
31
+ def self.setup
32
+ yield self
33
+ end
25
34
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JwtApiAuth
4
+ module Authentication
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_action :authenticate_user
9
+
10
+ rescue_from ::JWT::DecodeError do
11
+ head :unauthorized
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def authenticate_user
18
+ token = request.headers['Authorization']&.split('Bearer ')&.last
19
+ ::JWT.decode token, JwtApiAuth.token_secret.call, true, { algorithm: 'HS256' }
20
+
21
+ head :unauthorized unless token
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JwtApiAuth
4
- VERSION = '0.0.1.pre.1'
4
+ VERSION = '0.0.1.pre.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jwt_api_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre.1
4
+ version: 0.0.1.pre.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cristian Stügelmayer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-21 00:00:00.000000000 Z
11
+ date: 2020-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -248,7 +248,7 @@ dependencies:
248
248
  - - ">="
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
- description: JWT api authentication
251
+ description: JWT authentication for rails APIs
252
252
  email:
253
253
  - cristian@yellowspot.dev
254
254
  executables: []
@@ -269,6 +269,7 @@ files:
269
269
  - app/views/layouts/jwt_api_auth/application.html.erb
270
270
  - config/routes.rb
271
271
  - lib/jwt_api_auth.rb
272
+ - lib/jwt_api_auth/authentication.rb
272
273
  - lib/jwt_api_auth/engine.rb
273
274
  - lib/jwt_api_auth/helpers.rb
274
275
  - lib/jwt_api_auth/version.rb
@@ -296,5 +297,5 @@ requirements: []
296
297
  rubygems_version: 3.0.8
297
298
  signing_key:
298
299
  specification_version: 4
299
- summary: JWT api authentication
300
+ summary: JWT authentication
300
301
  test_files: []