File: authorization.rb

package info (click to toggle)
ruby-app-store-connect 0.38.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 516 kB
  • sloc: ruby: 1,193; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 768 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

require 'jwt'

module AppStoreConnect
  class Client
    class Authorization
      OPTIONS = %i[key_id issuer_id private_key].freeze

      AUDIENCE = 'appstoreconnect-v1'
      ALGORITHM = 'ES256'

      attr_reader(*OPTIONS)

      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

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

      def header_fields
        { kid: key_id }
      end

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