-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathflask_middleware.py
117 lines (92 loc) · 4.28 KB
/
flask_middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
#
# This software is licensed under the Apache License, Version 2.0 (the
# "License") as published by the Apache Software Foundation.
#
# You may not use this file except in compliance with the License. You may
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Union
from supertokens_python.async_to_sync.base import sync
from supertokens_python.framework import BaseResponse
if TYPE_CHECKING:
from flask import Flask
class Middleware:
def __init__(self, app: Flask):
self.app = app
self.set_before_after_request()
self.set_error_handler()
def set_before_after_request(self):
app = self.app
from supertokens_python.framework.flask.flask_request import FlaskRequest
from supertokens_python.framework.flask.flask_response import FlaskResponse
from supertokens_python.supertokens import manage_session_post_response
from supertokens_python.utils import default_user_context
from flask.wrappers import Response
# There is an error in the typing provided by flask, so we ignore it
# for now.
@app.before_request # type: ignore
def _():
from supertokens_python import Supertokens
from flask import request
from flask.wrappers import Response
st = Supertokens.get_instance()
request_ = FlaskRequest(request)
response_ = FlaskResponse(Response())
user_context = default_user_context(request_)
result: Union[BaseResponse, None] = sync(
st.middleware(request_, response_, user_context),
)
if result is not None:
if isinstance(result, FlaskResponse):
return result.response
raise Exception("Should never come here")
return None
@app.after_request
def _(response: Response):
from flask import g
response_ = FlaskResponse(response)
if hasattr(g, "supertokens") and g.supertokens is not None:
manage_session_post_response(g.supertokens, response_, {})
return response_.response
@app.teardown_request
def _(_):
from flask import g
if hasattr(g, "supertokens"):
# this is to ensure there are no shared objects between requests.
# calling any other API with a shared request causes a security issue, resulting in unintentional
# sign-ins. More on this here - https://github.com/supertokens/supertokens-python/issues/463
g.pop("supertokens")
def set_error_handler(self):
app = self.app
from supertokens_python.exceptions import SuperTokensError
from flask import request
@app.errorhandler(SuperTokensError)
def _(error: Exception):
from supertokens_python import Supertokens
from supertokens_python.framework.flask.flask_request import FlaskRequest
from supertokens_python.framework.flask.flask_response import FlaskResponse
from supertokens_python.utils import default_user_context
from flask.wrappers import Response
st = Supertokens.get_instance()
response = Response(json.dumps({}), mimetype="application/json", status=200)
base_request = FlaskRequest(request)
user_context = default_user_context(base_request)
result: BaseResponse = sync(
st.handle_supertokens_error(
base_request,
error,
FlaskResponse(response),
user_context,
),
)
if isinstance(result, FlaskResponse):
return result.response
raise Exception("Should never come here")