mirror of
https://github.com/makeplane/plane.git
synced 2026-07-13 14:01:45 +02:00
chore: mobile token session authentication endpoint (#1500)
* chore: mobile token session authentication endpoint * fix: error response --------- Co-authored-by: pablohashescobar <nikhilschacko@gmail.com>
This commit is contained in:
@@ -50,6 +50,8 @@ from .views import (
|
||||
MobileMagicSignInEndpoint,
|
||||
MobileSessionTokenCheckEndpoint,
|
||||
MobileSignOutAuthEndpoint,
|
||||
MobileSessionTokenEndpoint,
|
||||
MobileTokenEndpoint,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
@@ -290,4 +292,14 @@ urlpatterns = [
|
||||
MobileSignOutAuthEndpoint.as_view(),
|
||||
name="mobile-sign-out",
|
||||
),
|
||||
path(
|
||||
"mobile/session-token/",
|
||||
MobileSessionTokenEndpoint.as_view(),
|
||||
name="mobile-token",
|
||||
),
|
||||
path(
|
||||
"mobile/token/",
|
||||
MobileTokenEndpoint.as_view(),
|
||||
name="mobile-token",
|
||||
),
|
||||
]
|
||||
|
||||
@@ -45,7 +45,6 @@ from .app.saml import (
|
||||
from .app.signout import SignOutAuthEndpoint
|
||||
|
||||
|
||||
|
||||
from .space.email import SignInAuthSpaceEndpoint, SignUpAuthSpaceEndpoint
|
||||
|
||||
from .space.github import (
|
||||
@@ -86,5 +85,9 @@ from .app.password_management import (
|
||||
# Mobile web view authentication exports
|
||||
from .app.mobile.email import MobileSignInAuthEndpoint
|
||||
from .app.mobile.magic import MobileMagicSignInEndpoint
|
||||
from .app.mobile.token import MobileSessionTokenCheckEndpoint
|
||||
from .app.mobile.signout import MobileSignOutAuthEndpoint
|
||||
from .app.mobile.token import (
|
||||
MobileSessionTokenCheckEndpoint,
|
||||
MobileSessionTokenEndpoint,
|
||||
MobileTokenEndpoint,
|
||||
)
|
||||
from .app.mobile.signout import MobileSignOutAuthEndpoint
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
# Django imports
|
||||
from django.conf import settings
|
||||
|
||||
# Third party imports
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import AllowAny
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
|
||||
# Module imports
|
||||
from plane.authentication.utils.mobile.login import (
|
||||
ValidateAuthToken,
|
||||
)
|
||||
from plane.db.models import User
|
||||
from plane.authentication.utils.mobile.login import mobile_user_login
|
||||
|
||||
|
||||
class MobileSessionTokenCheckEndpoint(APIView):
|
||||
@@ -77,3 +82,50 @@ class MobileSessionTokenCheckEndpoint(APIView):
|
||||
{"error": "Something went wrong"},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
|
||||
class MobileTokenEndpoint(APIView):
|
||||
def get_tokens_for_user(self, user):
|
||||
# Get the refresh token
|
||||
refresh = RefreshToken.for_user(user)
|
||||
# Return the tokens
|
||||
return {
|
||||
"refresh_token": str(refresh),
|
||||
"access_token": str(refresh.access_token),
|
||||
}
|
||||
|
||||
def get(self, request):
|
||||
try:
|
||||
# Get the tokens
|
||||
response = self.get_tokens_for_user(user=request.user)
|
||||
# Return the tokens
|
||||
return Response(response, status=status.HTTP_200_OK)
|
||||
except Exception:
|
||||
return Response(
|
||||
{"error": "Something went wrong"},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
|
||||
class MobileSessionTokenEndpoint(APIView):
|
||||
authentication_classes = [
|
||||
JWTAuthentication,
|
||||
]
|
||||
|
||||
def post(self, request):
|
||||
try:
|
||||
# Get the user
|
||||
session = mobile_user_login(request=request, user=request.user)
|
||||
# Return the tokens
|
||||
return Response(
|
||||
{
|
||||
"session_name": settings.SESSION_COOKIE_NAME,
|
||||
"session_id": session.session_key,
|
||||
},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
except Exception:
|
||||
return Response(
|
||||
{"error": "Something went wrong"},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user