Class: Mi::Service::Account
- Inherits:
-
Object
- Object
- Mi::Service::Account
- 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
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#notification_url ⇒ Object
readonly
Returns the value of attribute notification_url.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#userid ⇒ Object
readonly
Returns the value of attribute userid.
Instance Method Summary collapse
- #auth_cookies(sid) ⇒ Object
- #authed_by_sid?(sid) ⇒ Boolean
-
#initialize(userid, password, debug: false, info: {}) ⇒ Account
constructor
A new instance of Account.
- #login(sid) ⇒ Object
- #login_all ⇒ Object
- #login_from_data(data) ⇒ Object
- #security_token_service(sid, location, nonce, ssecurity) ⇒ Object
- #service_login(sid) ⇒ Object
- #service_login_auth2(data) ⇒ Object
- #success? ⇒ Boolean
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
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
19 20 21 |
# File 'lib/mi/service/account.rb', line 19 def debug @debug end |
#info ⇒ Object (readonly)
Returns the value of attribute info.
19 20 21 |
# File 'lib/mi/service/account.rb', line 19 def info @info end |
#notification_url ⇒ Object (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 |
#password ⇒ Object (readonly)
Returns the value of attribute password.
19 20 21 |
# File 'lib/mi/service/account.rb', line 19 def password @password end |
#userid ⇒ Object (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 (sid) return {} if @info[sid].nil? { "userId" => @info["userId"], "serviceToken" => @info[sid][1] } end |
#authed_by_sid?(sid) ⇒ 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 login(sid) device_id = @info["deviceId"] || Mi::Utils.get_random(16).upcase login_response = service_login(sid) unless (login_response["code"]).zero? data = { _json: "true", qs: login_response["qs"], sid: login_response["sid"], _sign: login_response["_sign"], callback: login_response["callback"], user: userid, hash: md5_hash(password) } login_response = service_login_auth2(data) if login_response["notificationUrl"] @notification_url = "https://account.xiaomi.com#{login_response["notificationUrl"]}" raise "Please verify your account by visiting #{@notification_url}" end end if (login_response["code"]).zero? @info = @info.merge login_response.slice("userId", "passToken") @info["deviceId"] = device_id security_token_service( sid, login_response["location"], login_response["nonce"], login_response["ssecurity"] ) else @success = false end end |
#login_all ⇒ Object
43 44 45 46 |
# File 'lib/mi/service/account.rb', line 43 def login_all login("xiaomiio") login("micoapi") end |
#login_from_data(data) ⇒ Object
48 49 50 51 |
# File 'lib/mi/service/account.rb', line 48 def login_from_data(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) = DEFAULT_COOKIES headers = DEFAULT_HEADERS.merge({ "Cookie" => Mi::Utils.() }) 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 service_login(sid) = DEFAULT_COOKIES if @info["passToken"] = .merge({ "userId" => @info["userId"], "passToken" => @info["passToken"] }) end headers = DEFAULT_HEADERS.merge({ "Cookie" => Mi::Utils.() }) 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 service_login_auth2(data) = DEFAULT_COOKIES if @info["passToken"] = .merge({ "userId" => @info["userId"], "passToken" => @info["passToken"] }) end headers = DEFAULT_HEADERS.merge({ "Cookie" => Mi::Utils.(), "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
163 164 165 |
# File 'lib/mi/service/account.rb', line 163 def success? @success end |