Class: Mi::Service::Account

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mi/service/account.rb

Constant Summary collapse

DEFAULT_COOKIES =
{
  "sdkVersion" => "3.9",
  "uLocale" => "zh_CN"
}.freeze
DEFAULT_HEADERS =
{
  "User-Agent" => "APP/com.xiaomi.mihome APPV/6.0.103 iosPassportSDK/3.9.0 iOS/14.4 miHSTS"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(userid, password, debug: false, info: {}) ⇒ Account

Returns a new instance of Account.



30
31
32
33
34
35
36
37
38
# File 'lib/mi/service/account.rb', line 30

def initialize(userid, password, debug: false, info: {})
  @userid = userid
  @password = password
  @debug = debug
  @success = false
  @info = info
  @miio = MiIO.new(self, debug: debug)
  @mina = Mina.new(self, debug: debug)
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



19
20
21
# File 'lib/mi/service/account.rb', line 19

def debug
  @debug
end

#infoObject (readonly)

Returns the value of attribute info.



19
20
21
# File 'lib/mi/service/account.rb', line 19

def info
  @info
end

#notification_urlObject (readonly)

Returns the value of attribute notification_url.



19
20
21
# File 'lib/mi/service/account.rb', line 19

def notification_url
  @notification_url
end

#passwordObject (readonly)

Returns the value of attribute password.



19
20
21
# File 'lib/mi/service/account.rb', line 19

def password
  @password
end

#useridObject (readonly)

Returns the value of attribute userid.



19
20
21
# File 'lib/mi/service/account.rb', line 19

def userid
  @userid
end

Instance Method Details

#auth_cookies(sid) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/mi/service/account.rb', line 154

def auth_cookies(sid)
  return {} if @info[sid].nil?

  {
    "userId" => @info["userId"],
    "serviceToken" => @info[sid][1]
  }
end

#authed_by_sid?(sid) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/mi/service/account.rb', line 167

def authed_by_sid?(sid)
  !@info[sid].nil?
end

#login(sid) ⇒ Object



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
# File 'lib/mi/service/account.rb', line 53

def (sid)
  device_id = @info["deviceId"] || Mi::Utils.get_random(16).upcase
   = (sid)

  unless (["code"]).zero?
    data = {
      _json: "true",
      qs: ["qs"],
      sid: ["sid"],
      _sign: ["_sign"],
      callback: ["callback"],
      user: userid,
      hash: md5_hash(password)
    }
     = (data)
    if ["notificationUrl"]
      @notification_url = "https://account.xiaomi.com#{login_response["notificationUrl"]}"
      raise "Please verify your account by visiting #{@notification_url}"
    end
  end

  if (["code"]).zero?
    @info = @info.merge .slice("userId", "passToken")
    @info["deviceId"] = device_id

    security_token_service(
      sid,
      ["location"],
      ["nonce"],
      ["ssecurity"]
    )
  else
    @success = false
  end
end

#login_allObject



43
44
45
46
# File 'lib/mi/service/account.rb', line 43

def 
  ("xiaomiio")
  ("micoapi")
end

#login_from_data(data) ⇒ Object



48
49
50
51
# File 'lib/mi/service/account.rb', line 48

def (data)
  @info = data
  @success = true
end

#security_token_service(sid, location, nonce, ssecurity) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mi/service/account.rb', line 132

def security_token_service(sid, location, nonce, ssecurity)
  cookies = DEFAULT_COOKIES
  headers = DEFAULT_HEADERS.merge({
                                    "Cookie" => Mi::Utils.cookie2str(cookies)
                                  })
  nsec = "nonce=#{nonce}&#{ssecurity}"
  client_sign = Base64.encode64(Digest::SHA1.digest(nsec)).chomp

  new_url = "#{location}&clientSign=#{URI.encode_uri_component(client_sign)}"
  client = Faraday.new do |faraday|
    faraday.headers = headers
    faraday.response :logger if debug
  end
  response = client.get(new_url)
  service_token = response.headers["set-cookie"][/serviceToken=([^;]+)/, 1]
  if service_token
    @info[sid] = [ssecurity, service_token]
    @success = true
  end
  response
end

#service_login(sid) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mi/service/account.rb', line 89

def (sid)
  cookies = DEFAULT_COOKIES
  if @info["passToken"]
    cookies = cookies.merge({ "userId" => @info["userId"],
                              "passToken" => @info["passToken"] })
  end

  headers = DEFAULT_HEADERS.merge({
                                    "Cookie" => Mi::Utils.cookie2str(cookies)
                                  })
  client = Faraday.new do |faraday|
    faraday.headers = headers
    faraday.use RequestLogger if debug
  end
  response = client.get(
    "https://account.xiaomi.com/pass/serviceLogin?sid=#{sid}&_json=true"
  )
  JSON.parse(response.body.sub("&&&START&&&", ""))
end

#service_login_auth2(data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mi/service/account.rb', line 109

def (data)
  cookies = DEFAULT_COOKIES
  if @info["passToken"]
    cookies = cookies.merge({ "userId" => @info["userId"],
                              "passToken" => @info["passToken"] })
  end

  headers = DEFAULT_HEADERS.merge({
                                    "Cookie" => Mi::Utils.cookie2str(cookies),
                                    "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"
                                  })
  client = Faraday.new do |faraday|
    faraday.request :url_encoded
    faraday.headers = headers
    faraday.use RequestLogger if debug
  end
  response = client.post(
    "https://account.xiaomi.com/pass/serviceLoginAuth2",
    data
  )
  JSON.parse(response.body.sub("&&&START&&&", ""))
end

#success?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/mi/service/account.rb', line 163

def success?
  @success
end