Class: ConnectorKit::TokenGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/connector_kit/token_generator.rb

Overview

Helper class for generating JWT tokens

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issuer_id, key_id, private_key) ⇒ TokenGenerator

Returns a new instance of TokenGenerator.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/connector_kit/token_generator.rb', line 13

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

Class Method Details

.cretat_from_file(issuer_id, key_id, private_key_file_path) ⇒ Object



8
9
10
11
# File 'lib/connector_kit/token_generator.rb', line 8

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

Instance Method Details

#generate_tokenObject



25
26
27
28
29
30
31
32
33
# File 'lib/connector_kit/token_generator.rb', line 25

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