Sha256: 23582476a9c87ded66eaa72d632f0a6de2451bcd9e89548445fded4d072c68cf

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'jwt'

require_relative 'jwks'

module SMARTAppLaunch
  class ClientAssertionBuilder
    def self.build(...)
      new(...).client_assertion
    end

    attr_reader :aud,
                :client_assertion_type,
                :content_type,
                :client_auth_encryption_method,
                :exp,
                :grant_type,
                :iss,
                :jti,
                :sub

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

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

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

    def kid
      private_key.kid
    end

    def signing_key
      private_key.signing_key
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smart_app_launch_test_kit-0.4.0 lib/smart_app_launch/client_assertion_builder.rb
smart_app_launch_test_kit-0.3.0 lib/smart_app_launch/client_assertion_builder.rb