Skip to content

Commit

Permalink
Working start prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
KiOui committed Sep 18, 2023
1 parent ac71c65 commit f432bfc
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 13 deletions.
4 changes: 2 additions & 2 deletions website/age/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@


urlpatterns = [
path("start/", YiviStartAPIView.as_view(), name="yivi_start"),
path("result/", YiviResultAPIView.as_view(), name="yivi_result"),
path("session/", YiviStartAPIView.as_view(), name="yivi_start"),
path("session/<str:pk>/result/", YiviResultAPIView.as_view(), name="yivi_result"),
]
12 changes: 10 additions & 2 deletions website/age/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.views import APIView

from age.models import SessionMapping
from age.services import get_yivi_client
from age.yivi import YiviException
from tosti.api.permissions import IsAuthenticatedOrTokenHasScopeForMethod
Expand Down Expand Up @@ -28,10 +30,14 @@ def post(self, request, **kwargs):
"disclose": [[["irma-demo.MijnOverheid.ageLower.over18"]]],
}
)
return Response(data=response)
except YiviException as e:
return Response(status=e.http_status, data=e.msg)

token = response["token"]
session_mapping = SessionMapping.objects.create(session_token=token)
response["token"] = session_mapping.id
return Response(data=response)


class YiviResultAPIView(APIView):
"""
Expand All @@ -48,7 +54,9 @@ class YiviResultAPIView(APIView):
def get(self, request, **kwargs):
"""Get the result of a Yivi session."""
yivi_client = get_yivi_client()
session_uuid = kwargs.get("pk")
session = get_object_or_404(SessionMapping, pk=session_uuid)
try:
return Response(data=yivi_client.session_status(""))
return Response(data=yivi_client.session_result(session.session_token))
except YiviException as e:
return Response(status=e.http_status, data=e.msg)
21 changes: 21 additions & 0 deletions website/age/migrations/0003_sessionmapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.4 on 2023-09-18 14:53

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
("age", "0002_alter_is18yearsold_user"),
]

operations = [
migrations.CreateModel(
name="SessionMapping",
fields=[
("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
("session_token", models.CharField(max_length=20)),
],
),
]
18 changes: 18 additions & 0 deletions website/age/migrations/0004_alter_sessionmapping_session_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.4 on 2023-09-18 14:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("age", "0003_sessionmapping"),
]

operations = [
migrations.AlterField(
model_name="sessionmapping",
name="session_token",
field=models.CharField(max_length=20, unique=True),
),
]
9 changes: 9 additions & 0 deletions website/age/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

from django.contrib.auth import get_user_model
from django.db import models

Expand All @@ -10,3 +12,10 @@ class Is18YearsOld(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="is_18_years_old")
created_at = models.DateTimeField(auto_now_add=True)


class SessionMapping(models.Model):
"""Session mapping class for Yivi."""

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
session_token = models.CharField(max_length=20, unique=True)
1 change: 1 addition & 0 deletions website/age/static/age/js/yivi.js

Large diffs are not rendered by default.

37 changes: 34 additions & 3 deletions website/age/templates/age/age_overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ <h2>Age check</h2>
<div class="alert alert-danger">
You have not been verified to be over 18 years old yet, please verify down below.
</div>
<button class="btn btn-primary mx-auto" data-bs-toggle="modal" data-bs-target="#verify-age-modal">Verify your age</button>
<button class="btn btn-primary mx-auto" data-bs-toggle="modal" data-bs-target="#verify-age-modal">Verify your age
</button>

<div class="modal" id="verify-age-modal" tabindex="-1" role="dialog" aria-labelledby="verify-age-modal-label" aria-hidden="true">
<div class="modal" id="verify-age-modal" tabindex="-1" role="dialog" aria-labelledby="verify-age-modal-label"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
Expand All @@ -20,9 +22,38 @@ <h3 class="modal-title">Verify your age</h3>
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body"></div>
<div class="modal-body">
<section class="yivi-web-form" id="yivi-web-form"></section>
</div>
<div class="modal-footer"></div>
</div>
</div>
</div>
<script src="{% static "age/js/yivi.js" %}" type="text/javascript"></script>
<script>
const yiviWeb = yivi.newWeb({
debugging: true,
element: '#yivi-web-form',
session: {
start: {
url: () => '{% url "v1:yivi_start" %}',
method: 'POST',
headers: {
"X-CSRFToken": get_csrf_token(),
},
},
result: {
url: (o, {sessionPtr, sessionToken}) => `/api/v1/age/session/${sessionToken}/result/`,
method: 'GET',
headers: {
"X-CSRFToken": get_csrf_token(),
},
}
}
});

yiviWeb.start()
.then(result => console.log("Successful disclosure! 🎉", result))
.catch(error => console.error("Couldn't do what you asked 😢", error));
</script>
{% endif %}
11 changes: 5 additions & 6 deletions website/age/yivi.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ def _patch(self, url, args=None, payload=None, **kwargs):

def start_session(self, payload):
"""Start a Yivi session."""
response = self._post("/session/", payload=payload)
# TODO: Save token somewhere.
token = response["token"]
print(token)
del response["token"]
return response
return self._post("/session/", payload=payload)

def session_status(self, token):
"""Get the session status of a session."""
return self._get(f"/session/{token}/status")

def session_result(self, token):
"""Get the session result."""
return self._get(f"/session/{token}/result")

0 comments on commit f432bfc

Please sign in to comment.