Skip to content

Commit

Permalink
Enforce authmode
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed Jan 4, 2024
1 parent b809ce9 commit 433f43e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down
12 changes: 8 additions & 4 deletions pypowerwall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 433f43e

Please sign in to comment.