forked from lacostej/iOSprovisioningprofiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple_dev_center.rb
executable file
·416 lines (367 loc) · 13 KB
/
apple_dev_center.rb
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
39
40
41
42
43
44
45
46
47
48
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/ruby
require 'rubygems'
require 'optparse'
require 'mechanize'
require 'json'
require 'yaml'
require 'encrypted_strings'
INSTALL_DIR = File.dirname($0)
USAGE = "Usage: #{File.basename($0)} [-d [DIR]] [-u login] [-p password] [-T team] [-O file] [-C config][-S secret_key] [-n] [-h]"
class Profile
attr_accessor :uuid, :blobId, :type, :name, :appid, :statusXcode, :downloadUrl
def to_json(*a)
{
'uuid' => uuid,
'type' => type,
'name' => name,
'appid' => appid,
'statusXcode' => statusXcode,
'blobId' => blobId,
'downloadUrl' => downloadUrl
}.to_json(*a)
end
end
class Certificate
attr_accessor :displayId, :type, :name, :exp_date, :profile, :status, :downloadUrl
def to_json(*a)
{
'displayId' => displayId,
'type' => type,
'name' => name,
'exp_date' => exp_date,
'status' => status,
'profile' => profile,
'downloadUrl' => downloadUrl
}.to_json(*a)
end
end
class Device
attr_accessor :udid, :name
def to_json(*a)
{
'udid' => udid,
'name' => name
}.to_json(*a)
end
end
def info(message)
puts message
end
def parse_config(options)
config = YAML::load_file(options[:configFile])
login_to_fetch = options[:login]
if login_to_fetch.nil?
login_to_fetch = config['default']
options[:login] = login_to_fetch
end
account = config['accounts'].select { |a| a['login'] == login_to_fetch }[0]
secret_key = options[:secretKey].nil? ? "" : options[:secretKey]
encrypted = account['password']
decrypted = encrypted.decrypt(:symmetric, :password => secret_key)
options[:passwd] = decrypted
end
def parse_command_line(args)
options = {}
options[:profileFileName] = :uuid
OptionParser.new { |opts|
opts.banner = USAGE
opts.on( '-u', '--user USER', 'the apple developer store login') do |login|
options[:login] = login
end
opts.on( '-p', '--password PASSWORD', 'the apple developer store login') do |passwd|
options[:passwd] = passwd
end
opts.on( '-n', '--name', 'use the profile name instead of its UUID as basename when saving them') do
options[:profileFileName] = :name
end
opts.on( '-T', '--team TEAMID', 'the identifier for the team that you are wanting to query') do |team|
options[:team] = team.nil? ? "" : team
end
opts.on( '-d', '--dump [DIR]', 'dumps the site content as JSON format (to the optional specified directory, that will be created if non existent)') do |dir|
options[:dump] = true
options[:dumpDir] = dir.nil? ? "." : dir
if not File.exists?(options[:dumpDir])
Dir.mkdir(options[:dumpDir])
end
end
opts.on( '-S', '--seed SEED', 'the secret_key for the config file if required') do |secret_key|
options[:secretKey] = secret_key.nil? ? "" : secret_key
end
opts.on( '-C', '--config FILE', 'fetch password (and optionally default user) information from the specified config file, with the optional secret_key') do |config_file, secret_key|
options[:configFile] = config_file
if not File.exists?(options[:configFile])
raise OptionParser::InvalidArgument, "Specified '#{config_file}'file doesn't exists"
end
end
opts.on( '-O', '--output FILE', 'writes output to the specified file. Uses standard output otherwise') do |output|
options[:output] = output
end
opts.on( '', '--device-uuid UUID', 'UUID to use for adding a device') do |output|
options[:device_uuid] = output
end
opts.on( '', '--device-name DEVICE_NAME', 'Device Name') do |output|
options[:device_name] = output
end
opts.on( '', '--add-device', 'Adds a device to the provisioning portal') do |output|
options[:add_device] = true
end
opts.on( '', '--profile-name NAME', 'The name of the profile you wanna add interact with') do |output|
options[:profile_name] = output
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
}.parse!(args)
parse_config(options) unless options[:configFile].nil?
options
end
def dump(text, file)
if (file)
File.open(file, 'w') { |f| f.write(text) }
else
puts text
end
end
class AppleDeveloperCenter
def initialize
@agent = Mechanize.new
@profileUrls = {}
@profileUrls[:development] = "https://developer.apple.com/ios/manage/provisioningprofiles/index.action"
@profileUrls[:distribution] = "https://developer.apple.com/ios/manage/provisioningprofiles/viewDistributionProfiles.action"
@certificateUrls = {}
@certificateUrls[:development] = "https://developer.apple.com/ios/manage/certificates/team/index.action"
@certificateUrls[:distribution] = "https://developer.apple.com/ios/manage/certificates/team/distribute.action"
@devicesUrl = "https://developer.apple.com/ios/manage/devices/index.action"
@deviceAddUrl = "https://developer.apple.com/ios/manage/devices/add.action"
@editProfileUrl = "https://developer.apple.com/ios/manage/provisioningprofiles/edit.action?provDisplayId="
end
def load_page_or_login(url, options)
#info "Loading #{url}"
page = @agent.get(url)
# Log in to Apple Developer portal if we're presented with a login form
form = page.form_with :name => 'appleConnectForm'
if form
info("Found login page, logging in now")
form.theAccountName = options[:login]
form.theAccountPW = options[:passwd]
form.submit
page = select_team(url, options)
end
page
end
def select_team(url, options)
page = @agent.get(url)
form = page.form_with :name => 'saveTeamSelection'
if form
teams_selector = form.field_with :name => 'memberDisplayId'
teams_selector.options.each { |option|
if option.to_s == options[:team]
teams_selector.value = options[:team]
puts 'Selected Team: ' + options[:team]
break
end
}
continueButton = form.button_with :value => 'Continue'
form.click_button(continueButton)
page = @agent.get(url)
end
page
end
def read_profiles(page, type)
profiles = []
# Format each row as name,udid
rows = page.parser.xpath('//fieldset[@id="fs-0"]/table/tbody/tr')
rows.each do |row|
p = Profile.new()
p.blobId = row.at_xpath('td[@class="checkbox"]/input/@value')
p.type = type
next if row.at_xpath('td[@class="profile"]/a/span').nil?
p.name = row.at_xpath('td[@class="profile"]/a/span').text
p.appid = row.at_xpath('td[@class="appid"]/text()')
p.statusXcode = row.at_xpath('td[@class="statusXcode"]').text.strip.split("\n")[0]
p.downloadUrl = row.at_xpath('td[@class="action"]/a/@href')
profiles << p
end
profiles
end
def read_all_profiles(options)
all_profiles = []
@profileUrls.each { |type, url|
info("Fetching #{type} profiles")
page = load_page_or_login(url, options)
all_profiles.concat(read_profiles(page, type))
}
all_profiles
end
def read_certificates_distribution(page, type)
certs = []
# Format each row as name,udid
rows = page.parser.xpath('//div[@class="nt_multi"]/table/tbody/tr')
rows.each do |row|
last_elt = row.at_xpath('td[@class="action last"]')
if last_elt.nil?
msg_elt = row.at_xpath('td[@colspan="4"]/span')
if !msg_elt.nil?
info("-->#{msg_elt.text}")
end
next
end
next if last_elt.at_xpath('form').nil?
c = Certificate.new()
# :displayId, :type, :name, :exp_date, :profiles, :status, :downloadUrl
c.downloadUrl = last_elt.at_xpath('a/@href')
c.displayId = c.downloadUrl.to_s.split("certDisplayId=")[1]
c.type = type
c.name = row.at_xpath('td[@class="name"]/a').text
c.exp_date = row.at_xpath('td[@class="expdate"]').text.strip
# unsure if one certificate can be mapped to several profiles
c.profile = row.at_xpath('td[@class="profile"]').text.strip
c.status = row.at_xpath('td[@class="status"]').text.strip
certs << c
end
certs
end
def read_certificates_development(page, type)
certs = []
# Format each row as name,udid
rows = page.parser.xpath('//div[@class="nt_multi"]/table/tbody/tr')
rows.each do |row|
last_elt = row.at_xpath('td[@class="last"]')
next if last_elt.at_xpath('form').nil?
c = Certificate.new()
# :displayId, :type, :name, :exp_date, :profiles, :status, :downloadUrl
c.downloadUrl = last_elt.at_xpath('a/@href')
c.displayId = c.downloadUrl.to_s.split("certDisplayId=")[1]
c.type = type
c.name = row.at_xpath('td[@class="name"]/div/p').text
c.exp_date = row.at_xpath('td[@class="date"]').text.strip
# unsure if one certificate can be mapped to several profiles
c.profile = row.at_xpath('td[@class="profiles"]').text.strip
c.status = row.at_xpath('td[@class="status"]').text.strip
certs << c
end
certs
end
def read_all_certificates(options)
all_certs = []
info("Fetching development certificates")
page = load_page_or_login(@certificateUrls[:development], options)
all_certs.concat(read_certificates_development(page, :development))
info("Fetching distribution certificates")
page = load_page_or_login(@certificateUrls[:distribution], options)
all_certs.concat(read_certificates_distribution(page, :distribution))
all_certs
end
def read_devices(options)
info("Fetching devices")
page = load_page_or_login(@devicesUrl, options)
devices = []
rows = page.parser.xpath('//fieldset[@id="fs-0"]/table/tbody/tr')
rows.each do |row|
d = Device.new()
d.name = row.at_xpath('td[@class="name"]/span/text()')
d.udid = row.at_xpath('td[@class="id"]/text()')
devices << d
end
devices
end
def add_device_uuid(device_name,device_uuid,options)
info("Fetching add device page")
page = load_page_or_login(@deviceAddUrl, options)
# Log in to Apple Developer portal if we're presented with a login form
form = page.form_with :name => 'add'
if form
info("Found add device form. Adding device")
form['deviceNameList[0]'] = device_name
form['deviceNumberList[0]'] = device_uuid
end
#TODO: Check device added successuflly
info("Device added successfully")
end
def update_provisioning_profile(profile_name,options)
profiles = read_all_profiles(options)
profile = profiles.select {|p| p.name == profile_name}.first
if profile
page = load_page_or_login(@editProfileUrl + profile.blobId,options)
form = page.form_with :name => 'save'
if form
form.checkboxes_with(:name => 'selectedDevices').each{|box|box.check}
form.submit
info("Submitting form with form data")
end
#TODO: Check success state of the form
end
end
def fetch_site_data(options)
site = {}
site[:devices] = read_devices(options)
site[:profiles] = read_all_profiles(options)
site[:certificates] = read_all_certificates(options)
download_profiles(site[:profiles], options[:dumpDir], options[:profileFileName])
download_certificates(site[:certificates], options[:dumpDir])
site
end
# return the uuid of the specified mobile provisioning file
def pp_uuid(ppfile)
# FIXME extract script into a reusable ruby library
uuid = `bundle exec ruby #{INSTALL_DIR}/mobileprovisioning.rb #{ppfile} -d UUID`
# strip trailing \n
uuid = uuid[0..-2]
uuid
end
def download_profiles(profiles, dumpDir, profileFileName)
profiles.each do |p|
filename = "#{dumpDir}/#{p.blobId}.mobileprovision"
info("Downloading profile #{p.blobId} '#{p.name}'")
@agent.download(p.downloadUrl, filename)
uuid = pp_uuid filename
p.uuid = uuid
if profileFileName == :uuid
basename = p.uuid
else
basename = p.name.gsub(/ /, '_').gsub(/'/, '')
end
newfilename = "#{dumpDir}/#{basename}.mobileprovision"
File.rename(filename, "#{newfilename}")
info("Saved profile #{p.uuid} '#{p.name}' in #{newfilename}")
end
end
def download_certificates(certs, dumpDir)
certs.each do |c|
filename = "#{dumpDir}/#{c.displayId}.cer"
info("Downloading cert #{c.displayId} -#{c.type}- '#{c.name}'")
@agent.download(c.downloadUrl, filename)
info("Saved cert #{c.displayId} '#{c.name}' in #{filename}")
end
end
end
def dumpSite(options)
@ADC = AppleDeveloperCenter.new()
site = @ADC.fetch_site_data(options)
text = site.to_json
dump(text, options[:output])
end
def addDeviceUUID(options)
@ADC = AppleDeveloperCenter.new()
#site = @ADC.add_device_uuid(options[:device_name],options[:device_uuid],options)
if options[:profile_name]
@ADC.update_provisioning_profile(options[:profile_name],options)
end
end
def main()
begin
options = parse_command_line(ARGV)
rescue OptionParser::ParseError => e
puts "Invalid argument: #{e}"
puts "#{USAGE}"
exit 1
end
if (options[:dump])
dumpSite(options)
end
if (options[:add_device])
addDeviceUUID(options)
end
end
main()