Class: Spaceship::ConnectAPI::Token

Inherits:
Object
  • Object
show all
Defined in:
spaceship/lib/spaceship/connect_api/token.rb

Constant Summary collapse

MAX_TOKEN_DURATION =

maximum expiration supported by AppStore (20 minutes)

1200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_id: nil, issuer_id: nil, key: nil, duration: nil, in_house: nil) ⇒ Token

Returns a new instance of Token.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 63

def initialize(key_id: nil, issuer_id: nil, key: nil, duration: nil, in_house: nil)
  @key_id = key_id
  @key = key
  @issuer_id = issuer_id
  @duration = duration
  @in_house = in_house

  @duration ||= MAX_TOKEN_DURATION
  @duration = @duration.to_i if @duration

  refresh!
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



19
20
21
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 19

def duration
  @duration
end

#expirationObject (readonly)

Returns the value of attribute expiration.



20
21
22
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 20

def expiration
  @expiration
end

#in_houseObject

Temporary attribute not needed to create the JWT text There is no way to determine if the team associated with this key is for App Store or Enterprise so this is the temporary workaround



25
26
27
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 25

def in_house
  @in_house
end

#issuer_idObject (readonly)

Returns the value of attribute issuer_id.



17
18
19
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 17

def issuer_id
  @issuer_id
end

#key_idObject (readonly)

Returns the value of attribute key_id.



16
17
18
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 16

def key_id
  @key_id
end

#textObject (readonly)

Returns the value of attribute text.



18
19
20
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 18

def text
  @text
end

Class Method Details

.create(key_id: nil, issuer_id: nil, filepath: nil, key: nil, duration: nil, in_house: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 42

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

  in_house_env = ENV['SPACESHIP_CONNECT_API_IN_HOUSE']
  in_house ||= !["", "no", "false", "off", "0"].include?(in_house_env) if in_house_env

  key ||= ENV['SPACESHIP_CONNECT_API_KEY']
  key ||= File.binread(filepath)

  self.new(
    key_id: key_id,
    issuer_id: issuer_id,
    key: OpenSSL::PKey::EC.new(key),
    duration: duration,
    in_house: in_house
  )
end

.from_json_file(filepath) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 27

def self.from_json_file(filepath)
  json = JSON.parse(File.read(filepath), { symbolize_names: true })

  missing_keys = []
  missing_keys << 'key_id' unless json.key?(:key_id)
  missing_keys << 'issuer_id' unless json.key?(:issuer_id)
  missing_keys << 'key' unless json.key?(:key)

  unless missing_keys.empty?
    raise "App Store Connect API key JSON is missing field(s): #{missing_keys.join(', ')}"
  end

  self.create(json)
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 92

def expired?
  @expiration < Time.now
end

#refresh!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 76

def refresh!
  @expiration = Time.now + @duration

  header = {
    kid: key_id
  }

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

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