Class: ConnectorKit::TokenGenerator
- Inherits:
-
Object
- Object
- ConnectorKit::TokenGenerator
- Defined in:
- lib/connector_kit/token_generator.rb
Overview
Helper class for generating JWT tokens
Instance Method Summary collapse
- #generate_token ⇒ Object
-
#initialize(issuer_id, key_id, private_key_file_path) ⇒ TokenGenerator
constructor
A new instance of TokenGenerator.
Constructor Details
#initialize(issuer_id, key_id, private_key_file_path) ⇒ TokenGenerator
Returns a new instance of TokenGenerator.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/connector_kit/token_generator.rb', line 7 def initialize(issuer_id, key_id, private_key_file_path) @private_key_file_path = private_key_file_path @custom_headers = { kid: key_id, typ: 'JWT' } @payload = { iss: issuer_id, aud: 'appstoreconnect-v1' } end |
Instance Method Details
#generate_token ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/connector_kit/token_generator.rb', line 19 def generate_token private_key = File.read(@private_key_file_path) 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 |