24.09.2019, 06:16
(15.07.2019, 13:27)admin Wrote: Send GET request to https://api.cp.dyson.com/v1/provisioning...e/manifest after the first one to get device list. You need to decode the first result using JSON and then pass Accountassword values as Basic auth. See last example in HTTP docs: http://w3.impa.br/~diego/software/luasocket/http.html
Yes i found why there was no return data. I have to use the v2 api for newer devices:
Code:
https = require 'ssl.https'
require('json')
socket.http.TIMEOUT = 5
local cUsername = 'xxxx@xxxxxx.com'
local cPassword = 'xxxxxxxx'
-- get session info
local cBody1 = '{"Email":"' .. cUsername .. '","Password":"' .. cPassword ..'"}'
local cReq1 = {}
local cUrl1 = 'https://api.cp.dyson.com/v1/userregistration/authenticate?country=NL'
result1 = https.request({
url = cUrl1,
method = 'POST',
headers = {
['content-length'] = #cBody1,
['content-type'] = 'application/json'
},
source = ltn12.source.string(cBody1),
sink = ltn12.sink.table(cReq1)
})
if result1 and cReq1 then
cReq1 = table.concat(cReq1)
cReq1 = json.pdecode(cReq1)
cAccount = nil
cPassword = nil
if cReq1 then
if cReq1.Account and cReq1.Password then
cAccount = cReq1.Account
cPassword = cReq1.Password
end
end
--log(cAccount,cPassword)
else
log(result1)
end
mime = require("mime")
local cAuth = "Basic " .. mime.b64(cAccount..":"..cPassword)
local cReq2 = {}
local cUrl2 = 'https://api.cp.dyson.com/v2/provisioningservice/manifest' -- v2 for new devices
result2, c, h = https.request({
url = cUrl2,
method = 'GET',
headers = {
['authorization'] = cAuth
},
sink = ltn12.sink.table(cReq2)
})
--log(result2,c,h)
log(cReq2)
But the next question, how to decode the password to use it in my mqtt client. Original code from https://github.com/CharlesBlonde/libpure...k/utils.py:
Code:
def decrypt_password(encrypted_password):
"""Decrypt password.
:param encrypted_password: Encrypted password
"""
key = b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10' \
b'\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f '
init_vector = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
b'\x00\x00\x00\x00'
cipher = AES.new(key, AES.MODE_CBC, init_vector)
json_password = json.loads(unpad(
cipher.decrypt(base64.b64decode(encrypted_password)).decode('utf-8')))
return json_password["apPasswordHash"]