Skip to content

fix: Make flask response consistent with other frameworks #271

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

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## unreleased

- Add missing `original` attribute to flask response and remove logic for cases where `response` is `None`

## [0.11.12] - 2022-12-27
- Fix django cookie expiry time format to make it consistent with other frameworks: https://github.com/supertokens/supertokens-python/issues/267

Expand Down
58 changes: 13 additions & 45 deletions supertokens_python/framework/flask/flask_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FlaskResponse(BaseResponse):
def __init__(self, response: Response):
super().__init__({})
self.response = response
self.original = response
self.headers: List[Any] = []
self.response_sent = False
self.status_set = False
Expand All @@ -44,53 +45,22 @@ def set_cookie(
httponly: bool = False,
samesite: str = "lax",
):
from werkzeug.http import dump_cookie

if self.response is None:
cookie = dump_cookie(
key,
value=value,
expires=int(expires / 1000),
path=path,
domain=domain,
secure=secure,
httponly=httponly,
samesite=samesite,
)
self.headers.append(("Set-Cookie", cookie))
else:
self.response.set_cookie(
key,
value=value,
expires=expires / 1000,
path=path,
domain=domain,
secure=secure,
httponly=httponly,
samesite=samesite,
)
self.response.set_cookie(
key,
value=value,
expires=expires / 1000,
path=path,
domain=domain,
secure=secure,
httponly=httponly,
samesite=samesite,
)

def set_header(self, key: str, value: str):
if self.response is None:
# TODO in the future the headrs must be validated..
# if not isinstance(value, str):
# raise TypeError("Value should be unicode.")
if "\n" in value or "\r" in value:
raise ValueError(
"Detected newline in header value. This is "
"a potential security problem"
)
self.headers.append((key, value))
else:
self.response.headers.add(key, value)
self.response.headers.add(key, value)

def get_header(self, key: str) -> Union[None, str]:
if self.response is not None:
return self.response.headers.get(key)
for value in self.headers:
if value[0] == key:
return value[1]
return None
return self.response.headers.get(key)

def set_status_code(self, status_code: int):
if not self.status_set:
Expand All @@ -99,8 +69,6 @@ def set_status_code(self, status_code: int):
self.status_set = True

def get_headers(self):
if self.response is None:
return self.headers
return self.response.headers

def set_json_content(self, content: Dict[str, Any]):
Expand Down