Skip to content

Commit

Permalink
sync session to takahe
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email authored and alphatownsman committed Nov 13, 2023
1 parent 685fc75 commit 8a3a552
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions boofilsic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion misc/nginx.conf.d/neodb-dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 / {
Expand Down
2 changes: 1 addition & 1 deletion misc/nginx.conf.d/neodb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 / {
Expand Down
22 changes: 22 additions & 0 deletions takahe/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
9 changes: 9 additions & 0 deletions takahe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions takahe/urls.py
Original file line number Diff line number Diff line change
@@ -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"),
]
65 changes: 63 additions & 2 deletions takahe/views.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8a3a552

Please sign in to comment.