From 8a3a552bd6d0c5f1a1dd4f7f8abb9b5c526597d8 Mon Sep 17 00:00:00 2001 From: Her Email Date: Sun, 12 Nov 2023 20:24:17 -0500 Subject: [PATCH] sync session to takahe --- boofilsic/urls.py | 1 + misc/nginx.conf.d/neodb-dev.conf | 2 +- misc/nginx.conf.d/neodb.conf | 2 +- takahe/migrations/0001_initial.py | 22 +++++++++++ takahe/models.py | 9 +++++ takahe/urls.py | 9 +++++ takahe/views.py | 65 ++++++++++++++++++++++++++++++- 7 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 takahe/urls.py diff --git a/boofilsic/urls.py b/boofilsic/urls.py index ad023bf1..17576bfc 100644 --- a/boofilsic/urls.py +++ b/boofilsic/urls.py @@ -42,6 +42,7 @@ path("", include("common.urls")), path("", include("legacy.urls")), path("", include("developer.urls")), + path("", include("takahe.urls")), # path("oauth/", include("oauth2_provider.urls", namespace="oauth2_provider")), path("tz_detect/", include("tz_detect.urls")), path(settings.ADMIN_URL + "/", admin.site.urls), diff --git a/misc/nginx.conf.d/neodb-dev.conf b/misc/nginx.conf.d/neodb-dev.conf index 7050f90f..726ae84a 100644 --- a/misc/nginx.conf.d/neodb-dev.conf +++ b/misc/nginx.conf.d/neodb-dev.conf @@ -90,7 +90,7 @@ server { proxy_cache_valid any 72h; add_header X-Cache $upstream_cache_status; } - location ~* ^/(static|@|\.well-known|actor|inbox|api/v1|api/v2|auth|oauth|tags|settings|media|proxy|admin|djadmin) { + location ~* ^/(static|@|\.well-known|actor|inbox|api/v1|api/v2|oauth|tags|settings|media|proxy|admin|djadmin) { proxy_pass http://takahe; } location / { diff --git a/misc/nginx.conf.d/neodb.conf b/misc/nginx.conf.d/neodb.conf index 4293b2a2..4273b242 100644 --- a/misc/nginx.conf.d/neodb.conf +++ b/misc/nginx.conf.d/neodb.conf @@ -98,7 +98,7 @@ server { proxy_cache_valid any 72h; add_header X-Cache $upstream_cache_status; } - location ~* ^/(@|\.well-known|actor|inbox|api/v1|api/v2|auth|oauth|tags|settings|media|proxy|admin|djadmin) { + location ~* ^/(@|\.well-known|actor|inbox|api/v1|api/v2|oauth|tags|settings|media|proxy|admin|djadmin) { proxy_pass http://takahe; } location / { diff --git a/takahe/migrations/0001_initial.py b/takahe/migrations/0001_initial.py index 842280d6..36567011 100644 --- a/takahe/migrations/0001_initial.py +++ b/takahe/migrations/0001_initial.py @@ -15,6 +15,28 @@ class Migration(migrations.Migration): dependencies = [] operations = [ + migrations.CreateModel( + name="TakaheSession", + fields=[ + ( + "session_key", + models.CharField( + max_length=40, + primary_key=True, + serialize=False, + verbose_name="session key", + ), + ), + ("session_data", models.TextField(verbose_name="session data")), + ( + "expire_date", + models.DateTimeField(db_index=True, verbose_name="expire date"), + ), + ], + options={ + "db_table": "django_session", + }, + ), migrations.CreateModel( name="Domain", fields=[ diff --git a/takahe/models.py b/takahe/models.py index 635c0331..ed0c08f8 100644 --- a/takahe/models.py +++ b/takahe/models.py @@ -33,6 +33,15 @@ from django.db.models.manager import RelatedManager +class TakaheSession(models.Model): + session_key = models.CharField(_("session key"), max_length=40, primary_key=True) + session_data = models.TextField(_("session data")) + expire_date = models.DateTimeField(_("expire date"), db_index=True) + + class Meta: + db_table = "django_session" + + class Snowflake: """ Snowflake ID generator and parser. diff --git a/takahe/urls.py b/takahe/urls.py new file mode 100644 index 00000000..03b40de9 --- /dev/null +++ b/takahe/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from .views import * + +app_name = "users" +urlpatterns = [ + path("auth/login/", auth_login, name="auth_login"), + path("auth/logout/", auth_logout, name="auth_logout"), +] diff --git a/takahe/views.py b/takahe/views.py index 91ea44a2..bc1044bf 100644 --- a/takahe/views.py +++ b/takahe/views.py @@ -1,3 +1,64 @@ -from django.shortcuts import render +import time -# Create your views here. +from django.conf import settings +from django.contrib.auth.decorators import login_required +from django.contrib.sessions.backends.signed_cookies import SessionStore +from django.http import HttpRequest +from django.shortcuts import redirect, render +from django.utils.http import http_date +from loguru import logger + +from .models import TakaheSession + +_TAKAHE_SESSION_COOKIE_NAME = "sessionid" + + +@login_required +def auth_login(request: HttpRequest): + """Redirect to the login page if not yet, otherwise sync login info to takahe session""" + + # if SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies" in Takahe + session = SessionStore(session_key=request.COOKIES.get(_TAKAHE_SESSION_COOKIE_NAME)) + session._session_cache = request.session._session # type: ignore + session["_auth_user_backend"] = "django.contrib.auth.backends.ModelBackend" + session_key: str = session._get_session_key() # type: ignore + + # if SESSION_ENGINE = "django.contrib.sessions.backends.db" + # sess = request.session._session + # sess["_auth_user_backend"] = "django.contrib.auth.backends.ModelBackend" + # logger.info(f"session: {sess}") + # TakaheSession.objects.update_or_create( + # session_key=request.session.session_key, + # defaults={ + # "session_data": request.session.encode(sess), + # "expire_date": request.session.get_expiry_date(), + # }, + # ) + # session_key = request.session.session_key + + response = redirect(request.GET.get("next", "/")) + if request.session.get_expire_at_browser_close(): + max_age = None + expires = None + else: + max_age = request.session.get_expiry_age() + expires_time = time.time() + max_age + expires = http_date(expires_time) + response.set_cookie( + _TAKAHE_SESSION_COOKIE_NAME, + session_key, + max_age=max_age, + expires=expires, + domain=settings.SESSION_COOKIE_DOMAIN, + path=settings.SESSION_COOKIE_PATH, + secure=settings.SESSION_COOKIE_SECURE, + httponly=settings.SESSION_COOKIE_HTTPONLY, + samesite=settings.SESSION_COOKIE_SAMESITE, + ) + return response + + +def auth_logout(request: HttpRequest): + response = redirect("/account/logout") + response.delete_cookie(_TAKAHE_SESSION_COOKIE_NAME) + return response