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 is mostly a helpful wrapper around this gem: github.com/nov/json-jwt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJwtTestHelper

Returns a new instance of JwtTestHelper.



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

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.



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

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



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

def public_key
  @public_key
end

Instance Method Details

#jwkObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 35

def jwk
  base_jwk_params = public_key.to_jwk.to_h
  JSON::JWK.new(
    base_jwk_params.merge(
      x5t: base_jwk_params["kid"],
      alg: "RS256",
      use: "sig",
      x5c: [certificate_der]
    )
  )
end

#jwks_jsonObject



47
48
49
# File 'lib/nulogy_sso/test_utilities/jwt_test_helper.rb', line 47

def jwks_json
  JSON::JWK::Set.new(jwk).to_json
end

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



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

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)

  jwt = JSON::JWT.new(claim)
  jwt.header[:kid] = jwk["kid"]
  jwt = jwt.sign(private_key, :RS256)
  jwt.to_s
end