Class: Cupertino::ProvisioningPortal::Agent

Inherits:
Mechanize
  • Object
show all
Defined in:
lib/download-profiles/provisioning_portal/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 14

def initialize
  super
  @profile_csrf_headers = {}
  self.user_agent_alias = 'Mac Safari'

  self.log ||= Logger.new(STDOUT)
  self.log.level = Logger::ERROR

  if ENV['HTTP_PROXY']
    uri = URI.parse(ENV['HTTP_PROXY'])
    user = ENV['HTTP_PROXY_USER'] if ENV['HTTP_PROXY_USER']
    password = ENV['HTTP_PROXY_PASSWORD'] if ENV['HTTP_PROXY_PASSWORD']

    set_proxy(uri.host, uri.port, user || uri.user, password || uri.password)
  end

  pw = Security::InternetPassword.find(:server => Cupertino::ProvisioningPortal::HOST)
  @username, @password = pw.attributes['acct'], pw.password if pw
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 12

def password
  @password
end

#teamObject

Returns the value of attribute team.



12
13
14
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 12

def team
  @team
end

#team_idObject

Returns the value of attribute team_id.



12
13
14
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 12

def team_id
  @team_id
end

#usernameObject

Returns the value of attribute username.



12
13
14
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 12

def username
  @username
end

Instance Method Details

#download_profile(profile) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 118

def download_profile(profile)
  ext = (profile.platform == :ios) ? 'mobileprovision' : 'provisionprofile'
  self.pluggable_parser.default = Mechanize::Download
  download = get(profile.download_url)
  download.save!(::File.expand_path("~/Library/MobileDevice/Provisioning Profiles/#{profile.identifier}.#{ext}"))
  download.filename
end

#get(uri, parameters = [], referer = nil, headers = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 41

def get(uri, parameters = [], referer = nil, headers = {})
  uri = ::File.join("https://#{Cupertino::ProvisioningPortal::HOST}", uri) unless /^https?/ === uri

  3.times do
    super(uri, parameters, referer, headers)

    return page unless page.respond_to?(:title)

    case page.title
    when /Sign in with your Apple ID/
      login!
    when /Select Team/
      select_team!
    else
      return page
    end
  end

  raise UnsuccessfulAuthenticationError
end

#list_profiles(platform, type) ⇒ Object



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
# File 'lib/download-profiles/provisioning_portal/agent.rb', line 62

def list_profiles(platform, type)
  url = case type
        when :development
          "https://developer.apple.com/account/#{platform}/profile/profileList.action?type=limited"
        when :distribution
          "https://developer.apple.com/account/#{platform}/profile/profileList.action?type=production"
        else
          raise ArgumentError, 'Provisioning profile type must be :development or :distribution'
        end

  self.pluggable_parser.default = Mechanize::File
  get(url)

  regex = /profileDataURL = "([^"]*)"/
  profile_data_url = (page.body.match regex or raise UnexpectedContentError)[1]

  profile_data_url += case type
                      when :development
                        '&type=limited'
                      when :distribution
                        '&type=production'
                      end
  
  profile_data_url += "&pageSize=50&pageNumber=1&sort=name=asc"
  
  post(profile_data_url)
  @profile_csrf_headers = {
    'csrf' => page.response['csrf'],
    'csrf_ts' => page.response['csrf_ts']
  }

  @profile_csrf_headers = {
    'csrf' => page.response['csrf'],
    'csrf_ts' => page.response['csrf_ts']
  }

  profile_data = page.content
  parsed_profile_data = JSON.parse(profile_data)

  profiles = []
  parsed_profile_data['provisioningProfiles'].each do |row|
    profile = ProvisioningProfile.new
    profile.name = row['name']
    profile.type = type
    profile.platform = platform
    profile.status = row['status']
    profile.expiration = (Time.parse(row['dateExpire']) rescue nil)
    profile.download_url = "https://developer.apple.com/account/#{platform}/profile/profileContentDownload.action?displayId=#{row['provisioningProfileId']}"
    profile.edit_url = "https://developer.apple.com/account/#{platform}/profile/profileEdit.action?provisioningProfileId=#{row['provisioningProfileId']}"
    profile.identifier = row['UUID']
    profiles << profile
  end

  profiles
end