Skip to content

Commit

Permalink
Merge pull request #801 from riteshjha/feature/800
Browse files Browse the repository at this point in the history
cookie settings from config
  • Loading branch information
josephmancuso authored Aug 13, 2024
2 parents bd03d85 + 51605ae commit 80c738b
Showing 1 changed file with 26 additions and 19 deletions.
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

0 comments on commit 80c738b

Please sign in to comment.