diff --git a/modules/soc_eq/auth.py b/modules/soc_eq/auth.py index b8068c177..e22aefb04 100644 --- a/modules/soc_eq/auth.py +++ b/modules/soc_eq/auth.py @@ -1,83 +1,79 @@ #!/usr/bin/python3 -import requests, json, sys, time, os, html - -#call parameters -ChargePoint = str(sys.argv[1]) -code = str(sys.argv[2]) - -#Debug = int(os.environ.get('debug')) -Debug = 0 - -moddir = '/var/www/html/openWB/modules/soc_eq/' +import requests +import json +import sys +import time +import html +from pathlib import Path + +# call parameters +charge_point = sys.argv[1][-1] # get last Character to identify the Chargepoint +code = sys.argv[2] + +# debug = int(os.environ.get('debug')) +debug = 0 +base_dir = str(Path(__file__).resolve().parents[2]) +module_dir = str(Path(__file__).resolve().parents[0]) +client_id = "" +client_secret = "" +callback = "" def printDebug(message, level): - htmlmsg = html.escape(message) - if level <= Debug: - print("
" + htmlmsg + "
") + if level <= debug: + print("" + html.escape(message) + "
") def printHtml(message): - htmlmsg = html.escape(message) - print("" + htmlmsg + "
") - -print("") - -client_id = "" -client_secret = "" -callback = "" - -#get last Character to identify the Chargepoint -ChargePoint = ChargePoint[-1] - -#get SoC module config from openWB cofig -fd = open('/var/www/html/openWB/openwb.conf','r') -for line in fd: - try: - printDebug("owb Conf: " + line,2 ) - (key, val) = line.rstrip().split("=") - if key == "debug": - Debug = int(val) - if key == "soc_eq_client_id_lp" + str(ChargePoint): - printDebug("Found Client ID: " + val ,1) - client_id = val - if key == "soc_eq_client_secret_lp" + str(ChargePoint): - printDebug("Found Client Secret: " + val ,1) - client_secret = val - if key == "soc_eq_cb_lp" + str(ChargePoint): - printDebug("Found callback URL: " + val ,1) - callback = val.replace("'","") - except: - - val = "" - -fd.close() - -tok_url = "https://id.mercedes-benz.com/as/token.oauth2" + print("" + html.escape(message) + "
") + + +print("\n\n\n\n\n") + +# get SoC module config from openwb.conf +with open(base_dir + '/openwb.conf', 'r') as fd: + for line in fd: + try: + printDebug("owb Conf: " + line, 2) + (key, val) = line.rstrip().split("=") + if key == "debug": + debug = int(val) + if key == "soc_eq_client_id_lp" + str(charge_point): + printDebug("Found Client ID: " + val, 1) + client_id = val + if key == "soc_eq_client_secret_lp" + str(charge_point): + printDebug("Found Client Secret: " + val, 1) + client_secret = val + if key == "soc_eq_cb_lp" + str(charge_point): + printDebug("Found callback URL: " + val, 1) + callback = val.replace("'", "") + except Exception: + val = "" + +# tok_url = "https://id.mercedes-benz.com/as/token.oauth2" +tok_url = "https://ssoalpha.dvb.corpinter.net/v1/token" data = {'grant_type': 'authorization_code', 'code': str(code), 'redirect_uri': callback} -#call API to get Access/Refresh tokens +# call API to get Access/Refresh tokens act = requests.post(tok_url, data=data, verify=True, allow_redirects=False, auth=(client_id, client_secret)) -printDebug(act.url,1) +printDebug(act.url, 1) if act.status_code == 200: - #valid Response - toks = json.loads(act.text) - access_token = toks['access_token'] - refresh_token = toks['refresh_token'] - expires_in = int(time.time()) - - #write tokens to files + # valid Response + tokens = json.loads(act.text) + access_token = tokens['access_token'] + refresh_token = tokens['refresh_token'] + expires_in = tokens['expires_in'] - 60 + int(time.time()) - fd = open(moddir + 'soc_eq_acc_lp' + str(ChargePoint),'w') - json.dump({'expires_in' : expires_in, 'refresh_token' : refresh_token, 'access_token' : access_token}, fd) - fd.close() + # write tokens to files + with open(module_dir + '/' + 'soc_eq_acc_lp' + str(charge_point), 'w') as fd: + json.dump({'expires_in': expires_in, 'refresh_token': refresh_token, 'access_token': access_token}, fd) if act.status_code == 200: - printHtml( "Anmeldung erfolgreich!" ) - print( "Sie können das Fenster schließen." ) -else: + printHtml("Anmeldung erfolgreich!") + print("Sie können das Fenster schließen.") +else: printHtml("Anmeldung Fehlgeschlagen Code: " + str(act.status_code) + " " + act.text) -print("") +print("\n") diff --git a/modules/soc_eq/callback_lp.php b/modules/soc_eq/callback_lp.php index 53b5cc031..b54e20bea 100644 --- a/modules/soc_eq/callback_lp.php +++ b/modules/soc_eq/callback_lp.php @@ -1,7 +1,12 @@ "; diff --git a/modules/soc_eq/callback_lp1.php b/modules/soc_eq/callback_lp1.php index cd12438ea..c6ad17fa7 100644 --- a/modules/soc_eq/callback_lp1.php +++ b/modules/soc_eq/callback_lp1.php @@ -1,6 +1,14 @@ "; + echo "" . $_GET["error"] . "
"; + echo "" . $_GET["error_description"] . "
"; + echo "