Module: Zaikio::JWTAuth::TestHelper

Defined in:
lib/zaikio/jwt_auth/test_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.jwkObject



4
5
6
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 4

def self.jwk
  @jwk ||= JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), { kid: "test-kid", use: "sig", alg: "RS256" })
end

.jwk_setObject



8
9
10
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 8

def self.jwk_set
  @jwk_set ||= JWT::JWK::Set.new(jwk).export
end

Instance Method Details

#after_teardownObject



14
15
16
17
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 14

def after_teardown
  Zaikio::JWTAuth.mocked_jwt_payload = nil
  super
end

#generate_token_payload(params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 32

def generate_token_payload(params)
  {
    iss: "ZAI",
    sub: nil,
    aud: %w[test_app],
    jti: SecureRandom.uuid,
    nbf: Time.now.to_i,
    exp: 1.hour.from_now.to_i,
    jku: "http://hub.zaikio.test/api/v1/jwt_public_keys.json",
    scope: []
  }.merge(params).stringify_keys
end

#issue_mock_jwt_token(params) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 23

def issue_mock_jwt_token(params)
  JWT.encode(
    generate_token_payload(params),
    jwk.signing_key,
    jwk[:alg],
    kid: jwk[:kid]
  )
end

#jwkObject



45
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 45

def jwk = Zaikio::JWTAuth::TestHelper.jwk

#mock_jwt(params) ⇒ Object



19
20
21
# File 'lib/zaikio/jwt_auth/test_helper.rb', line 19

def mock_jwt(params)
  Zaikio::JWTAuth.mocked_jwt_payload = generate_token_payload(params)
end