Sha256: beff9210eabf9d8cc43306b65b5267ad2ec6800c2309cbc44e7fbfdc2375afd1

Contents?: true

Size: 1.32 KB

Versions: 156

Compression:

Stored size: 1.32 KB

Contents

require 'jwt'
require 'openssl'

# extract pem from .p8
# openssl pkcs8 -topk8 -outform PEM -in AuthKey.p8 -out key.pem -nocrypt

# compute public key
# openssl ec -in key.pem -pubout -out public_key.pem -aes256

module Spaceship
  class ConnectAPI
    class Token
      # maximum expiration supported by AppStore (20 minutes)
      MAX_TOKEN_DURATION = 1200

      attr_reader :key_id
      attr_reader :issuer_id
      attr_reader :text

      def self.create(key_id: nil, issuer_id: nil, filepath: nil)
        key_id ||= ENV['SPACESHIP_CONNECT_API_KEY_ID']
        issuer_id ||= ENV['SPACESHIP_CONNECT_API_ISSUER_ID']
        filepath ||= ENV['SPACESHIP_CONNECT_API_KEY_FILEPATH']

        self.new(
          key_id: key_id,
          issuer_id: issuer_id,
          key: OpenSSL::PKey::EC.new(File.read(filepath))
        )
      end

      def initialize(key_id: nil, issuer_id: nil, key: nil)
        @expiration = Time.now + MAX_TOKEN_DURATION
        @key_id = key_id
        @key = key
        @issuer_id = issuer_id

        header = {
          kid: key_id
        }

        payload = {
          iss: issuer_id,
          exp: @expiration.to_i,
          aud: 'appstoreconnect-v1'
        }

        @text = JWT.encode(payload, key, 'ES256', header)
      end

      def expired?
        @expiration < Time.now
      end
    end
  end
end

Version data entries

156 entries across 156 versions & 2 rubygems

Version Path
fastlane-2.156.1 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.156.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.155.3 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.155.2 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.155.1 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.155.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.154.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.153.1 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.153.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.152.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.151.2 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.151.1 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.151.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.3 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.2 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.1 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.0.rc7 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.0.rc6 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.150.0.rc5 spaceship/lib/spaceship/connect_api/token.rb