From 433f43ebd33faeeac9e626c11b0e120eeb2fef70 Mon Sep 17 00:00:00 2001 From: Jason Cox Date: Wed, 3 Jan 2024 21:10:56 -0800 Subject: [PATCH] Enforce authmode --- RELEASE.md | 4 ++++ pypowerwall/__init__.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 50e6b05..c00f977 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -2,6 +2,7 @@ ## v0.7.4 - Bearer Token Auth +pyPowerwall Updates * This release adds the ability to use a Bearer Token for Authentication for the local Powerwall gateway API calls. This is selectable by defining `authmode='token'` in the initialization. The default mode uses the existing `AuthCookie` and `UserRecord` method. ```python @@ -10,6 +11,9 @@ import pypowerwall pw = pypowerwall.Powerwall(HOST, PASSWORD, EMAIL, TIMEZONE, authmode="token") ``` +Proxy +* The above option is extended to the pyPowerwall Proxy via the envrionmental variable `PW_AUTH_MODE` set to cookie (default) or token. + Powerwall Network Scanner * Added optional IP address argument to network scanner by @mcbirse in https://github.com/jasonacox/pypowerwall/pull/63. The Scan Function can now accept an additional argument `-ip=` to override the host IP address detection (`python -m pypowerwall scan -ip=192.168.1.100`). This may be useful where the host IP address/network cannot be detected correctly, for instance if pypowerwall is running inside a container. diff --git a/pypowerwall/__init__.py b/pypowerwall/__init__.py index 0eb0145..69bdd83 100644 --- a/pypowerwall/__init__.py +++ b/pypowerwall/__init__.py @@ -163,6 +163,10 @@ def __init__(self, host="", password="", email="nobody@nowhere.com", else: # Disable http persistent connections self.session = requests + # Enforce authmode + if self.authmode not in ['cookie', 'token']: + log.debug("Invalid value for parameter 'authmode' (%s) switching to default" % str(self.authmode)) + self.authmode = 'cookie' # Load cached auth session try: f = open(self.cachefile, "r") @@ -273,7 +277,7 @@ def poll(self, api='/api/site_info/site_name', jsonformat=False, raw=False, recu if(fetch): if(api == '/api/devices/vitals'): - # Always want the raw output from the vitals call; protobuf binary payload + # Always want the raw stream output from the vitals call; protobuf binary payload raw = True url = "https://%s%s" % (self.host, api) @@ -282,7 +286,6 @@ def poll(self, api='/api/site_info/site_name', jsonformat=False, raw=False, recu r = self.session.get(url, headers=self.auth, verify=False, timeout=self.timeout, stream=raw) else: r = self.session.get(url, cookies=self.auth, verify=False, timeout=self.timeout, stream=raw) - except requests.exceptions.Timeout: log.debug('ERROR Timeout waiting for Powerwall API %s' % url) return None @@ -294,14 +297,15 @@ def poll(self, api='/api/site_info/site_name', jsonformat=False, raw=False, recu return None if r.status_code >= 400 and r.status_code < 500: # Session Expired - Try to get a new one unless we already tried + log.debug('Session Expired - Trying to get a new one') if(not recursive): if raw: - # Drain the stream + # Drain the stream before retrying payload = r.raw.data self._get_session() return self.poll(api, jsonformat, raw, True) else: - log.debug('ERROR Unable to establish session with Powerwall at %s - check password' % url) + log.error('Unable to establish session with Powerwall at %s - check password' % url) return None if(raw): payload = r.raw.data