Sha256: 07dce3db1b56a42610d2e31a4959f6c213397a7f0e8ac57650598632df1bc67a

Contents?: true

Size: 874 Bytes

Versions: 1

Compression:

Stored size: 874 Bytes

Contents

require 'openssl'
require 'jwt'

module AppstoreKitFork
  # Helper class for generating JWT tokens
  class TokenGenerator

    def self.cretat_from_file(issuer_id, key_id, private_key_file_path)
      private_key = File.read(@private_key_file_path)
      instance = TokenGenerator.new(issuer_id, key_id, private_key)
    end

    def initialize(issuer_id, key_id, private_key)
      @private_key = private_key
      @custom_headers = {
        kid: key_id,
        typ: 'JWT'
      }
      @payload = {
        iss: issuer_id,
        aud: 'appstoreconnect-v1'
      }
    end

    def generate_token
      ecdsa_key = OpenSSL::PKey.read(@private_key)
      expiration = Time.now.to_i + 20 * 60
      @payload[:exp] = expiration

      puts "Generated token expires: #{Time.at(expiration)}"

      JWT.encode(@payload, ecdsa_key, 'ES256', @custom_headers)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appstore_kit_fork-0.0.6 lib/appstore_kit_fork/token_generator.rb