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.130.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190829200051 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190828200015 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190827200051 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190826200016 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190825200030 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190824200021 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190823200017 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190822200101 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190821200034 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190820200024 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190819200109 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190818200034 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190817200018 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190816200015 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190815200057 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.130.0.beta.20190814200010 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.129.0 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.129.0.beta.20190813200016 spaceship/lib/spaceship/connect_api/token.rb
fastlane-2.129.0.beta.20190811200106 spaceship/lib/spaceship/connect_api/token.rb