Class: DaVinciCRDTestKit::JwtHelper
- Inherits:
-
Object
- Object
- DaVinciCRDTestKit::JwtHelper
- Defined in:
- lib/davinci_crd_test_kit/jwt_helper.rb
Instance Attribute Summary collapse
-
#aud ⇒ Object
readonly
Returns the value of attribute aud.
-
#encryption_method ⇒ Object
readonly
Returns the value of attribute encryption_method.
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
-
#iat ⇒ Object
readonly
Returns the value of attribute iat.
-
#iss ⇒ Object
readonly
Returns the value of attribute iss.
-
#jku ⇒ Object
readonly
Returns the value of attribute jku.
-
#jti ⇒ Object
readonly
Returns the value of attribute jti.
-
#kid ⇒ Object
readonly
Returns the value of attribute kid.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(aud:, encryption_method:, iss:, jku:, iat: Time.now.to_i, exp: 5.minutes.from_now.to_i, jti: SecureRandom.hex(32), kid: nil) ⇒ JwtHelper
constructor
A new instance of JwtHelper.
- #jwt_header ⇒ Object
- #jwt_payload ⇒ Object
- #key_id ⇒ Object
- #private_key ⇒ Object
- #signed_jwt ⇒ Object
- #signing_key ⇒ Object
Constructor Details
#initialize(aud:, encryption_method:, iss:, jku:, iat: Time.now.to_i, exp: 5.minutes.from_now.to_i, jti: SecureRandom.hex(32), kid: nil) ⇒ JwtHelper
Returns a new instance of JwtHelper.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 22 def initialize( aud:, encryption_method:, iss:, jku:, iat: Time.now.to_i, exp: 5.minutes.from_now.to_i, jti: SecureRandom.hex(32), kid: nil ) @aud = aud @encryption_method = encryption_method @iss = iss @jku = jku @iat = iat @exp = exp @jti = jti @kid = kid end |
Instance Attribute Details
#aud ⇒ Object (readonly)
Returns the value of attribute aud.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def aud @aud end |
#encryption_method ⇒ Object (readonly)
Returns the value of attribute encryption_method.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def encryption_method @encryption_method end |
#exp ⇒ Object (readonly)
Returns the value of attribute exp.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def exp @exp end |
#iat ⇒ Object (readonly)
Returns the value of attribute iat.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def iat @iat end |
#iss ⇒ Object (readonly)
Returns the value of attribute iss.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def iss @iss end |
#jku ⇒ Object (readonly)
Returns the value of attribute jku.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def jku @jku end |
#jti ⇒ Object (readonly)
Returns the value of attribute jti.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def jti @jti end |
#kid ⇒ Object (readonly)
Returns the value of attribute kid.
20 21 22 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 20 def kid @kid end |
Class Method Details
.build ⇒ Object
5 6 7 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 5 def self.build(...) new(...).signed_jwt end |
.decode_jwt(token, jwks_hash, kid = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 9 def self.decode_jwt(token, jwks_hash, kid = nil) jwks = JWT::JWK::Set.new(jwks_hash) jwks.filter! { |key| key[:use] == 'sig' } algorithms = jwks.map { |key| key[:alg] }.compact.uniq begin JWT.decode(token, kid, true, algorithms:, jwks:) rescue StandardError => e raise Inferno::Exceptions::AssertionException, e. end end |
Instance Method Details
#jwt_header ⇒ Object
58 59 60 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 58 def jwt_header { alg: encryption_method, typ: 'JWT', kid: key_id, jku: } end |
#jwt_payload ⇒ Object
62 63 64 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 62 def jwt_payload { iss:, aud:, exp:, iat:, jti: } end |
#key_id ⇒ Object
66 67 68 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 66 def key_id @private_key['kid'] end |
#private_key ⇒ Object
42 43 44 45 46 47 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 42 def private_key @private_key ||= JWKS.jwks .select { |key| key[:key_ops]&.include?('sign') } .select { |key| key[:alg] == encryption_method } .find { |key| !kid || key[:kid] == kid } end |
#signed_jwt ⇒ Object
70 71 72 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 70 def signed_jwt @signed_jwt ||= JWT.encode jwt_payload, signing_key, encryption_method, jwt_header end |
#signing_key ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 49 def signing_key if private_key.nil? raise Inferno::Exceptions::AssertionException, "No signing key found for inputs: encryption method = '#{encryption_method}' and kid = '#{kid}'" end @private_key.signing_key end |