auth/tauth/views/api.py
2024-12-23 22:38:56 -05:00

60 lines
1.6 KiB
Python

import json
from django.contrib.auth.models import User
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from tauth.sessions import get_user
from tauth.settings import config
def get_username(request: WSGIRequest) -> HttpResponse:
try:
if request.GET.get("token") == config["services"][request.GET.get("service")]["token"]:
user = get_user(request)
return HttpResponse(
json.dumps({
"success": True,
"authenticated": user is not None,
"username": user and user.username
}),
content_type="application/json"
)
except KeyError:
...
return HttpResponse(
json.dumps({
"success": False
}),
content_type="application/json",
status=401
)
def username_exists(request: WSGIRequest) -> HttpResponse:
try:
if request.GET.get("token") == config["services"][request.GET.get("service")]["token"]:
exists = True
try:
User.objects.get(username=request.GET.get("username"))
except User.DoesNotExist:
exists = False
return HttpResponse(
json.dumps({
"success": True,
"exists": exists
}),
content_type="application/json"
)
except KeyError:
...
return HttpResponse(
json.dumps({
"success": False
}),
content_type="application/json",
status=401
)