|
1 | 1 | #!/usr/bin/env python3 |
2 | | -import base64 |
3 | 2 | import getpass |
4 | | -import hashlib |
5 | | -import hmac |
6 | | -import json |
7 | 3 | import sys |
8 | 4 |
|
9 | | -import boto3 |
10 | | -import requests |
11 | | - |
12 | | -# Those values have been obtained from the following files in SwitchBot Android app |
13 | | -# That's how you can verify them yourself |
14 | | -# /assets/switchbot_config.json |
15 | | -# /res/raw/amplifyconfiguration.json |
16 | | -# /res/raw/awsconfiguration.json |
17 | | -SWITCHBOT_INTERNAL_API_BASE_URL = ( |
18 | | - "https://l9ren7efdj.execute-api.us-east-1.amazonaws.com" |
19 | | -) |
20 | | -SWITCHBOT_COGNITO_POOL = { |
21 | | - "PoolId": "us-east-1_x1fixo5LC", |
22 | | - "AppClientId": "66r90hdllaj4nnlne4qna0muls", |
23 | | - "AppClientSecret": "1v3v7vfjsiggiupkeuqvsovg084e3msbefpj9rgh611u30uug6t8", |
24 | | - "Region": "us-east-1", |
25 | | -} |
| 5 | +from switchbot import SwitchbotLock |
26 | 6 |
|
27 | 7 |
|
28 | 8 | def main(): |
29 | 9 | if len(sys.argv) < 3: |
30 | 10 | print(f"Usage: {sys.argv[0]} <device_mac> <username> [<password>]") |
31 | 11 | exit(1) |
32 | 12 |
|
33 | | - device_mac = sys.argv[1].replace(":", "").replace("-", "").upper() |
34 | | - username = sys.argv[2] |
35 | 13 | if len(sys.argv) == 3: |
36 | 14 | password = getpass.getpass() |
37 | 15 | else: |
38 | 16 | password = sys.argv[3] |
39 | 17 |
|
40 | | - msg = bytes(username + SWITCHBOT_COGNITO_POOL["AppClientId"], "utf-8") |
41 | | - secret_hash = base64.b64encode( |
42 | | - hmac.new( |
43 | | - SWITCHBOT_COGNITO_POOL["AppClientSecret"].encode(), |
44 | | - msg, |
45 | | - digestmod=hashlib.sha256, |
46 | | - ).digest() |
47 | | - ).decode() |
48 | | - |
49 | | - cognito_idp_client = boto3.client( |
50 | | - "cognito-idp", region_name=SWITCHBOT_COGNITO_POOL["Region"] |
51 | | - ) |
52 | | - auth_response = None |
53 | 18 | try: |
54 | | - auth_response = cognito_idp_client.initiate_auth( |
55 | | - ClientId=SWITCHBOT_COGNITO_POOL["AppClientId"], |
56 | | - AuthFlow="USER_PASSWORD_AUTH", |
57 | | - AuthParameters={ |
58 | | - "USERNAME": username, |
59 | | - "PASSWORD": password, |
60 | | - "SECRET_HASH": secret_hash, |
61 | | - }, |
62 | | - ) |
63 | | - except cognito_idp_client.exceptions.NotAuthorizedException as e: |
64 | | - print(f"Error: Failed to authenticate - {e}") |
65 | | - exit(1) |
66 | | - except BaseException as e: |
67 | | - print(f"Error: Unexpected error during authentication - {e}") |
68 | | - exit(1) |
69 | | - |
70 | | - if ( |
71 | | - auth_response is None |
72 | | - or "AuthenticationResult" not in auth_response |
73 | | - or "AccessToken" not in auth_response["AuthenticationResult"] |
74 | | - ): |
75 | | - print(f"Error: unexpected authentication result") |
76 | | - exit(1) |
77 | | - |
78 | | - access_token = auth_response["AuthenticationResult"]["AccessToken"] |
79 | | - key_response = requests.post( |
80 | | - url=SWITCHBOT_INTERNAL_API_BASE_URL + "/developStage/keys/v1/communicate", |
81 | | - headers={"authorization": access_token}, |
82 | | - json={"device_mac": device_mac, "keyType": "user"}, |
83 | | - ) |
84 | | - key_response_content = json.loads(key_response.content) |
85 | | - if key_response_content["statusCode"] != 100: |
86 | | - print( |
87 | | - "Error: {} ({})".format( |
88 | | - key_response_content["message"], key_response_content["statusCode"] |
89 | | - ) |
90 | | - ) |
| 19 | + result = SwitchbotLock.retrieve_encryption_key(sys.argv[1], sys.argv[2], password) |
| 20 | + except RuntimeError as e: |
| 21 | + print(e) |
91 | 22 | exit(1) |
92 | 23 |
|
93 | | - print("Key ID: " + key_response_content["body"]["communicationKey"]["keyId"]) |
94 | | - print("Encryption key: " + key_response_content["body"]["communicationKey"]["key"]) |
| 24 | + print("Key ID: " + result["key_id"]) |
| 25 | + print("Encryption key: " + result["encryption_key"]) |
95 | 26 |
|
96 | 27 |
|
97 | 28 | if __name__ == "__main__": |
|
0 commit comments