2024-12-22 00:07:17 -05:00
|
|
|
import json
|
|
|
|
|
2024-12-23 13:22:07 -05:00
|
|
|
from django.contrib.auth.models import User
|
2024-12-22 00:07:17 -05:00
|
|
|
from django.core.handlers.wsgi import WSGIRequest
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
2024-12-23 22:38:56 -05:00
|
|
|
from tauth.sessions import get_user
|
2024-12-22 00:07:17 -05:00
|
|
|
from tauth.settings import config
|
|
|
|
|
|
|
|
|
2024-12-23 13:22:07 -05:00
|
|
|
def get_username(request: WSGIRequest) -> HttpResponse:
|
2024-12-22 00:07:17 -05:00
|
|
|
try:
|
|
|
|
if request.GET.get("token") == config["services"][request.GET.get("service")]["token"]:
|
2024-12-23 22:38:56 -05:00
|
|
|
user = get_user(request)
|
2024-12-22 00:07:17 -05:00
|
|
|
return HttpResponse(
|
|
|
|
json.dumps({
|
|
|
|
"success": True,
|
2024-12-23 22:38:56 -05:00
|
|
|
"authenticated": user is not None,
|
|
|
|
"username": user and user.username
|
2024-12-22 00:07:17 -05:00
|
|
|
}),
|
|
|
|
content_type="application/json"
|
|
|
|
)
|
|
|
|
except KeyError:
|
|
|
|
...
|
|
|
|
|
|
|
|
return HttpResponse(
|
|
|
|
json.dumps({
|
|
|
|
"success": False
|
|
|
|
}),
|
|
|
|
content_type="application/json",
|
|
|
|
status=401
|
|
|
|
)
|
2024-12-23 13:22:07 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|