Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Unified Remote webclient password auth #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions custom_components/unified_remote/cli/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
from json import dumps
from uuid import uuid4
from hashlib import sha256

from requests import Session

Expand All @@ -16,6 +17,9 @@ def __init__(self):
# Creating a persistent http session
self.__session = Session()

self.web_user = ""
self.web_password = ""

def __del__(self):
self.__session.close()

Expand Down Expand Up @@ -54,10 +58,14 @@ def __set_headers(self):
and set it to request headers."""

response = self.__session.get(self.__url + "connect")
conn_id = response.json()["id"]
headers = {"UR-Connection-ID": conn_id}
self.__conn_id = response.json()["id"] # Session later
headers = {"UR-Connection-ID": self.__conn_id}
self.__headers = headers

def __encrypt_sha256(self, hash_str):
sha_sign = sha256(hash_str.encode()).hexdigest()
return sha_sign

def __autenticate(self):
"Do some server authentication to make connection persistent and stable."

Expand All @@ -70,9 +78,14 @@ def __autenticate(self):
"Platform": "web",
"Source": self.__source_guid,
}
self.__session.post(
response = self.__session.post(
self.__url + "request", headers=self.__headers, data=dumps(payload)
)
pw_uuid = response.json()["Password"]

# hash is SHA256 of PWUID + WEBPW + REQUID - Or - SHA256(user:pw)
hash_pw = self.__encrypt_sha256(f"{pw_uuid}{self.web_password}{password}")
#hash_pw = self.__encrypt_sha256("{self.web_user}:{self.web_password}")

payload = {
"Capabilities": {
Expand All @@ -85,6 +98,7 @@ def __autenticate(self):
},
"Action": 1,
"Request": 1,
"Password": hash_pw,
"Source": self.__source_guid,
}
self.__session.post(
Expand Down