mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-22 01:16:14 +00:00
add strong authentification
This commit is contained in:
+85
-76
@@ -1,15 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import xml.etree.ElementTree as ET
|
||||
import base64
|
||||
from androguard.core.bytecodes.apk import APK
|
||||
|
||||
import requests
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.serialization import pkcs12
|
||||
|
||||
from ChargeControl import ChargeControl, ChargeControls
|
||||
from MyPSACC import MyPSACC
|
||||
from sys import argv
|
||||
import tarfile
|
||||
import sys
|
||||
import traceback
|
||||
import re
|
||||
|
||||
|
||||
@@ -40,91 +41,99 @@ def find_preferences_xml():
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def save_key_to_pem(pfx_data, pfx_password):
|
||||
private_key, certificate, additional_certificates = pkcs12.load_key_and_certificates(pfx_data, bytes.fromhex(pfx_password))
|
||||
with open("public.pem", "wb") as f:
|
||||
f.write(certificate.public_bytes(encoding=serialization.Encoding.PEM))
|
||||
|
||||
with open("private.pem", "wb") as f:
|
||||
f.write(private_key.private_bytes(encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption()))
|
||||
|
||||
|
||||
current_dir = os.getcwd()
|
||||
script_dir = dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
if sys.version_info < (3, 6):
|
||||
raise RuntimeError("This application requres Python 3.6+")
|
||||
|
||||
if argv[1].endswith(".apk"):
|
||||
from androguard.core.bytecodes.apk import APK
|
||||
a = APK(argv[1])
|
||||
package_name = "com.psa.mym.mypeugeot"
|
||||
resources = a.get_android_resources() # .get_strings_resources()
|
||||
client_id = resources.get_string(package_name, "PSA_API_CLIENT_ID_PROD")[1]
|
||||
client_secret = resources.get_string(package_name, "PSA_API_CLIENT_SECRET_PROD")[1]
|
||||
remote_refresh_token = None
|
||||
customer_id = None
|
||||
|
||||
else:
|
||||
if len(argv) > 2:
|
||||
password = argv[2]
|
||||
else:
|
||||
password = ""
|
||||
res = os.system("java --version")
|
||||
if res != 0:
|
||||
print("You need to install java on your computer : https://www.java.com/fr/download/")
|
||||
exit(1)
|
||||
os.system(f"java -jar {script_dir}/abe-all.jar unpack {argv[1]} backup.tar {password}")
|
||||
my_tar = tarfile.open('backup.tar')
|
||||
my_tar.extractall()
|
||||
dir = find_app_path()
|
||||
os.chdir(dir + "/sp")
|
||||
# get client id/secret
|
||||
psa_pref = find_preferences_xml()
|
||||
root = ET.parse(psa_pref).getroot()
|
||||
client_secret = getxmlvalue(root, "CEA_CLIENT_SECRET")
|
||||
client_id = getxmlvalue(root, "CEA_CLIENT_ID")
|
||||
#get customer id
|
||||
remote_info_file = "BASIC_AUTH_CVS.xml"
|
||||
root = ET.parse(remote_info_file).getroot()
|
||||
customer_id_enc = getxmlvalue(root, "CRYPTED_CUSTOMER_ID")
|
||||
customer_id = base64.b64decode(customer_id_enc).decode('utf-8')
|
||||
#get remote token
|
||||
try:
|
||||
root = ET.parse("HUTokenManager.xml").getroot()
|
||||
except FileNotFoundError:
|
||||
traceback.print_exc()
|
||||
print("Do a remote request (start preconditioning for example) to generate a remote refresh token, "
|
||||
"make a new backup and relaunch this script")
|
||||
exit(1)
|
||||
remote_enc = root[0].text
|
||||
remote_dec = json.loads(base64.b64decode(remote_enc))
|
||||
try:
|
||||
if "refresh_token" in remote_dec:
|
||||
remote_refresh_token = remote_dec["refresh_token"]
|
||||
# Mypeugeot >= 1.26
|
||||
else:
|
||||
remote_refresh_token = next(iter(remote_dec.values()))["refresh_token"]
|
||||
except:
|
||||
traceback.print_exc()
|
||||
print(remote_dec)
|
||||
|
||||
if not argv[1].endswith(".apk"):
|
||||
print("No apk given")
|
||||
exit(1)
|
||||
print("APK loading...")
|
||||
a = APK(argv[1])
|
||||
package_name = a.get_package()
|
||||
resources = a.get_android_resources() # .get_strings_resources()
|
||||
client_id = resources.get_string(package_name, "PSA_API_CLIENT_ID_PROD")[1]
|
||||
client_secret = resources.get_string(package_name, "PSA_API_CLIENT_SECRET_PROD")[1]
|
||||
HOST_BRANDID_PROD = resources.get_string(package_name, "HOST_BRANDID_PROD")[1]
|
||||
pfx_cert = a.get_file("assets/MWPMYMA1.pfx")
|
||||
remote_refresh_token = None
|
||||
print("APK loaded !")
|
||||
|
||||
|
||||
client_email = input("mypeugeot email: ")
|
||||
client_paswword = input("mypeugeot password: ")
|
||||
client_realm = input("What is the car api realm : clientsB2CPeugeot, clientsB2CCitroen, clientsB2CDS, clientsB2COpel, clientsB2CVauxhall\n")
|
||||
client_password = input("mypeugeot password: ")
|
||||
client_realm = input("What is the car api realm : clientsB2CPeugeot, clientsB2CCitroen, clientsB2CDS, clientsB2COpel, "
|
||||
"clientsB2CVauxhall\n")
|
||||
country_code = input("What is your country code ? (ex: FR, GB, DE, ES...)\n")
|
||||
|
||||
## Customer id
|
||||
site_code="AP_"+country_code+"_ESP"
|
||||
res=requests.post(HOST_BRANDID_PROD+"/GetAccessToken",
|
||||
headers={
|
||||
"Connection": "Keep-Alive",
|
||||
"Content-Type": "application/json",
|
||||
"Host": "id-dcr.peugeot.com",
|
||||
"User-Agent": "okhttp/2.3.0"
|
||||
},
|
||||
params={"jsonRequest": json.dumps({"siteCode": site_code, "culture": "fr-FR", "action": "authenticate",
|
||||
"fields": {"USR_EMAIL": {"value": client_email},
|
||||
"USR_PASSWORD": {"value": client_password}}})
|
||||
}
|
||||
)
|
||||
|
||||
token = res.json()["accessToken"]
|
||||
save_key_to_pem(pfx_cert,"")
|
||||
|
||||
res2 = requests.post("https://mw-ap-m2c.mym.awsmpsa.com/api/v1/user?culture=fr_FR&width=1080&cgu=1611656517&v=1.27.0",
|
||||
data=json.dumps({"site_code":site_code, "ticket":token}),
|
||||
headers={
|
||||
"Connection": "Keep-Alive",
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
"Source-Agent": "App-Android",
|
||||
"Token": token,
|
||||
"User-Agent": "okhttp/4.8.0",
|
||||
"Version": "1.27.0"
|
||||
},
|
||||
cert=("public.pem","private.pem"),
|
||||
)
|
||||
res_dict = res2.json()["success"]
|
||||
customer_id = res_dict["dealers"]["apv"]["data"]["Brand"] + "-" + res_dict["id"]
|
||||
|
||||
|
||||
# Psacc
|
||||
|
||||
psacc = MyPSACC(None, client_id, client_secret, remote_refresh_token, customer_id, client_realm)
|
||||
psacc.connect(client_email, client_paswword)
|
||||
psacc.connect(client_email, client_password)
|
||||
|
||||
os.chdir(current_dir)
|
||||
psacc.save_config(name="test.json")
|
||||
res = psacc.get_vehicles()
|
||||
print(f"\nYour vehicles: {res}")
|
||||
|
||||
## Charge control
|
||||
charge_controls = ChargeControls()
|
||||
for vin, vehicle in res.items():
|
||||
chc = ChargeControl(None, vin, 100, [0, 0])
|
||||
charge_controls.list[vin] = chc
|
||||
charge_controls.save_config(name="charge_config1.json")
|
||||
|
||||
try:
|
||||
os.remove("private.pem")
|
||||
os.remove("public.pem")
|
||||
except:
|
||||
print("Error when deleting temp files")
|
||||
|
||||
if not argv[1].endswith(".apk"):
|
||||
try:
|
||||
os.remove("backup.tar")
|
||||
shutil.rmtree('apps')
|
||||
except:
|
||||
print("Error when deleting temp files")
|
||||
charge_controls = ChargeControls()
|
||||
for vin,vehicle in res.items():
|
||||
chc = ChargeControl(None,vin,100,[0,0])
|
||||
charge_controls.list[vin] = chc
|
||||
charge_controls.save_config(name="charge_config1.json")
|
||||
|
||||
|
||||
|
||||
print("Success !!!")
|
||||
Reference in New Issue
Block a user