From 9bb18a6cca00bc2bc7f45913508e98dbf11b1447 Mon Sep 17 00:00:00 2001 From: trinkey Date: Fri, 20 Dec 2024 23:44:31 -0500 Subject: [PATCH] sign up works --- .gitignore | 1 + .vscode/settings.json | 9 +++++ config.py | 10 ++--- tauth/apps.py | 6 +++ tauth/migrations/0001_initial.py | 23 ++++++++++++ tauth/migrations/__init__.py | 0 tauth/models.py | 6 +++ tauth/settings.py | 24 +++++++++--- tauth/templates/404.html | 2 +- tauth/templates/base.html | 19 +++------- tauth/templates/css/base.css | 60 ------------------------------ tauth/templates/index.html | 6 +++ tauth/templates/js/theme.js | 30 --------------- tauth/templates/noauth/signup.html | 24 ++++++++++-- tauth/views/helper.py | 5 ++- tauth/views/templates.py | 50 ++++++++++++++++++++++++- 16 files changed, 154 insertions(+), 121 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 tauth/apps.py create mode 100644 tauth/migrations/0001_initial.py create mode 100644 tauth/migrations/__init__.py create mode 100644 tauth/models.py delete mode 100644 tauth/templates/css/base.css create mode 100644 tauth/templates/index.html delete mode 100644 tauth/templates/js/theme.js 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 %}

Hmm. That doesn't look right.

Make sure the URL is correct and try again. (Error 404 - Page not found) diff --git a/tauth/templates/base.html b/tauth/templates/base.html index 4c55939..8538d86 100644 --- a/tauth/templates/base.html +++ b/tauth/templates/base.html @@ -8,21 +8,14 @@ + + + {% block head %}{% endblock %} - - - + + + diff --git a/tauth/templates/css/base.css b/tauth/templates/css/base.css deleted file mode 100644 index 965550e..0000000 --- a/tauth/templates/css/base.css +++ /dev/null @@ -1,60 +0,0 @@ -/* Colors */ -html { --rosewater: #f5e0dc; --flamingo: #f2cdcd; --pink: #f5c2e7; --mauve: #cba6f7; --red: #f38ba8; --maroon: #eba0ac; --peach: #fab387; --yellow: #f9e2af; --green: #a6e3a1; --teal: #94e2d5; --sky: #89dceb; --sapphire: #74c7ec; --blue: #89b4fa; --lavender: #b4befe; --text: #cdd6f4; --subtext1: #bac2de; --subtext0: #a6adc8; --overlay2: #9399b2; --overlay1: #7f849c; --overlay0: #6c7086; --surface2: #585b70; --surface1: #45475a; --surface0: #313244; --base: #1e1e2e; --mantle: #181825; --crust: #11111b; --base-low-op: #1e1e2ed0 } -html[data-light] { --rosewater: #dc8a78; --flamingo: #dd7878; --pink: #ea76cb; --mauve: #8839ef; --red: #d20f39; --maroon: #e64553; --peach: #fe640b; --yellow: #df8e1d; --green: #40a02b; --teal: #179299; --sky: #04a5e5; --sapphire: #209fb5; --blue: #1e66f5; --lavender: #7287fd; --text: #4c4f69; --subtext1: #5c5f77; --subtext0: #6c6f85; --overlay2: #7c7f93; --overlay1: #8c8fa1; --overlay0: #9ca0b0; --surface2: #acb0be; --surface1: #bcc0cc; --surface0: #ccd0da; --base: #eff1f5; --mantle: #e6e9ef; --crust: #dce0e8; --base-low-op: #eff1f5b0} - -/* Font */ -@font-face { font-family: 'DejaVu Sans'; font-style: normal; font-weight: 400; font-display: block; src: url("/static/font/DejaVuSans.ttf") format("truetype"); } -@font-face { font-family: 'DejaVu Sans'; font-style: normal; font-weight: 700; font-display: block; src: url("/static/font/DejaVuSans-Bold.ttf") format("truetype"); } -@font-face { font-family: 'DejaVu Sans'; font-style: italic; font-weight: 400; font-display: block; src: url("/static/font/DejaVuSans-Oblique.ttf") format("truetype"); } -@font-face { font-family: 'DejaVu Sans'; font-style: italic; font-weight: 700; font-display: block; src: url("/static/font/DejaVuSans-BoldOblique.ttf") format("truetype"); } -@font-face { font-family: 'DejaVu Sans'; font-style: normal; font-weight: 200; font-display: block; src: url("/static/font/DejaVuSans-ExtraLight.ttf") format("truetype"); } -@font-face { font-family: "Ubuntu Mono"; font-style: normal; font-weight: 400; font-display: block; src: url("/static/font/UbuntuMono-Regular.ttf") format("truetype"); } -@font-face { font-family: "Ubuntu Mono"; font-style: italic; font-weight: 400; font-display: block; src: url("/static/font/UbuntuMono-Italic.ttf") format("truetype"); } -@font-face { font-family: "Ubuntu Mono"; font-style: normal; font-weight: 700; font-display: block; src: url("/static/font/UbuntuMono-Bold.ttf") format("truetype"); } -@font-face { font-family: "Ubuntu Mono"; font-style: italic; font-weight: 700; font-display: block; src: url("/static/font/UbuntuMono-BoldItalic.ttf") format("truetype"); } - -body { - margin: 0; - width: 100vw; - min-height: 100vh; - overflow-x: hidden; - background-color: var(--base); - color: var(--text); - font-family: "DejaVu Sans"; - /* background-image: linear-gradient( - 135deg, - var(--base) 25%, - var(--mantle) 25%, - var(--mantle) 50%, - var(--base) 50%, - var(--base) 75%, - var(--mantle) 75% - ); - background-size: 20px 20px; - background-repeat: repeat; */ -} - -code, pre { - font-family: "Ubuntu Mono" -} - -small, i { - color: var(--subtext0); -} - -h1 { - color: var(--accent); -} - -a:link, -a:visited { - color: var(--accent); - font-weight: 700; -} - -#container { - text-align: center; - padding: 10px; - width: calc(100vw - 20px); - max-width: calc(100vw - 20px); -} diff --git a/tauth/templates/index.html b/tauth/templates/index.html new file mode 100644 index 0000000..73cc965 --- /dev/null +++ b/tauth/templates/index.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} + +{% block body %} +

Welcome back, {{ username }}!

+ tAuth +{% endblock %} diff --git a/tauth/templates/js/theme.js b/tauth/templates/js/theme.js deleted file mode 100644 index e0fd755..0000000 --- a/tauth/templates/js/theme.js +++ /dev/null @@ -1,30 +0,0 @@ -let _themeMM = matchMedia("(prefers-color-scheme: light)"); -let light, useAutoTheme; -// {% if theme == "auto" %} - light = _themeMM.matches; - useAutoTheme = true; -// {% else %} - // {% if theme == "light" %} - light = true; - // {% else %} - light = false; - // {% endif %} - useAutoTheme = false; -// {% endif %} - -function setTheme() { - if (light) { - document.documentElement.setAttribute("data-light", ""); - } else { - document.documentElement.removeAttribute("data-light"); - } -} - -_themeMM.addEventListener("change", function() { - if (useAutoTheme) { - light = _themeMM.matches; - setTheme(); - } -}); - -setTheme(); diff --git a/tauth/templates/noauth/signup.html b/tauth/templates/noauth/signup.html index aa41700..77b8806 100644 --- a/tauth/templates/noauth/signup.html +++ b/tauth/templates/noauth/signup.html @@ -4,12 +4,30 @@

Sign Up

tAuth

-

+ {% csrf_token %}

-

-
2-30 characters, A-Z, 0-9, _, and -. Can't start or end with a -
+
+

+

+

+ +

+

{% 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"