mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-12-10 05:37:46 +01:00
feat: distribute qrcode image over api/me/qrcode
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import base64
|
||||
import os
|
||||
from io import BytesIO
|
||||
import qrcode
|
||||
import pyotp
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework.generics import RetrieveAPIView
|
||||
from django.http.response import HttpResponse
|
||||
from rest_framework import viewsets, mixins
|
||||
|
||||
from .serializers import OAuthSerializer
|
||||
|
||||
|
||||
class UserInformationView(RetrieveAPIView):
|
||||
class UserInformationView(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
|
||||
"""
|
||||
These fields are give to OAuth authenticators.
|
||||
"""
|
||||
@@ -18,3 +24,11 @@ class UserInformationView(RetrieveAPIView):
|
||||
|
||||
def get_object(self):
|
||||
return self.request.user
|
||||
|
||||
def qrcode(self, request, *args, **kwargs):
|
||||
secret = base64.b32encode(os.getenv("DJANGO_SECRET_KEY").encode())
|
||||
qr_img = qrcode.make(f"{str(request.user.username)}{pyotp.TOTP(secret, interval=30).now()}")
|
||||
buffer = BytesIO()
|
||||
qr_img.save(buffer, format="PNG")
|
||||
buffer.seek(0)
|
||||
return HttpResponse(buffer, content_type="image/png")
|
||||
|
||||
Reference in New Issue
Block a user