feat: enhance redirect handling and add scope support in VisioneStartView
This commit is contained in:
@@ -3,6 +3,7 @@ import hmac
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from django.conf import settings
|
||||
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_provider_issuer"] = provider
|
||||
|
||||
return HttpResponseRedirect(
|
||||
f"/oidc/rp/authorization?provider={provider}&profile={profile}"
|
||||
)
|
||||
query = {"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):
|
||||
@@ -83,7 +86,7 @@ class VisioneCompleteView(View):
|
||||
opener = urllib.request.build_opener(_NoRedirectHandler)
|
||||
|
||||
try:
|
||||
opener.open(req)
|
||||
response = opener.open(req)
|
||||
except urllib.error.HTTPError as exc:
|
||||
if exc.code in (301, 302, 303, 307, 308):
|
||||
location = exc.headers.get("Location")
|
||||
@@ -102,4 +105,13 @@ class VisioneCompleteView(View):
|
||||
except Exception as 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")
|
||||
|
||||
Reference in New Issue
Block a user