core: add current attribute to authenticated_session API

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-05-30 00:31:21 +02:00
parent 45b8b1e198
commit 059da74d1c
2 changed files with 14 additions and 0 deletions

View File

@ -1,6 +1,8 @@
"""AuthenticatedSessions API Viewset"""
from guardian.utils import get_anonymous_user
from rest_framework import mixins
from rest_framework.fields import SerializerMethodField
from rest_framework.request import Request
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import GenericViewSet
@ -10,11 +12,19 @@ from authentik.core.models import AuthenticatedSession
class AuthenticatedSessionSerializer(ModelSerializer):
"""AuthenticatedSession Serializer"""
current = SerializerMethodField()
def get_current(self, instance: AuthenticatedSession) -> bool:
"""Check if session is currently active session"""
request: Request = self.context["request"]
return request._request.session.session_key == instance.session_key
class Meta:
model = AuthenticatedSession
fields = [
"uuid",
"current",
"user",
"last_ip",
"last_user_agent",

View File

@ -15569,6 +15569,9 @@ components:
uuid:
type: string
format: uuid
current:
type: boolean
readOnly: true
user:
type: integer
last_ip:
@ -15583,6 +15586,7 @@ components:
type: string
format: date-time
required:
- current
- last_ip
- last_used
- user