model_driven_api 3.12.2 → 3.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64d6dc96e02bf63e76a9948d41e1f7203433f9be2f3bb0a2abe16f19179351ad
4
- data.tar.gz: 47bf8c07a5e6381bcabb15a150faae8bd08ae02000db9798c091b2a916e628df
3
+ metadata.gz: 9fcc02687ce88ff1cc47523ddd04f9d1a7c4918d40c2f2765d59f19a1697c0dd
4
+ data.tar.gz: 2eb8c0586bbbdaed24d44d5c8de670ff5339df53c7d6609eedcb8559cc49e16a
5
5
  SHA512:
6
- metadata.gz: 6d7039ac2e39aa75800db5a1ecdabd121a7465c8ffdc966ab88dac7961e53bd00eff37c0b8794b5d6ecca68f5a8d9eed3e4a3a8dd9d800a084301e12a7e321b0
7
- data.tar.gz: 37566ee9050bfe09de0f3d784d0c6b4b6aebb39ec365ceb8c7f59e045b4865d3bf66518ebd1a393c38a8a04db575a2bd3ee2c106df84551f17e79c2b27c33780
6
+ metadata.gz: c48fa599733f52da66626b80067df10d3cb223c3f950a3f3e4f62afe104e50f5b93778cf228b95b89268227b811e74e3b10455fd8d8a3cff0f9d8b8d0f1caf5b
7
+ data.tar.gz: bb50aa93d2b0ce0616e8128b9634b6e38e7fd066bb8fd7a0dba61429000dbf3d125b49930273ebe333126f8ee1104d922cf78eec8f4db6d946cb3f47768d8708
@@ -1,21 +1,44 @@
1
1
  class JsonWebToken
2
2
  class << self
3
3
  def encode(payload, expiry = ENV.fetch('SESSION_TIMEOUT_IN_MINUTES', 31).to_i.minutes.from_now.to_i)
4
- result = ::JWT.encode(payload.merge(exp: expiry), ::Rails.application.credentials.dig(:secret_key_base).presence||ENV["SECRET_KEY_BASE"])
4
+ result = ::JWT.encode(payload.merge(exp: expiry), signing_key)
5
5
  # Store the created token into the DB for later checks if is invalid
6
6
  # In a public environment management, without login, it has no interest, so I don't pollute the DB
7
7
  UsedToken.find_or_create_by(token: result, user_id: payload[:user_id]) if ENV["ALLOW_MULTISESSIONS"] == "false"
8
8
  result
9
9
  end
10
-
10
+
11
11
  def decode(token)
12
- # Check if the passed token is present and valid into the UsedToken
12
+ # Check if the passed token is present and valid into the UsedToken
13
13
  raise "Token is invalidated by new login" if !UsedToken.exists?(token: token, is_valid: true) && ENV["ALLOW_MULTISESSIONS"] == "false"
14
- body = ::JWT.decode(token, ::Rails.application.credentials.dig(:secret_key_base).presence||ENV["SECRET_KEY_BASE"])[0]
14
+ body = verified_payload(token)
15
15
  Rails.logger.debug "JsonWebToken: decode -> body -> #{body}"
16
16
  ::HashWithIndifferentAccess.new body
17
17
  # rescue
18
18
  # nil
19
19
  end
20
+
21
+ private
22
+
23
+ def signing_key
24
+ ::Rails.application.credentials.dig(:secret_key_base).presence||ENV["SECRET_KEY_BASE"]
25
+ end
26
+
27
+ # After a SECRET_KEY_BASE rotation, a token signed with the previous key is still accepted until
28
+ # SECRET_KEY_BASE_PREVIOUS_EXPIRES_AT (ISO 8601), so app users never log in again: every authenticated
29
+ # response renews the token with the current key. The previous key may be public, so it is never
30
+ # trusted without an expiry, and only a bad signature falls back to it (an expired token stays expired).
31
+ def verified_payload(token)
32
+ ::JWT.decode(token, signing_key)[0]
33
+ rescue ::JWT::VerificationError
34
+ previous = previous_signing_key or raise
35
+ ::JWT.decode(token, previous)[0]
36
+ end
37
+
38
+ def previous_signing_key
39
+ key = ENV["SECRET_KEY_BASE_PREVIOUS"].presence or return
40
+ expires_at = Time.iso8601(ENV["SECRET_KEY_BASE_PREVIOUS_EXPIRES_AT"].to_s) rescue nil
41
+ key if expires_at && Time.current < expires_at
42
+ end
20
43
  end
21
- end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.12.2".freeze
2
+ VERSION = "3.13.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_driven_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.12.2
4
+ version: 3.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni