Skip to content

Commit

Permalink
Merge pull request #108 from ecolabdata/fix/auth-wo-token
Browse files Browse the repository at this point in the history
fix: support auth w/o token
  • Loading branch information
streino authored Nov 7, 2024
2 parents e2903f0 + 13456e1 commit 35d6703
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 4 additions & 0 deletions isomorphe/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def login():
gn_info = migrator.gn.info()
except (requests.exceptions.RequestException, GeonetworkConnectionError) as e:
flash(f"Problème d'authentification ({e})", "error")
# still record the login info for the next try
session["url"] = url.rstrip("/")
session["username"] = username
session["password"] = password
return redirect(url_for("login_form"))
else:
authenticated = gn_info.get("me", {}).get("@authenticated", "false") == "true"
Expand Down
15 changes: 9 additions & 6 deletions isomorphe/geonetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ def authenticate(self):
raise GeonetworkConnectionError(
f"Redirection détectée vers {r.headers['Location']}. Merci d'utiliser l'URL canonique du serveur."
)
xsrf_token = r.cookies.get("XSRF-TOKEN")
if xsrf_token:
self.session.headers.update({"X-XSRF-TOKEN": xsrf_token})
log.debug(f"XSRF token: {xsrf_token}")
else:
raise GeonetworkConnectionError("Impossible de récupérer le token XSRF")
# if the POST above failed, we need the XSFR-TOKEN to proceed further
# if it did not, (username, password) basic auth should be enough
if not r.ok:
xsrf_token = r.cookies.get("XSRF-TOKEN")
if xsrf_token:
self.session.headers.update({"X-XSRF-TOKEN": xsrf_token})
log.debug("XSRF token found")
else:
raise GeonetworkConnectionError("Impossible de récupérer le token XSRF")

def _get_md_type(self, md: dict) -> MetadataType:
return MetadataType(md.get("isTemplate", MetadataType.METADATA))
Expand Down

0 comments on commit 35d6703

Please sign in to comment.