-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into chore/support-cdk-environment-e2e
* develop: chore(deps-dev): bump aws-cdk-lib from 2.40.0 to 2.41.0 (#1507) update changelog with latest changes feat(tracer): support methods with the same name (ABCs) by including fully qualified name in v2 (#1486) chore(deps-dev): bump aws-cdk-aws-apigatewayv2-integrations-alpha from 2.39.1a0 to 2.40.0a0 (#1496) chore(deps-dev): bump mkdocs-material from 8.4.2 to 8.4.3 (#1504) chore(deps): bump pydantic from 1.10.1 to 1.10.2 (#1502) update changelog with latest changes feat(data-classes): add KafkaEvent and KafkaEventRecord (#1485) chore(deps-dev): bump pytest from 7.1.2 to 7.1.3 (#1497) update changelog with latest changes feat(event_handler): add cookies as 1st class citizen in v2 (#1487) chore(deps-dev): bump black from 22.6.0 to 22.8.0 (#1494) chore(deps-dev): bump aws-cdk-lib from 2.39.1 to 2.40.0 (#1495) chore(maintenance): add discord link to first PR and first issue (#1493) update changelog with latest changes refactor(batch): remove legacy sqs_batch_processor (#1492) chore(deps): bump pydantic from 1.10.0 to 1.10.1 (#1491) chore(deps-dev): bump flake8-variables-names from 0.0.4 to 0.0.5 (#1490)
- Loading branch information
Showing
37 changed files
with
1,641 additions
and
1,243 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from datetime import datetime | ||
from enum import Enum | ||
from io import StringIO | ||
from typing import List, Optional | ||
|
||
|
||
class SameSite(Enum): | ||
""" | ||
SameSite allows a server to define a cookie attribute making it impossible for | ||
the browser to send this cookie along with cross-site requests. The main | ||
goal is to mitigate the risk of cross-origin information leakage, and provide | ||
some protection against cross-site request forgery attacks. | ||
See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00 for details. | ||
""" | ||
|
||
DEFAULT_MODE = "" | ||
LAX_MODE = "Lax" | ||
STRICT_MODE = "Strict" | ||
NONE_MODE = "None" | ||
|
||
|
||
def _format_date(timestamp: datetime) -> str: | ||
# Specification example: Wed, 21 Oct 2015 07:28:00 GMT | ||
return timestamp.strftime("%a, %d %b %Y %H:%M:%S GMT") | ||
|
||
|
||
class Cookie: | ||
""" | ||
A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an | ||
HTTP response or the Cookie header of an HTTP request. | ||
See https://tools.ietf.org/html/rfc6265 for details. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
value: str, | ||
path: str = "", | ||
domain: str = "", | ||
secure: bool = True, | ||
http_only: bool = False, | ||
max_age: Optional[int] = None, | ||
expires: Optional[datetime] = None, | ||
same_site: Optional[SameSite] = None, | ||
custom_attributes: Optional[List[str]] = None, | ||
): | ||
""" | ||
Parameters | ||
---------- | ||
name: str | ||
The name of this cookie, for example session_id | ||
value: str | ||
The cookie value, for instance an uuid | ||
path: str | ||
The path for which this cookie is valid. Optional | ||
domain: str | ||
The domain for which this cookie is valid. Optional | ||
secure: bool | ||
Marks the cookie as secure, only sendable to the server with an encrypted request over the HTTPS protocol | ||
http_only: bool | ||
Enabling this attribute makes the cookie inaccessible to the JavaScript `Document.cookie` API | ||
max_age: Optional[int] | ||
Defines the period of time after which the cookie is invalid. Use negative values to force cookie deletion. | ||
expires: Optional[datetime] | ||
Defines a date where the permanent cookie expires. | ||
same_site: Optional[SameSite] | ||
Determines if the cookie should be sent to third party websites | ||
custom_attributes: Optional[List[str]] | ||
List of additional custom attributes to set on the cookie | ||
""" | ||
self.name = name | ||
self.value = value | ||
self.path = path | ||
self.domain = domain | ||
self.secure = secure | ||
self.expires = expires | ||
self.max_age = max_age | ||
self.http_only = http_only | ||
self.same_site = same_site | ||
self.custom_attributes = custom_attributes | ||
|
||
def __str__(self) -> str: | ||
payload = StringIO() | ||
payload.write(f"{self.name}={self.value}") | ||
|
||
if self.path: | ||
payload.write(f"; Path={self.path}") | ||
|
||
if self.domain: | ||
payload.write(f"; Domain={self.domain}") | ||
|
||
if self.expires: | ||
payload.write(f"; Expires={_format_date(self.expires)}") | ||
|
||
if self.max_age: | ||
if self.max_age > 0: | ||
payload.write(f"; MaxAge={self.max_age}") | ||
else: | ||
# negative or zero max-age should be set to 0 | ||
payload.write("; MaxAge=0") | ||
|
||
if self.http_only: | ||
payload.write("; HttpOnly") | ||
|
||
if self.secure: | ||
payload.write("; Secure") | ||
|
||
if self.same_site: | ||
payload.write(f"; SameSite={self.same_site.value}") | ||
|
||
if self.custom_attributes: | ||
for attr in self.custom_attributes: | ||
payload.write(f"; {attr}") | ||
|
||
return payload.getvalue() |
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
Oops, something went wrong.