Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates authentification endpoint #25

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lowatt_grdf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@

from . import LOGGER, models

OPENID_ENDPOINT = "https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf"
ATraineauAKA marked this conversation as resolved.
Show resolved Hide resolved
OLD_AUTH_ENDPOINT = (
"https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf/access_token"
)
NEW_AUTH_ENDPOINT = (
"https://adict-connexion.grdf.fr/oauth2/aus5y2ta2uEHjCWIR417/v1/token"
)


def raise_for_status(resp: requests.Response) -> None:
Expand Down Expand Up @@ -83,12 +88,20 @@ def access_token(self) -> str:
if self._access_token is None or (
self._access_expires is not None and self._access_expires < time.time()
):
self._access_token, self._access_expires = self._authenticate()
self._access_token, self._access_expires = (
self._authenticate_with_fallback()
)
return self._access_token

def _authenticate(self) -> tuple[str, float]:
def _authenticate_with_fallback(self) -> tuple[str, float]:
try:
return self._authenticate(auth_endpoint=NEW_AUTH_ENDPOINT)
except requests.exceptions.HTTPError:
return self._authenticate(auth_endpoint=OLD_AUTH_ENDPOINT)

def _authenticate(self, auth_endpoint: str) -> tuple[str, float]:
resp = requests.post(
f"{OPENID_ENDPOINT}/access_token",
auth_endpoint,
data={
"grant_type": "client_credentials",
"client_id": self.client_id,
Expand Down Expand Up @@ -231,6 +244,9 @@ class StagingAPI(BaseAPI):
scope = "/adict/bas/v6"
api = "https://api.grdf.fr/adict/bas/v6"

def _authenticate_with_fallback(self) -> tuple[str, float]:
return self._authenticate(auth_endpoint=OLD_AUTH_ENDPOINT)

def _parse_response(self, resp: requests.Response) -> Any:
# XXX: Adjusts GRDF API responses to fit ndjson expected input because
# GRDF Staging API v6 responses contain multiple-lines JSON objects
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
def grdf() -> api.API:
responses.add(
responses.POST,
f"{api.OPENID_ENDPOINT}/access_token",
api.NEW_AUTH_ENDPOINT,
json={
"access_token": "xxx",
"expires_in": 14400,
Expand Down
Loading