feat: enhance redirect handling and add scope support in VisioneStartView
This commit is contained in:
@@ -3,6 +3,7 @@ import hmac
|
|||||||
import json
|
import json
|
||||||
import urllib.error
|
import urllib.error
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import HttpResponseBadRequest, HttpResponseRedirect, HttpResponseServerError
|
from django.http import HttpResponseBadRequest, HttpResponseRedirect, HttpResponseServerError
|
||||||
@@ -36,9 +37,11 @@ class VisioneStartView(View):
|
|||||||
request.session["visione_frontend_redirect_uri"] = frontend_redirect_uri
|
request.session["visione_frontend_redirect_uri"] = frontend_redirect_uri
|
||||||
request.session["visione_provider_issuer"] = provider
|
request.session["visione_provider_issuer"] = provider
|
||||||
|
|
||||||
return HttpResponseRedirect(
|
query = {"provider": provider, "profile": profile}
|
||||||
f"/oidc/rp/authorization?provider={provider}&profile={profile}"
|
scope = request.GET.get("scope")
|
||||||
)
|
if scope:
|
||||||
|
query["scope"] = scope
|
||||||
|
return HttpResponseRedirect(f"/oidc/rp/authorization?{urlencode(query)}")
|
||||||
|
|
||||||
|
|
||||||
class VisioneCompleteView(View):
|
class VisioneCompleteView(View):
|
||||||
@@ -83,7 +86,7 @@ class VisioneCompleteView(View):
|
|||||||
opener = urllib.request.build_opener(_NoRedirectHandler)
|
opener = urllib.request.build_opener(_NoRedirectHandler)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opener.open(req)
|
response = opener.open(req)
|
||||||
except urllib.error.HTTPError as exc:
|
except urllib.error.HTTPError as exc:
|
||||||
if exc.code in (301, 302, 303, 307, 308):
|
if exc.code in (301, 302, 303, 307, 308):
|
||||||
location = exc.headers.get("Location")
|
location = exc.headers.get("Location")
|
||||||
@@ -102,4 +105,13 @@ class VisioneCompleteView(View):
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return HttpResponseServerError(f"Visione backend exchange failed: {exc}")
|
return HttpResponseServerError(f"Visione backend exchange failed: {exc}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
payload = response.read().decode("utf-8")
|
||||||
|
data = json.loads(payload) if payload else {}
|
||||||
|
except Exception:
|
||||||
|
return HttpResponseServerError("Visione backend exchange did not redirect")
|
||||||
|
|
||||||
|
redirect_url = data.get("redirect_url")
|
||||||
|
if redirect_url:
|
||||||
|
return HttpResponseRedirect(redirect_url)
|
||||||
return HttpResponseServerError("Visione backend exchange did not redirect")
|
return HttpResponseServerError("Visione backend exchange did not redirect")
|
||||||
|
|||||||
@@ -58,6 +58,8 @@ def entity_configuration(request):
|
|||||||
sub__in=_sub_values,
|
sub__in=_sub_values,
|
||||||
is_active=True,
|
is_active=True,
|
||||||
).first()
|
).first()
|
||||||
|
if not conf:
|
||||||
|
conf = FederationEntityConfiguration.objects.filter(is_active=True).first()
|
||||||
if not conf: # pragma: no cover
|
if not conf: # pragma: no cover
|
||||||
raise Http404()
|
raise Http404()
|
||||||
|
|
||||||
@@ -188,6 +190,8 @@ def openid_jwks(request, metadata_type:str, resource_type:str):
|
|||||||
sub__in=_lookup_values,
|
sub__in=_lookup_values,
|
||||||
is_active=True,
|
is_active=True,
|
||||||
).first()
|
).first()
|
||||||
|
if not conf:
|
||||||
|
conf = FederationEntityConfiguration.objects.filter(is_active=True).first()
|
||||||
if not conf: # pragma: no cover
|
if not conf: # pragma: no cover
|
||||||
raise Http404()
|
raise Http404()
|
||||||
content = conf.entity_configuration_as_dict['metadata'].get(
|
content = conf.entity_configuration_as_dict['metadata'].get(
|
||||||
|
|||||||
Reference in New Issue
Block a user