auth/tauth/views/templates.py

162 lines
5.3 KiB
Python
Raw Normal View History

2024-12-20 23:44:31 -05:00
import re
2024-12-22 00:07:17 -05:00
from django.contrib.auth import authenticate
2024-12-20 23:44:31 -05:00
from django.contrib.auth.models import User
2024-12-20 08:59:39 -05:00
from django.core.handlers.wsgi import WSGIRequest
2024-12-31 19:58:57 -05:00
from django.db import IntegrityError
2024-12-20 23:44:31 -05:00
from django.http import HttpResponse, HttpResponseRedirect
2024-12-20 08:59:39 -05:00
2024-12-23 13:22:07 -05:00
from tauth.models import tUser
2024-12-23 22:38:56 -05:00
from tauth.sessions import (clear_session, create_session, get_user,
is_authenticated)
2024-12-22 00:07:17 -05:00
from tauth.settings import config
2024-12-20 08:59:39 -05:00
from .helper import render_template
2024-12-23 13:22:07 -05:00
def auth(request: WSGIRequest) -> HttpResponseRedirect:
resp = HttpResponseRedirect("/")
if "remove" in request.GET:
2024-12-31 19:58:57 -05:00
resp.set_cookie("session_id", "", max_age=0)
2024-12-23 13:22:07 -05:00
else:
2024-12-23 22:38:56 -05:00
resp.set_cookie("session_id", request.GET.get("sessionid") or "")
2024-12-23 13:22:07 -05:00
return resp
2024-12-20 08:59:39 -05:00
def index(request: WSGIRequest) -> HttpResponse:
2024-12-23 22:38:56 -05:00
u = get_user(request)
if u:
2024-12-20 23:44:31 -05:00
return render_template(
request, "index.html",
2024-12-23 22:38:56 -05:00
username=u.username
2024-12-20 23:44:31 -05:00
)
2024-12-22 00:07:17 -05:00
2024-12-20 08:59:39 -05:00
return render_template(
2024-12-23 22:38:56 -05:00
request, "noauth/index.html"
2024-12-20 08:59:39 -05:00
)
def signup(request: WSGIRequest) -> HttpResponse:
2024-12-23 22:38:56 -05:00
if is_authenticated(request):
2024-12-20 23:44:31 -05:00
return HttpResponseRedirect("/")
2024-12-23 13:22:07 -05:00
if config["new_users"]:
2024-12-20 23:44:31 -05:00
if request.method == "POST":
username = (request.POST.get("username") or "").lower().strip()
password = (request.POST.get("password") or "")
error = None
if len(username) < 2 or len(username) > 30 or not re.match("^[a-z0-9_][a-z0-9_\\-]{0,28}[a-z0-9_]$", username):
error = "Invalid username"
elif len(password) < 6 or len(password) > 100 or not (any([i.isalpha() for i in password]) and any([i.isnumeric() for i in password]) and any([not i.isalnum() for i in password])):
error = "Invalid password"
2024-12-23 13:22:07 -05:00
elif username.lower() in password.lower():
error = "Password can't contain username"
2024-12-20 23:44:31 -05:00
else:
try:
u = User.objects.create_user(
username=username,
password=password
)
2024-12-31 19:58:57 -05:00
except IntegrityError:
2024-12-20 23:44:31 -05:00
error = "Username already in use"
else:
2024-12-23 13:22:07 -05:00
tUser.objects.create(user=u)
2024-12-22 00:07:17 -05:00
to = request.GET.get("to")
if to and to in config["services"] and config["services"][to]:
2024-12-23 22:38:56 -05:00
resp = HttpResponseRedirect(f"/redirect/?to={to}&reauth")
else:
resp = HttpResponseRedirect("/")
2024-12-22 00:07:17 -05:00
2024-12-23 22:38:56 -05:00
create_session(request, resp, u)
return resp
2024-12-20 23:44:31 -05:00
return render_template(
request, "noauth/signup.html",
title="Sign Up",
error=error,
2024-12-22 00:07:17 -05:00
login_extra=f"?to={request.GET.get('to')}" if "to" in request.GET else "",
2024-12-20 23:44:31 -05:00
repopulate={
"username": username
}
)
2024-12-20 08:59:39 -05:00
return render_template(
request, "noauth/signup.html",
2024-12-22 00:07:17 -05:00
title="Sign Up",
login_extra=f"?to={request.GET.get('to')}" if "to" in request.GET else ""
2024-12-20 08:59:39 -05:00
)
return render_template(
request, "404.html"
)
def login(request: WSGIRequest) -> HttpResponse:
2024-12-23 22:38:56 -05:00
if is_authenticated(request):
2024-12-22 00:07:17 -05:00
to = request.GET.get("to")
2024-12-23 22:38:56 -05:00
if to and to in config["services"] and config["services"][to]:
2024-12-22 00:07:17 -05:00
return HttpResponseRedirect(f"/redirect/?to={to}&reauth")
return HttpResponseRedirect("/")
if request.method == "POST":
username = (request.POST.get("username") or "").lower().strip()
password = (request.POST.get("password") or "")
user = authenticate(
request,
username=username,
password=password
)
if user is None:
return render_template(
request, "noauth/login.html",
title="Log In",
2024-12-23 13:22:07 -05:00
new_users=config["new_users"],
2024-12-22 00:07:17 -05:00
error="Username or password is incorrect",
login_extra=f"?to={request.GET.get('to')}" if "to" in request.GET else "",
repopulate={
"username": username
}
)
to = request.GET.get("to")
if to and to in config["services"] and config["services"][to]:
2024-12-23 22:38:56 -05:00
resp = HttpResponseRedirect(f"/redirect/?to={to}&reauth")
else:
resp = HttpResponseRedirect("/")
2024-12-22 00:07:17 -05:00
2024-12-23 22:38:56 -05:00
create_session(request, resp, user)
return resp
2024-12-22 00:07:17 -05:00
2024-12-20 08:59:39 -05:00
return render_template(
request, "noauth/login.html",
2024-12-22 00:07:17 -05:00
title="Log In",
2024-12-23 13:22:07 -05:00
new_users=config["new_users"],
2024-12-22 00:07:17 -05:00
login_extra=f"?to={request.GET.get('to')}" if "to" in request.GET else ""
2024-12-20 08:59:39 -05:00
)
2024-12-22 00:07:17 -05:00
def redirect(request: WSGIRequest) -> HttpResponseRedirect:
to = request.GET.get("to")
if to and to in config["services"] and config["services"][to]:
2024-12-23 22:38:56 -05:00
return HttpResponseRedirect(config["services"][to]["url"]["pub"] + (f"/auth/?sessionid={request.COOKIES.get('session_id')}" if "reauth" in request.GET else ""))
2024-12-22 00:07:17 -05:00
return HttpResponseRedirect("/")
2024-12-23 13:22:07 -05:00
def logout(request: WSGIRequest) -> HttpResponseRedirect:
2024-12-23 22:38:56 -05:00
clear_session(request)
2024-12-23 13:22:07 -05:00
to = request.GET.get("to")
if to and to in config["services"] and config["services"][to]:
return HttpResponseRedirect(config["services"][to]["url"]["pub"] + "/auth/?remove")
return HttpResponseRedirect("/")