api_guard 0.5.2 → 0.6.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: bc003426a48f2bf1a83d3b1653438efa43522f465a4cf98b4ac0b030f691cbfd
4
- data.tar.gz: 771a016a40a938684ea61accde401e7eed3dc8c201688f51bb0444cd241901d7
3
+ metadata.gz: 664b5fd08385f8cb690e350127d83bdeb8228264ccf179bc2a4242841e27e114
4
+ data.tar.gz: 6c94a5a7867c4ce5aa186f4f18fbfd4706b654818de791e23b8d83827e58fcf1
5
5
  SHA512:
6
- metadata.gz: 16a0a967961773fcfdab3744fcfd228198484eb550b7b6abeb19e4daa1d2a42d7ecf2730709c4ab7f518bdb0fe1781973255b24da60c67a88b71198cb76913d1
7
- data.tar.gz: e609d92695acc9d424739450b87c6a7cd4725d200e3b3bdf745e465e2c090fe9af7bb15820bb02d9964cc0af420f7faa34fede752484b41dcfe9aaf7bc53114c
6
+ metadata.gz: 4d5b0b0145c8c2c82a4a679c2ece7249e397ec3ad497dc9336388fff6d6830e76f70078336a50b790ace86f374967840c99535b86989bb2bec570d30fbe5b912
7
+ data.tar.gz: e90af09ed86e505eb7a38f489f3347fbfc9f906f7d141748c4c4bc28365cf165cbfa668832eceef29e744dcb9ea929e4b568181730cd6337023183b64cd5cd40
data/README.md CHANGED
@@ -424,7 +424,7 @@ To include token refreshing in your application you need to create a table to st
424
424
  Use below command to create a model `RefeshToken` with columns to store the token and the user reference
425
425
 
426
426
  ```bash
427
- $ rails generate model refresh_token token:string:uniq user:references
427
+ $ rails generate model refresh_token token:string:uniq user:references expire_at:datetime
428
428
  ```
429
429
 
430
430
  Then, run migration to create the `refresh_tokens` table
@@ -34,7 +34,8 @@ module ApiGuard
34
34
  # Also, create refresh token if enabled for the resource.
35
35
  #
36
36
  # This creates expired JWT token if the argument 'expired_token' is true which can be used for testing.
37
- def jwt_and_refresh_token(resource, resource_name, expired_token = false)
37
+ # This creates expired refresh token if the argument 'expired_refresh_token' is true which can be used for testing.
38
+ def jwt_and_refresh_token(resource, resource_name, expired_token = false, expired_refresh_token = false)
38
39
  payload = {
39
40
  "#{resource_name}_id": resource.id,
40
41
  exp: expired_token ? token_issued_at : token_expire_at,
@@ -44,7 +45,7 @@ module ApiGuard
44
45
  # Add custom data in the JWT token payload
45
46
  payload.merge!(resource.jwt_token_payload) if resource.respond_to?(:jwt_token_payload)
46
47
 
47
- [encode(payload), new_refresh_token(resource)]
48
+ [encode(payload), new_refresh_token(resource, expired_refresh_token)]
48
49
  end
49
50
 
50
51
  # Create tokens and set response headers
@@ -4,6 +4,11 @@ module ApiGuard
4
4
  module JwtAuth
5
5
  # Common module for refresh token functionality
6
6
  module RefreshJwtToken
7
+
8
+ def refresh_token_expire_at
9
+ @refresh_token_expire_at ||= (Time.now.utc + ApiGuard.refresh_token_validity)
10
+ end
11
+
7
12
  def refresh_token_association(resource)
8
13
  resource.class.refresh_token_association
9
14
  end
@@ -18,7 +23,7 @@ module ApiGuard
18
23
  end
19
24
 
20
25
  def find_refresh_token_of(resource, refresh_token)
21
- refresh_tokens_for(resource).find_by_token(refresh_token)
26
+ refresh_tokens_for(resource).where(token: refresh_token).where('expire_at IS NULL OR expire_at > ?', Time.now.utc).first
22
27
  end
23
28
 
24
29
  # Generate and return unique refresh token for the resource
@@ -30,10 +35,11 @@ module ApiGuard
30
35
  end
31
36
 
32
37
  # Create a new refresh_token for the current resource
33
- def new_refresh_token(resource)
38
+ # This creates expired refresh_token if the argument 'expired_refresh_token' is true which can be used for testing.
39
+ def new_refresh_token(resource, expired_refresh_token = false)
34
40
  return unless refresh_token_enabled?(resource)
35
41
 
36
- refresh_tokens_for(resource).create(token: uniq_refresh_token(resource)).token
42
+ refresh_tokens_for(resource).create(token: uniq_refresh_token(resource), expire_at: expired_refresh_token ? Time.now.utc : refresh_token_expire_at).token
37
43
  end
38
44
 
39
45
  def destroy_all_refresh_tokens(resource)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ApiGuard
4
- VERSION = '0.5.2'
4
+ VERSION = '0.6.0'
5
5
  end
data/lib/api_guard.rb CHANGED
@@ -14,6 +14,9 @@ module ApiGuard
14
14
  mattr_accessor :token_validity
15
15
  self.token_validity = 1.day
16
16
 
17
+ mattr_accessor :refresh_token_validity
18
+ self.refresh_token_validity = 2.weeks
19
+
17
20
  mattr_accessor :token_signing_secret
18
21
  self.token_signing_secret = nil
19
22
 
@@ -4,6 +4,10 @@ ApiGuard.setup do |config|
4
4
  # Validity of the JWT access token
5
5
  # Default: 1 day
6
6
  # config.token_validity = 1.day
7
+
8
+ # Validity of the refresh token
9
+ # Default: 2 weeks
10
+ # config.refresh_token_validity = 2.weeks
7
11
 
8
12
  # Secret key for signing (encoding & decoding) the JWT access token
9
13
  # Default: 'secret_key_base' from Rails secrets
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_guard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gokul Murali
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-04 00:00:00.000000000 Z
11
+ date: 2022-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt