Class: DaVinciCRDTestKit::JwtHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/davinci_crd_test_kit/jwt_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#audObject (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_methodObject (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

#expObject (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

#iatObject (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

#issObject (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

#jkuObject (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

#jtiObject (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

#kidObject (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

.buildObject



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.message
  end
end

Instance Method Details

#jwt_headerObject



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_payloadObject



62
63
64
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 62

def jwt_payload
  { iss:, aud:, exp:, iat:, jti: }
end

#key_idObject



66
67
68
# File 'lib/davinci_crd_test_kit/jwt_helper.rb', line 66

def key_id
  @private_key['kid']
end

#private_keyObject



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_jwtObject



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_keyObject



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