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) ⇒ ClientAssertionBuilder

Returns a new instance of ClientAssertionBuilder.



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

def initialize(
  client_auth_encryption_method:,
  iss:,
  sub:,
  aud:,
  exp: 5.minutes.from_now.to_i,
  jti: SecureRandom.hex(32),
  kid: 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
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

#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



66
67
68
69
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 66

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

#jwt_payloadObject



50
51
52
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 50

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

#key_idObject



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

def key_id
  @private_key['kid']
end

#private_keyObject



43
44
45
46
47
48
# File 'lib/smart_app_launch/client_assertion_builder.rb', line 43

def private_key
  @private_key ||= JWKS.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



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

def signing_key
  private_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
  return @private_key.signing_key
end