Class: ICloud::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/icloud.rb

Overview

Your code goes hereā€¦

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, password) ⇒ Api

Returns a new instance of Api.



30
31
32
33
34
35
# File 'lib/icloud.rb', line 30

def initialize(, password)
  @login = 
  @password = password
  @auth = nil
  @instance = nil
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



28
29
30
# File 'lib/icloud.rb', line 28

def auth
  @auth
end

#instanceObject

Returns the value of attribute instance.



28
29
30
# File 'lib/icloud.rb', line 28

def instance
  @instance
end

#loginObject

Returns the value of attribute login.



28
29
30
# File 'lib/icloud.rb', line 28

def 
  @login
end

#passwordObject

Returns the value of attribute password.



28
29
30
# File 'lib/icloud.rb', line 28

def password
  @password
end

Instance Method Details

#appsObject



58
59
60
# File 'lib/icloud.rb', line 58

def apps
  Oj.load((@auth && @auth.body)||"{}")['apps']
end

#authenticate(remember_me = false) ⇒ Object



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
# File 'lib/icloud.rb', line 179

def authenticate(remember_me = false)
  @auth ||
    begin
      validate
      conn = Faraday.new(:url => ICloud::URLS[:authenticate])
      checksum = Digest::SHA1.hexdigest([, instance].join).upcase

      auth_params = {
      "apple_id" => ,
      "password" => password,
      "id" => checksum,
      "extended_login" => remember_me
    }

      @auth = conn.post do |req|
      req.url '/setup/ws/1/login'
      req.headers["Origin"] =  "https://www.icloud.com"
      req.headers["Referer"] = "https://www.icloud.com"
      req.headers["Cookie"]  = cookie
      req.params = { "clientBuildNumber" => client_build_number, "clientId" => client_id}
      req.body = Oj.dump(auth_params)
    end

      self.cookie = @auth.env[:response_headers]["set-cookie"]
      self.instance = Oj.load(@auth.body)['instance']

    end

  @auth
end

#authenticate?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/icloud.rb', line 175

def authenticate?
  !!@auth
end

#client_build_numberObject



37
38
39
# File 'lib/icloud.rb', line 37

def client_build_number
  version["buildNumber"]
end

#client_idObject



40
41
42
# File 'lib/icloud.rb', line 40

def client_id
  @uuid ||= UUID.new.generate.upcase
end

#contactsObject



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
# File 'lib/icloud.rb', line 210

def contacts
  authenticate unless authenticate?

  @contacts ||
    begin
      conn = Faraday.new(:url => webservices["contacts"]["url"] )
      checksum = Digest::SHA1.hexdigest([, instance].join).upcase

      resp = conn.get do |req|
      req.url '/co/startup'
      req.headers["Origin"] =  "https://www.icloud.com"
      req.headers["Referer"] = "https://www.icloud.com"
      req.headers["Cookie"]  = cookie
      req.params = {
        "clientBuildNumber" => client_build_number,
        "clientId" => client_id,
        "dsid" => ds_info['dsid'],
        "clientVersion" => '2.1',
        "locale" => 'en_US',
        "order" => 'last,first',
        "id" => checksum
      }
      end

      @contacts = Oj.load(resp.body)
    end

  @contacts
end


140
141
142
# File 'lib/icloud.rb', line 140

def cookie
  @cookie.to_s
end

#cookie=(v) ⇒ Object



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
# File 'lib/icloud.rb', line 68

def cookie=(v)

  @cookie = v.to_s.split(/,(?=[^;,]*=)|,$/).map { |c|
    cookie_elem = c.split(/;+/)
    first_elem = cookie_elem.shift
    first_elem.strip!
    key, value = first_elem.split(/\=/, 2)

    cookie = { 'name' =>key, 'value' =>  value.dup }


    cookie_elem.each do |pair|
      pair.strip!
      key, value = pair.split(/=/, 2)
      next unless key
      value = dequote(value.strip) if value

      case key.downcase
      when 'domain'
        next unless value && !value.empty?
        begin
          cookie['domain'] = value
          cookie['for_domain'] = true
        rescue
          puts ("Couldn't parse domain: #{value}")
        end
      when 'path'
        next unless value && !value.empty?
        cookie['path'] = value
      when 'expires'
        next unless value && !value.empty?
        begin
          cookie['expires'] = Time::parse(value)
        rescue
          puts "Couldn't parse expires: #{value}"
        end
      when 'max-age'
        next unless value && !value.empty?
        begin
          cookie['max_age'] = Integer(value)
        rescue
          puts "Couldn't parse max age '#{value}'"
        end
      when 'comment'
        next unless value
        cookie['comment'] = value
      when 'version'
        next unless value
        begin
          cookie['version'] = Integer(value)
        rescue
          puts "Couldn't parse version '#{value}'"
          cookie['version'] = nil
        end
      when 'secure'
        cookie['secure'] = true
      end
    end

    cookie['secure']  ||= false


    # RFC 6265 4.1.2.2
    cookie['expires']   = Time.now + cookie['max_age']  if cookie['max_age']
    cookie['session']   = !cookie['expires']

    # Move this in to the cookie jar
    cookie
  }.map{|j| "#{j['name']}=#{j['value']}"}.join(';')

end

#dequote(str) ⇒ Object



44
45
46
47
48
# File 'lib/icloud.rb', line 44

def dequote(str)
  ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
  ret.gsub!(/\\(.)/, "\\1")
  ret
end

#ds_infoObject



54
55
56
# File 'lib/icloud.rb', line 54

def ds_info
  Oj.load((@auth && @auth.body)||"{}")['dsInfo']
end

#validateObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/icloud.rb', line 158

def validate
  conn = Faraday.new(:url => URLS[:validate])

  resp = conn.post do |req|
    req.url '/setup/ws/1/validate'
    req.headers["Origin"] =  "https://www.icloud.com"
    req.headers["Referer"] = "https://www.icloud.com"
    req.headers["Cookie"]  = cookie
    req.params = { "clientBuildNumber" => client_build_number, "clientId" => client_id}
  end
  self.cookie = resp.env[:response_headers]["set-cookie"] if resp.env[:response_headers].has_key?("set-cookie")
  self.instance = Oj.load(resp.body)['instance']

  resp

end

#versionObject



153
154
155
156
# File 'lib/icloud.rb', line 153

def version
  @version_resp ||= Faraday.get(URLS[:version])
  Oj.load(@version_resp.body)
end

#webservicesObject



50
51
52
# File 'lib/icloud.rb', line 50

def webservices
  Oj.load((@auth && @auth.body)||"{}")['webservices']
end