Class: AppStoreDevApi::Client::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/app_store_dev_api/client/authorization.rb

Constant Summary collapse

OPTIONS =
%i[key_id issuer_id private_key].freeze
AUDIENCE =
'appstoreconnect-v1'
ALGORITHM =
'ES256'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Authorization

Returns a new instance of Authorization.



15
16
17
18
19
# File 'lib/app_store_dev_api/client/authorization.rb', line 15

def initialize(options)
  @key_id = options.fetch(:key_id)
  @issuer_id = options.fetch(:issuer_id)
  @private_key = OpenSSL::PKey.read(options.fetch(:private_key))
end

Instance Method Details

#header_fieldsObject



31
32
33
34
35
36
# File 'lib/app_store_dev_api/client/authorization.rb', line 31

def header_fields
  {
    kid: key_id,
    typ: 'JWT'
  }
end

#payloadObject



21
22
23
24
25
26
27
28
29
# File 'lib/app_store_dev_api/client/authorization.rb', line 21

def payload
  current_time = Time.now.to_i
  {
    iss: issuer_id,
    iat: current_time,
    exp: current_time + 20 * 60,
    aud: AUDIENCE
  }
end

#tokenObject



38
39
40
# File 'lib/app_store_dev_api/client/authorization.rb', line 38

def token
  JWT.encode(payload, private_key, ALGORITHM, header_fields)
end