Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use compatible client to login #374

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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