researchable_jwt-authenticable 1.0.1 → 1.2.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: 4e4b4d435b56dec20b1d25bb55500516d5e2087159b7645bdba55eaa7208a7fb
4
- data.tar.gz: c8456c71943a94ab2d7d1539ea187f1ececfdb90e23394134e4c1a1ad1667221
3
+ metadata.gz: 774ef8d7205e1cb743e03ad7f658e0df91db3c77b349accbc3dd62b02ac3d7df
4
+ data.tar.gz: cca34a23985fa690828759f2d85e7175a980aabccdc578b31d795d109daa604f
5
5
  SHA512:
6
- metadata.gz: 469c94c6b1b18896508d2b4657e81780c252eb226eecb82416bae2eb8bed7b1e6beadc19daf98d9490bd057b537d590cd1503781f2564d08761a5e6dd11c0e43
7
- data.tar.gz: 5955c0be8af0285233e47f060962266edd047e4481f53e96ae3ecc144ce32461377cde35becba9e897300aeeac3e1b7c6bc07726b56d8bbe3f2e000cff13d4dc
6
+ metadata.gz: 773a8d193f54f53cf254224ea1db9b1a720183abf9644c36b9734139b6dd497dd52d610b3d1b45d5932d9732f7c8027f73b911aade2968acf32291c476f1f1b5
7
+ data.tar.gz: 7276f611cf981d9752a634b9ecc6e270f50f7c397804184ccc632d3db9d948fad33911929133e856adcf2878a4075abad76d5d246ff889dd8956ce70eef8f4ed
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.2.0](https://gitlab.com/researchable/general/gems/jwt-authenticable/compare/v1.1.0...v1.2.0) (2023-10-11)
2
+
3
+
4
+ ### Features
5
+
6
+ * allow skipping 2fa ([541de8a](https://gitlab.com/researchable/general/gems/jwt-authenticable/commit/541de8a7ac4cc2029c00b44f30dde2486b4dfb06))
7
+
8
+ # [1.1.0](https://gitlab.com/researchable/general/gems/jwt-authenticable/compare/v1.0.1...v1.1.0) (2023-06-30)
9
+
10
+
11
+ ### Features
12
+
13
+ * enforce 2fa ([a65aed5](https://gitlab.com/researchable/general/gems/jwt-authenticable/commit/a65aed5e2ae0ad180239e51aa20dd9d2aa588e81))
14
+
1
15
  ## [1.0.1](https://gitlab.com/researchable/general/gems/jwt-authenticable/compare/v1.0.0...v1.0.1) (2023-06-14)
2
16
 
3
17
 
@@ -11,25 +11,35 @@ module JwtAuthenticable
11
11
 
12
12
  # Authenticates a user.
13
13
  # @raise MissingAuthScope if the jwt does not have the right scope
14
- def authenticate_user!
15
- validate_jwt_token! token: authorization_token!
16
- rescue MissingAuth, MissingAuthScope, InvalidAuthScheme, JWT::VerificationError, JWT::ExpiredSignature => e
14
+ def authenticate_user!(skip_2fa: false)
15
+ validate_jwt_token! token: authorization_token!, skip_2fa: skip_2fa
16
+ rescue MissingAuth, MissingAuthScope, InvalidAuthScheme, TwoFANotEnabledError, JWT::VerificationError,
17
+ JWT::ExpiredSignature => e
17
18
  unauthorized(e.message)
18
19
  end
19
20
 
21
+ def authenticate_user_without_2fa!
22
+ authenticate_user!(skip_2fa: true)
23
+ end
24
+
20
25
  # Consider any method below as private and not meant to be used by including classes
21
26
 
22
27
  # Validate that the JWT token signature and the following claims are valid:
23
28
  # - exp
24
29
  # - scope
25
30
  # @param token [String] JWT token string (just the token, with the header, payload and signature separated by '.')
26
- # @param is_researcher [Boolean] Whether to validate the token as a researcher's or a participant's
31
+ # @param skip_2fa [Boolean] When set to true it will not raise a TwoFANotEnabledError if the jwt payload does not
32
+ # contain the 2fa claim.
27
33
  # @raise AuthorizationError if the user is trying to login with the incorrect rights.
28
34
  # @return [Hash] the JWT payload
29
- def validate_jwt_token!(token:)
35
+ def validate_jwt_token!(token:, skip_2fa: false)
30
36
  # NOTE: it is still safe if JWT_SECRET_KEY is not set. The method will trigger a JWT exception
31
- JWT.decode(token, JwtAuthenticable.config.jwt_secret_key, true,
32
- { algorithm: algorithm }).first
37
+ payload = JWT.decode(token, JwtAuthenticable.config.jwt_secret_key, true,
38
+ { algorithm: algorithm }).first
39
+
40
+ raise TwoFANotEnabledError if JwtAuthenticable.config.enforce_2fa && !payload['2fa'] && !skip_2fa
41
+
42
+ payload
33
43
  end
34
44
 
35
45
  # Extracts the authorization token from the Authorization header
@@ -47,5 +47,12 @@ module JwtAuthenticable
47
47
  "Authorization error: #{@msg}"
48
48
  end
49
49
  end
50
+
51
+ # Exception to raise when 2fa enforce is enabled but user has not enabled 2fa
52
+ class TwoFANotEnabledError < StandardError
53
+ def message
54
+ '2FA must be enabled'
55
+ end
56
+ end
50
57
  end
51
58
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JwtAuthenticable
4
- VERSION = '1.0.1'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -11,6 +11,9 @@ module JwtAuthenticable
11
11
  # Note that for RSA algorithms this will actually be the public key
12
12
  setting :jwt_secret_key, default: nil
13
13
 
14
+ # If set to true, a jwt will only be considered valid if 2fa has been enabled
15
+ setting :enforce_2fa, default: false
16
+
14
17
  SUPPORTED_ALGOS = [JWT::Algos::Hmac, JWT::Algos::Rsa].freeze
15
18
 
16
19
  class Error < StandardError; end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestGem
4
- VERSION = '1.0.1'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: researchable_jwt-authenticable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Researchable
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-14 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -67,7 +67,7 @@ files:
67
67
  - node_modules/semantic-release-rubygem/src/__tests__/fixtures/prerelease/lib/test-gem/version.rb
68
68
  - node_modules/semantic-release-rubygem/src/__tests__/fixtures/valid/lib/test-gem/version.rb
69
69
  - sig/jwt_authenticable.rbs
70
- homepage: https://gitlab.com/researchable/general/gems/jwt-authenticable/-/blob/v1.0.1/README.md
70
+ homepage: https://gitlab.com/researchable/general/gems/jwt-authenticable/-/blob/v1.2.0/README.md
71
71
  licenses:
72
72
  - MIT
73
73
  metadata: