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, key_raw: nil, duration: nil, in_house: nil) ⇒ Token

Returns a new instance of Token.



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

def initialize(key_id: nil, issuer_id: nil, key: nil, key_raw: nil, duration: nil, in_house: nil)
  @key_id = key_id
  @key = key
  @key_raw = key_raw
  @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.



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

def duration
  @duration
end

#expirationObject (readonly)

Returns the value of attribute expiration.



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

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



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

def in_house
  @in_house
end

#issuer_idObject (readonly)

Returns the value of attribute issuer_id.



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

def issuer_id
  @issuer_id
end

#key_idObject (readonly)

Returns the value of attribute key_id.



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

def key_id
  @key_id
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Class Method Details

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 49

def self.create(key_id: nil, issuer_id: nil, filepath: nil, key: nil, is_key_content_base64: false, 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)

  if !key.nil? && is_key_content_base64
    key = Base64.decode64(key)
  end

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

.from(hash: nil, filepath: nil) ⇒ Object



28
29
30
31
32
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 28

def self.from(hash: nil, filepath: nil)
  api_token ||= self.create(**hash) if hash
  api_token ||= self.from_json_file(filepath) if filepath
  return api_token
end

.from_json_file(filepath) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 34

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)


105
106
107
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 105

def expired?
  @expiration < Time.now
end

#refresh!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 89

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

#write_key_to_file(path) ⇒ Object



109
110
111
# File 'spaceship/lib/spaceship/connect_api/token.rb', line 109

def write_key_to_file(path)
  File.open(path, 'w') { |f| f.write(@key_raw) }
end