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

cookie settings from config #801

Merged
merged 3 commits into from
Aug 13, 2024
Merged
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
45 changes: 26 additions & 19 deletions src/masonite/cookies/Cookie.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from masonite.configuration import config


class Cookie:
def __init__(
self,
name,
value,
expires=None,
http_only=True,
path="/",
timezone=None,
secure=False,
samesite="Strict",
encrypt=True,
):
def __init__(self, name, value, **options):
self.options = options

self.name = name
self.value = value
self.http_only = http_only
self.secure = secure
self.expires = expires
self.timezone = timezone
self.samesite = samesite
self.path = path
self.encrypt = encrypt
self.http_only = self.__get_option('http_only', True)
self.secure = self.__get_option('secure', False)
self.expires = self.__get_option('expires', None)
self.timezone = self.__get_option('timezone', None)
self.samesite = self.__get_option('samesite', 'Strict')
self.path = self.__get_option('path', '/')
self.encrypt = self.__get_option('encrypt', True)

def render(self):
response = f"{self.name}={self.value};"
Expand All @@ -38,3 +32,16 @@ def render(self):
response += f"SameSite={self.samesite};"

return response

def __get_option(self, key: str, default: any):
"""
Get cookie options from config/session.py
if option key found in options then it return that
if not found in options then it will fetch from config
if not found in config then use the default value
"""
if key in self.options:
return self.options[key]
else:
cookie = config('session.drivers.cookie')
return cookie[key] if key in cookie else default
Loading