diff --git a/.gitignore b/.gitignore index c18dd8d..7213079 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__/ +auth.sqlite3 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5f5d25f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "cSpell.words": [ + "noauth", + "noscript", + "stylesheet", + "tauth", + "TCOMMON" + ] +} diff --git a/config.py b/config.py index 7f340fe..d46a695 100644 --- a/config.py +++ b/config.py @@ -1,8 +1,4 @@ ALLOW_NEW_USERS = True -DEBUG = True -ENABLED_APPLICATIONS = { - "search": False, - "music": False, - "messages": False, - "info": False -} + +tCOMMON_URL_INTERNAL = "http://localhost:8888" +tCOMMON_TOKEN = "Secret tCommon-specific token" diff --git a/tauth/apps.py b/tauth/apps.py new file mode 100644 index 0000000..37524ca --- /dev/null +++ b/tauth/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class DBConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "tauth" diff --git a/tauth/migrations/0001_initial.py b/tauth/migrations/0001_initial.py new file mode 100644 index 0000000..a441369 --- /dev/null +++ b/tauth/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0.7 on 2024-12-21 04:40 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='TUser', + fields=[ + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/tauth/migrations/__init__.py b/tauth/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tauth/models.py b/tauth/models.py new file mode 100644 index 0000000..11542d8 --- /dev/null +++ b/tauth/models.py @@ -0,0 +1,6 @@ +from django.contrib.auth.models import User +from django.db import models + + +class TUser(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) diff --git a/tauth/settings.py b/tauth/settings.py index 5e71183..d5651ba 100644 --- a/tauth/settings.py +++ b/tauth/settings.py @@ -1,12 +1,25 @@ from pathlib import Path -from config import DEBUG # noqa: F401 +import requests + +from config import tCOMMON_TOKEN, tCOMMON_URL_INTERNAL + +config = requests.get(f"{tCOMMON_URL_INTERNAL}/api/initialize/", params={ + "token": tCOMMON_TOKEN +}, allow_redirects=False).json() + +if not config["success"]: + raise ImportError("tCommon token doesn't match") + +if not config["services"]["auth"]: + raise ImportError("tAuth isn't registered in tCommon") + +DEBUG = config["debug"] +SECRET_KEY = config["services"]["auth"]["token"] BASE_DIR = Path(__file__).resolve().parent.parent -SECRET_KEY = "django-insecure--h3t*!a-h+(m7537)oxl9&fpjsin)#ht(5e!8w^5%ea1@f84u1" - -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["*"] INSTALLED_APPS = [ "django.contrib.admin", @@ -15,6 +28,7 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "tauth.apps.DBConfig" ] MIDDLEWARE = [ @@ -52,7 +66,7 @@ WSGI_APPLICATION = "tauth.wsgi.application" DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + "NAME": BASE_DIR / "auth.sqlite3", } } diff --git a/tauth/templates/404.html b/tauth/templates/404.html index 823d322..221afca 100644 --- a/tauth/templates/404.html +++ b/tauth/templates/404.html @@ -1,6 +1,6 @@ {% extends "base.html" %} -{% block card %} +{% block body %}
-
{% endblock %} diff --git a/tauth/views/helper.py b/tauth/views/helper.py index c5a9676..d60faa9 100644 --- a/tauth/views/helper.py +++ b/tauth/views/helper.py @@ -4,6 +4,8 @@ from django.core.handlers.wsgi import WSGIRequest from django.http import HttpResponse from django.template import loader +from tauth.settings import config + COLORS = ["rosewater", "flamingo", "pink", "mauve", "red", "maroon", "peach", "yellow", "green", "teal", "sky", "sapphire", "blue", "lavender"] def render_template( @@ -17,7 +19,8 @@ def render_template( ) -> HttpResponse: c = { "theme": "auto", - "accent": random.choice(COLORS) + "accent": random.choice(COLORS), + "config": config } for key, val in context.items(): diff --git a/tauth/views/templates.py b/tauth/views/templates.py index 8442a88..5cf989e 100644 --- a/tauth/views/templates.py +++ b/tauth/views/templates.py @@ -1,19 +1,67 @@ +import re + +from django.contrib.auth import login as set_auth +from django.contrib.auth import logout as remove_auth +from django.contrib.auth.models import User from django.core.handlers.wsgi import WSGIRequest -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from config import ALLOW_NEW_USERS +from tauth.models import TUser from .helper import render_template def index(request: WSGIRequest) -> HttpResponse: + if request.user.is_authenticated: + return render_template( + request, "index.html", + username=request.user.get_username() + ) return render_template( request, "noauth/index.html", new_users=ALLOW_NEW_USERS ) def signup(request: WSGIRequest) -> HttpResponse: + if request.user.is_authenticated: + return HttpResponseRedirect("/") + if ALLOW_NEW_USERS: + 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" + + else: + try: + u = User.objects.create_user( + username=username, + password=password + ) + except User.DoesNotExist: + error = "Username already in use" + else: + TUser.objects.create(user=u) + + set_auth(request, u) + return HttpResponseRedirect("/") + + return render_template( + request, "noauth/signup.html", + title="Sign Up", + error=error, + repopulate={ + "username": username + } + ) + return render_template( request, "noauth/signup.html", title="Sign Up"