Class: NulogySSO::TestUtilities::JwtTestHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/nulogy_sso/test_utilities/jwt_test_helper.rb

Overview

Test utilities that revolve around the JWT (JSON Web Token) protocool. This class uses ruby-jwt (github.com/jwt/ruby-jwt) for JWT operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJwtTestHelper

Returns a new instance of JwtTestHelper.



12
13
14
15
16
17
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 12

def initialize
  @private_key = OpenSSL::PKey::RSA.new(
    File.read(File.expand_path("key.pem", __dir__))
  )
  @public_key = private_key.public_key
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



19
20
21
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 19

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



19
20
21
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 19

def public_key
  @public_key
end

Instance Method Details

#jwkObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 37

def jwk
  # Create JWK from public key
  base_jwk = JWT::JWK.new(public_key)
  base_jwk_hash = base_jwk.export
  kid = base_jwk_hash[:kid] || base_jwk_hash["kid"]

  # Create JWK with additional parameters for Auth0 compatibility
  JWT::JWK.new(
    public_key,
    {
      kid: kid,
      alg: "RS256",
      use: "sig",
      x5t: kid,
      x5c: [certificate_der]
    }
  )
end

#jwks_jsonObject



56
57
58
59
60
61
62
63
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 56

def jwks_json
  # Export the single JWK and wrap it in a keys array for JWKS format
  jwk_hash = jwk.export
  # Convert symbol keys to strings for JSON serialization if needed
  jwk_hash = jwk_hash.transform_keys(&:to_s) if jwk_hash.is_a?(Hash) && jwk_hash.keys.first.is_a?(Symbol)

  { keys: [jwk_hash] }.to_json
end

#jwt(email, overrides = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 21

def jwt(email, overrides = {})
  claim = {
    NulogySSO::JWT_EMAIL_KEY => email,
    "iss" => "#{NulogySSO.sso_config.base_uri}/",
    "sub" => "MOCK",
    "aud" => [NulogySSO.sso_config.audience],
    "exp" => (Time.now + 1.day).to_i
  }.merge(overrides)

  # Get the kid from the JWK
  jwk_hash = jwk.export
  kid = jwk_hash[:kid] || jwk_hash["kid"]

  JWT.encode(claim, private_key, "RS256", { kid: kid })
end