Class: SMARTAppLaunch::ClientAssertionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_app_launch/client_assertion_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_auth_encryption_method:, iss:, sub:, aud:, exp: 5.minutes.from_now.to_i, jti: SecureRandom.hex(32), kid: nil, custom_jwks: nil) ⇒ ClientAssertionBuilder

Returns a new instance of ClientAssertionBuilder.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 23

def initialize(
  client_auth_encryption_method:,
  iss:,
  sub:,
  aud:,
  exp: 5.minutes.from_now.to_i,
  jti: SecureRandom.hex(32),
  kid: nil,
  custom_jwks: nil
)
  @client_auth_encryption_method = client_auth_encryption_method
  @iss = iss
  @sub = sub
  @aud = aud
  @content_type = content_type
  @grant_type = grant_type
  @client_assertion_type = client_assertion_type
  @exp = exp
  @jti = jti
  @kid = kid.presence
  @custom_jwks = custom_jwks
end

Instance Attribute Details

#audObject (readonly)

Returns the value of attribute aud.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def aud
  @aud
end

#client_assertion_typeObject (readonly)

Returns the value of attribute client_assertion_type.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def client_assertion_type
  @client_assertion_type
end

#client_auth_encryption_methodObject (readonly)

Returns the value of attribute client_auth_encryption_method.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def client_auth_encryption_method
  @client_auth_encryption_method
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def content_type
  @content_type
end

#custom_jwksObject (readonly)

Returns the value of attribute custom_jwks.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def custom_jwks
  @custom_jwks
end

#expObject (readonly)

Returns the value of attribute exp.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def exp
  @exp
end

#grant_typeObject (readonly)

Returns the value of attribute grant_type.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def grant_type
  @grant_type
end

#issObject (readonly)

Returns the value of attribute iss.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def iss
  @iss
end

#jtiObject (readonly)

Returns the value of attribute jti.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def jti
  @jti
end

#kidObject (readonly)

Returns the value of attribute kid.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def kid
  @kid
end

#subObject (readonly)

Returns the value of attribute sub.



11
12
13
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 11

def sub
  @sub
end

Class Method Details

.buildObject



7
8
9
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 7

def self.build(...)
  new(...).client_assertion
end

Instance Method Details

#client_assertionObject



80
81
82
83
84
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 80

def client_assertion
  @client_assertion ||=
    JWT.encode jwt_payload, signing_key, client_auth_encryption_method,
               { alg: client_auth_encryption_method, kid: key_id, typ: 'JWT' }
end

#jwksObject



46
47
48
49
50
51
52
53
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 46

def jwks
  @jwks ||=
    if custom_jwks.present?
      JWT::JWK::Set.new(JSON.parse(custom_jwks))
    else
      JWKS.jwks
    end
end

#jwt_payloadObject



63
64
65
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 63

def jwt_payload
  { iss:, sub:, aud:, exp:, jti: }.compact
end

#key_idObject



76
77
78
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 76

def key_id
  @private_key['kid']
end

#private_keyObject



55
56
57
58
59
60
61
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 55

def private_key
  @private_key ||=
    jwks
      .select { |key| key[:key_ops]&.include?('sign') }
      .select { |key| key[:alg] == client_auth_encryption_method }
      .find { |key| !kid || key[:kid] == kid }
end

#signing_keyObject



67
68
69
70
71
72
73
74
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 67

def signing_key
  if private_key.nil?
    raise Inferno::Exceptions::AssertionException,
          "No signing key found for inputs: encryption method = '#{client_auth_encryption_method}' and kid = '#{kid}'"
  end

  @private_key.signing_key
end