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

3060 active user session #3182

Merged
merged 28 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c0ec6fb
no change
raftmsohani Sep 11, 2024
be56f66
lok into token
raftmsohani Sep 11, 2024
269243d
Merge branch 'develop' into 3060-active-user-session
raftmsohani Sep 13, 2024
5c4a602
Merge branch 'develop' into 3060-active-user-session
raftmsohani Sep 19, 2024
998c60a
Merge branch 'develop' into 3060-active-user-session
raftmsohani Sep 23, 2024
b7109e1
Merge branch 'develop' into 3060-active-user-session
raftmsohani Sep 24, 2024
3de4cad
Added a custom session handler instead of signed_sessions
raftmsohani Sep 25, 2024
dc48d36
Merge branch '3060-active-user-session' of github.com:raft-tech/TANF-…
raftmsohani Sep 25, 2024
0658bea
Update common.py
raftmsohani Sep 25, 2024
5809393
Update README.md
raftmsohani Sep 25, 2024
5390920
added a new settings
raftmsohani Sep 25, 2024
338aea0
Merge branch '3060-active-user-session' of github.com:raft-tech/TANF-…
raftmsohani Sep 25, 2024
406e52e
Merge branch 'develop' into 3060-active-user-session
raftmsohani Sep 25, 2024
021bf69
3060 linting
raftmsohani Sep 25, 2024
66ee523
3060 linting
raftmsohani Sep 26, 2024
cec22a4
3060 remove unused params
raftmsohani Sep 26, 2024
398003b
3060 uncommented SIGNED_COOKIE_EXPIRES
raftmsohani Sep 26, 2024
73bcfe6
3060 update markdown
raftmsohani Sep 26, 2024
fad2ffa
disable session expire at browser close
raftmsohani Oct 1, 2024
c185454
Merge branch 'develop' into 3060-active-user-session
raftmsohani Oct 1, 2024
2ba20d7
Merge branch 'develop' into 3060-active-user-session
raftmsohani Oct 2, 2024
ee5052f
Merge branch 'develop' into 3060-active-user-session
raftmsohani Oct 9, 2024
6abb325
remove unused overrides
raftmsohani Oct 10, 2024
5f9b420
Merge branch '3060-active-user-session' of github.com:raft-tech/TANF-…
raftmsohani Oct 10, 2024
ae787a3
Merge branch 'develop' into 3060-active-user-session
ADPennington Oct 10, 2024
6d28693
Merge branch 'develop' into 3060-active-user-session
raftmsohani Oct 15, 2024
1b3984d
corrected the timeouts
raftmsohani Oct 15, 2024
1e01d38
Merge branch 'develop' into 3060-active-user-session
raftmsohani Oct 15, 2024
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
59 changes: 59 additions & 0 deletions tdrs-backend/tdpservice/core/custom_session_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Custom session engine for TDP."""

from django.contrib.sessions.backends import signed_cookies
from django.core import signing
import datetime
from django.conf import settings

class SessionStore(signed_cookies.SessionStore):
"""Custom session engine for TDP."""

def __init__(self, session_key=None):
"""Initialize the custom session engine."""
super().__init__(session_key)

def load(self):
"""Load the session data from the database."""
"""
Load the data from the key itself instead of fetching from some
external data store. Opposite of _get_session_key(), raise BadSignature
if signature fails.
"""

try:
return signing.loads(
self.session_key,
serializer=self.serializer,
# This doesn't handle non-default expiry dates, see #19201
max_age=datetime.timedelta(seconds=settings.SIGNED_COOKIE_EXPIRES),
salt="django.contrib.sessions.backends.signed_cookies",
)
except Exception:
# BadSignature, ValueError, or unpickling exceptions. If any of
# these happen, reset the session.
return {}

def cycle_key(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove these now that we don't need to debug anymore?

"""Cycle the session key."""
super().cycle_key()

def create(self):
"""Create a new session."""
# first check if the session exists
super().create()

def save(self, must_create=False):
"""Save the session data."""
super().save(must_create)

def exists(self, session_key):
"""Check if the session exists."""
return super().exists(session_key)

def delete(self, session_key=None):
"""Delete the session data."""
super().delete(session_key)

def _get_session_key(self):
"""Get the session key."""
return super()._get_session_key()
1 change: 0 additions & 1 deletion tdrs-backend/tdpservice/data_files/admin/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Admin class for DataFile objects."""
from django.contrib import admin
from tdpservice.core.utils import ReadOnlyAdminMixin
# from tdpservice.core.filters import custom_filter_title
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but I saw this is commented and not deleted

from tdpservice.data_files.models import DataFile, LegacyFileTransfer
from tdpservice.parsers.models import DataFileSummary, ParserError
from tdpservice.data_files.admin.filters import DataFileSummaryPrgTypeFilter, LatestReparseEvent, VersionFilter
Expand Down
1 change: 1 addition & 0 deletions tdrs-backend/tdpservice/security/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

def token_is_valid(token):
"""Check if token is valid."""
# TODO: Add logging
utc_now = datetime.now()
utc_now = utc_now.replace(tzinfo=pytz.utc)
if token.created < (utc_now - timedelta(hours=settings.TOKEN_EXPIRATION_HOURS)):
Expand Down
10 changes: 8 additions & 2 deletions tdrs-backend/tdpservice/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ class Common(Configuration):
)

# Sessions
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
SESSION_ENGINE = "tdpservice.core.custom_session_engine"
#SIGNED_COOKIE_EXPIRES = 60 * 60 * 24 # 24 hours
SESSION_COOKIE_HTTPONLY = True
SESSION_SAVE_EVERY_REQUEST = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setting causes the session expiry to be set to browser close and ignore SESSION_COOKIE_AGE.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot set expire the session at browser close together with session expiry age.

SESSION_COOKIE_AGE = 15 * 60 # 15 minutes
SESSION_COOKIE_AGE = 10 # 15 minutes
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For testing only: revert back to previous setting

# The CSRF token Cookie holds no security benefits when confined to HttpOnly.
# Setting this to false to allow the frontend to include it in the header
# of API POST calls to prevent false negative authorization errors.
Expand Down Expand Up @@ -551,4 +553,8 @@ class Common(Configuration):
IGNORE_DUPLICATE_ERROR_PRECEDENCE = os.getenv("IGNORE_DUPLICATE_ERROR_PRECEDENCE", False)
BULK_CREATE_BATCH_SIZE = os.getenv("BULK_CREATE_BATCH_SIZE", 10000)
MEDIAN_LINE_PARSE_TIME = os.getenv("MEDIAN_LINE_PARSE_TIME", 0.0005574226379394531)

CSRF_COOKIE_SAMESITE = None
SESSION_COOKIE_SAMESITE = None

BYPASS_OFA_AUTH = os.getenv("BYPASS_OFA_AUTH", False)
Loading