-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Due to breaking changes introduced by v2 for oAuth support, some hacking things done with depreciation warnings supporting backwards compatibility. When using secrets version will default to V2 which is not yet tested / implemented for all endpoints. Will be supported in the coming PRs in non-breaking way as possible. This will resolve #dogmatic69/nordigen-homeassistant/7 once the HA integration is updated
- Loading branch information
1 parent
c78becd
commit 1855cbb
Showing
17 changed files
with
384 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.python | ||
*egg* | ||
client.py | ||
examples/client.py | ||
**/__pycache__ | ||
.coverage | ||
htmlcov | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.2 | ||
0.2.0b1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
from nordigen.client import AccountClient, AgreementsClient, AspspsClient, RequisitionsClient | ||
import warnings | ||
|
||
from apiclient import HeaderAuthentication, NoAuthentication | ||
|
||
from nordigen.client import AccountClient, AgreementsClient, AspspsClient, AuthClient, RequisitionsClient | ||
from nordigen.oauth import OAuthAuthentication | ||
|
||
|
||
def Client(token=None, request_strategy=None, secret_id=None, secret_key=None, version=None): | ||
if token: | ||
warnings.warn("Use Client(secret_id=xxx, secret_key=xxx) instead of token", DeprecationWarning) | ||
|
||
if not token and (not secret_id or not secret_key): | ||
raise ValueError("secret_id and secret_key must be provided") | ||
|
||
def Client(token, request_strategy=None): | ||
def instance(): | ||
return instance | ||
|
||
instance.aspsps = AspspsClient(token=token, request_strategy=request_strategy) | ||
instance.agreements = AgreementsClient(token=token, request_strategy=request_strategy) | ||
instance.account = AccountClient(token=token, request_strategy=request_strategy) | ||
instance.requisitions = RequisitionsClient(token=token, request_strategy=request_strategy) | ||
auth = HeaderAuthentication(scheme="Token", token=token) | ||
if not token: | ||
version = "v2" if not version else version | ||
auth = OAuthAuthentication( | ||
body={ | ||
"secret_id": secret_id, | ||
"secret_key": secret_key, | ||
}, | ||
client=AuthClient(auth=NoAuthentication(), request_strategy=request_strategy, version=version), | ||
) | ||
|
||
instance.aspsps = AspspsClient(auth=auth, request_strategy=request_strategy, version=version) | ||
instance.agreements = AgreementsClient(auth=auth, request_strategy=request_strategy, version=version) | ||
instance.account = AccountClient(auth=auth, request_strategy=request_strategy, version=version) | ||
instance.requisitions = RequisitionsClient(auth=auth, request_strategy=request_strategy, version=version) | ||
|
||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from time import time | ||
|
||
from apiclient.authentication_methods import BaseAuthenticationMethod | ||
|
||
|
||
class OAuthAuthentication(BaseAuthenticationMethod): | ||
"""Authentication using secret_id and secret_key.""" | ||
|
||
_access_token = None | ||
_token_expiration = None | ||
_refresh_token = None | ||
_refresh_expiration = None | ||
|
||
def __init__( | ||
self, | ||
body, | ||
client, | ||
expiry_margin=10, | ||
): | ||
"""Initialize OAuthAuthentication.""" | ||
self._client = client | ||
self._body = body | ||
self._expiry_margin = expiry_margin | ||
|
||
def get_headers(self): | ||
self.refresh_token() | ||
return { | ||
"Authorization": f"Bearer {self._access_token}", | ||
} | ||
|
||
def refresh_token(self): | ||
if self._token_expiration and self._token_expiration >= int(time()): | ||
return True | ||
|
||
if self._refresh_expiration and self._refresh_expiration >= int(time()): | ||
ret = self._client.refresh(refresh_token=self._refresh_token) | ||
self._access_token = ret.get("access") | ||
self._token_expiration = int(time()) + int(ret.get("access_expires")) - self._expiry_margin | ||
return True | ||
|
||
ret = self._client.token(**self._body) | ||
self._access_token = ret.get("access") | ||
self._refresh_token = ret.get("refresh") | ||
self._token_expiration = int(time()) + int(ret.get("access_expires")) - self._expiry_margin | ||
self._refresh_expiration = int(time()) + int(ret.get("refresh_expires")) - self._expiry_margin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[tool.black] | ||
line-length=120 | ||
py36=true | ||
py36=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.