forked from doubaniux/boofilsic
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
685fc75
commit 8a3a552
Showing
7 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |