Skip to content

Commit

Permalink
✨ [#390] Add endpoint to pass info to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Oct 11, 2024
1 parent 293eb2a commit a9c22a6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
3 changes: 2 additions & 1 deletion backend/src/openarchiefbeheer/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ReviewersView,
WhoAmIView,
)
from openarchiefbeheer.config.api.views import ArchiveConfigView
from openarchiefbeheer.config.api.views import ArchiveConfigView, OIDCInfoView
from openarchiefbeheer.destruction.api.views import ListStatusesListView
from openarchiefbeheer.destruction.api.viewsets import (
DestructionListItemReviewViewSet,
Expand Down Expand Up @@ -101,6 +101,7 @@
path(
"archive-config", ArchiveConfigView.as_view(), name="archive-config"
),
path("oidc-info", OIDCInfoView.as_view(), name="oidc-info"),
path(
"_retrieve_zaken/", CacheZakenView.as_view(), name="retrieve-zaken"
),
Expand Down
2 changes: 1 addition & 1 deletion backend/src/openarchiefbeheer/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,4 @@
# Django OIDC
#
OIDC_AUTHENTICATE_CLASS = "mozilla_django_oidc_db.views.OIDCAuthenticationRequestView"
OIDC_CALLBACK_CLASS = "mozilla_django_oidc_db.views.OIDCCallbackView"
OIDC_CALLBACK_CLASS = "mozilla_django_oidc_db.views.OIDCCallbackView"
29 changes: 29 additions & 0 deletions backend/src/openarchiefbeheer/config/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from django.utils.translation import gettext_lazy as _

from drf_spectacular.utils import extend_schema_field
from mozilla_django_oidc_db.models import OpenIDConnectConfig
from rest_framework import serializers
from rest_framework.reverse import reverse

from ..models import ArchiveConfig

Expand All @@ -21,3 +26,27 @@ class Meta:
"resultaattype": {"required": True, "allow_null": False},
"informatieobjecttype": {"required": True, "allow_null": False},
}


class OIDCInfoSerializer(serializers.ModelSerializer):
login_url = serializers.SerializerMethodField(
label=_("OIDC authentication URL"),
help_text=_(
"URL where to start the OIDC login flow if it is enabled. If it is not enabled, it will be an empty string."
),
)

class Meta:
model = OpenIDConnectConfig
fields = (
"enabled",
"login_url",
)

@extend_schema_field(serializers.URLField)
def get_login_url(self, config: OpenIDConnectConfig) -> str:
if not config.enabled:
return ""

request = self.context.get("request")
return reverse("oidc_authentication_init", request=request)
22 changes: 21 additions & 1 deletion backend/src/openarchiefbeheer/config/api/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.utils.translation import gettext_lazy as _

from drf_spectacular.utils import extend_schema
from mozilla_django_oidc_db.models import OpenIDConnectConfig
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from openarchiefbeheer.destruction.api.permissions import CanStartDestructionPermission

from ..models import ArchiveConfig
from .serializers import ArchiveConfigSerializer
from .serializers import ArchiveConfigSerializer, OIDCInfoSerializer


class ArchiveConfigView(APIView):
Expand Down Expand Up @@ -60,3 +62,21 @@ def put(self, request, *args, **kwargs) -> Response:
)
def patch(self, request, *args, **kwargs) -> Response:
return self.update(partial=True)


class OIDCInfoView(APIView):
authentication_classes = ()
permission_classes = ()

@extend_schema(
summary=_("Retrieve OIDC info"),
description=_("Returns info about OIDC that is needed by the frontend. "),
tags=["Configuration"],
responses={
200: OIDCInfoSerializer,
},
)
def get(self, request: Request, *args, **kwargs):
config = OpenIDConnectConfig.get_solo()
serializer = OIDCInfoSerializer(instance=config, context={"request": request})
return Response(serializer.data)
2 changes: 1 addition & 1 deletion backend/src/openarchiefbeheer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import include, path
from django.views.generic.base import TemplateView
from mozilla_django_oidc_db.views import AdminLoginFailure

from maykin_2fa import monkeypatch_admin
from maykin_2fa.urls import urlpatterns, webauthn_urlpatterns
from mozilla_django_oidc_db.views import AdminLoginFailure

from openarchiefbeheer.accounts.views.password_reset import PasswordResetView

Expand Down

0 comments on commit a9c22a6

Please sign in to comment.