Skip to content

Commit a949385

Browse files
committed
adding dev-v0.14.8 tag to this commit to ensure building
1 parent 9baaa3b commit a949385

File tree

4 files changed

+138
-53
lines changed

4 files changed

+138
-53
lines changed

html/supertokens_python/constants.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h1 class="title">Module <code>supertokens_python.constants</code></h1>
4242
from __future__ import annotations
4343

4444
SUPPORTED_CDI_VERSIONS = [&#34;2.21&#34;]
45-
VERSION = &#34;0.14.7&#34;
45+
VERSION = &#34;0.14.8&#34;
4646
TELEMETRY = &#34;/telemetry&#34;
4747
USER_COUNT = &#34;/users/count&#34;
4848
USER_DELETE = &#34;/user/remove&#34;

html/supertokens_python/recipe/session/framework/fastapi/index.html

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
3939
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
4040
# License for the specific language governing permissions and limitations
4141
# under the License.
42+
import json
43+
4244
from typing import Any, Callable, Coroutine, Dict, Union, List, Optional
4345

46+
from supertokens_python import Supertokens
4447
from supertokens_python.framework.fastapi.fastapi_request import FastApiRequest
48+
from supertokens_python.framework.fastapi.fastapi_response import FastApiResponse
4549
from supertokens_python.recipe.session import SessionRecipe
50+
from supertokens_python.exceptions import SuperTokensError
4651
from supertokens_python.types import MaybeAwaitable
52+
from fastapi.responses import JSONResponse
53+
from fastapi import Request
4754

4855
from ...interfaces import SessionContainer, SessionClaimValidator
4956

@@ -62,13 +69,12 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
6269
) -&gt; Callable[..., Coroutine[Any, Any, Union[SessionContainer, None]]]:
6370
if user_context is None:
6471
user_context = {}
65-
from fastapi import Request
6672

6773
async def func(request: Request) -&gt; Union[SessionContainer, None]:
68-
baseRequest = FastApiRequest(request)
74+
base_req = FastApiRequest(request)
6975
recipe = SessionRecipe.get_instance()
7076
session = await recipe.verify_session(
71-
baseRequest,
77+
base_req,
7278
anti_csrf_check,
7379
session_required,
7480
check_database,
@@ -78,12 +84,31 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
7884
if session is None:
7985
if session_required:
8086
raise Exception(&#34;Should never come here&#34;)
81-
baseRequest.set_session_as_none()
87+
base_req.set_session_as_none()
8288
else:
83-
baseRequest.set_session(session)
84-
return baseRequest.get_session()
89+
base_req.set_session(session)
90+
return base_req.get_session()
8591

86-
return func</code></pre>
92+
return func
93+
94+
95+
async def session_exception_handler(
96+
request: Request, exc: SuperTokensError
97+
) -&gt; JSONResponse:
98+
&#34;&#34;&#34;FastAPI exceptional handler for errors raised by Supertokens SDK when not using middleware
99+
100+
Usage: `app.add_exception_handler(SuperTokensError, st_exception_handler)`
101+
&#34;&#34;&#34;
102+
base_req = FastApiRequest(request)
103+
base_res = FastApiResponse(JSONResponse())
104+
result = await Supertokens.get_instance().handle_supertokens_error(
105+
base_req, exc, base_res
106+
)
107+
if isinstance(result, FastApiResponse):
108+
body = json.loads(result.response.body)
109+
return JSONResponse(body, status_code=result.response.status_code)
110+
111+
raise Exception(&#34;Should never come here&#34;)</code></pre>
87112
</details>
88113
</section>
89114
<section>
@@ -93,6 +118,35 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
93118
<section>
94119
<h2 class="section-title" id="header-functions">Functions</h2>
95120
<dl>
121+
<dt id="supertokens_python.recipe.session.framework.fastapi.session_exception_handler"><code class="name flex">
122+
<span>async def <span class="ident">session_exception_handler</span></span>(<span>request: starlette.requests.Request, exc: <a title="supertokens_python.exceptions.SuperTokensError" href="../../../../exceptions.html#supertokens_python.exceptions.SuperTokensError">SuperTokensError</a>) ‑> starlette.responses.JSONResponse</span>
123+
</code></dt>
124+
<dd>
125+
<div class="desc"><p>FastAPI exceptional handler for errors raised by Supertokens SDK when not using middleware</p>
126+
<p>Usage: <code>app.add_exception_handler(SuperTokensError, st_exception_handler)</code></p></div>
127+
<details class="source">
128+
<summary>
129+
<span>Expand source code</span>
130+
</summary>
131+
<pre><code class="python">async def session_exception_handler(
132+
request: Request, exc: SuperTokensError
133+
) -&gt; JSONResponse:
134+
&#34;&#34;&#34;FastAPI exceptional handler for errors raised by Supertokens SDK when not using middleware
135+
136+
Usage: `app.add_exception_handler(SuperTokensError, st_exception_handler)`
137+
&#34;&#34;&#34;
138+
base_req = FastApiRequest(request)
139+
base_res = FastApiResponse(JSONResponse())
140+
result = await Supertokens.get_instance().handle_supertokens_error(
141+
base_req, exc, base_res
142+
)
143+
if isinstance(result, FastApiResponse):
144+
body = json.loads(result.response.body)
145+
return JSONResponse(body, status_code=result.response.status_code)
146+
147+
raise Exception(&#34;Should never come here&#34;)</code></pre>
148+
</details>
149+
</dd>
96150
<dt id="supertokens_python.recipe.session.framework.fastapi.verify_session"><code class="name flex">
97151
<span>def <span class="ident">verify_session</span></span>(<span>anti_csrf_check: Optional[bool] = None, session_required: bool = True, check_database: bool = False, override_global_claim_validators: Optional[Callable[[List[<a title="supertokens_python.recipe.session.interfaces.SessionClaimValidator" href="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionClaimValidator">SessionClaimValidator</a>], <a title="supertokens_python.recipe.session.interfaces.SessionContainer" href="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionContainer">SessionContainer</a>, Dict[str, Any]], Union[Awaitable[List[<a title="supertokens_python.recipe.session.interfaces.SessionClaimValidator" href="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionClaimValidator">SessionClaimValidator</a>]], List[<a title="supertokens_python.recipe.session.interfaces.SessionClaimValidator" href="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionClaimValidator">SessionClaimValidator</a>]]]] = None, user_context: Optional[Dict[str, Any]] = None) ‑> Callable[..., Coroutine[Any, Any, Optional[<a title="supertokens_python.recipe.session.interfaces.SessionContainer" href="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionContainer">SessionContainer</a>]]]</span>
98152
</code></dt>
@@ -116,13 +170,12 @@ <h2 class="section-title" id="header-functions">Functions</h2>
116170
) -&gt; Callable[..., Coroutine[Any, Any, Union[SessionContainer, None]]]:
117171
if user_context is None:
118172
user_context = {}
119-
from fastapi import Request
120173

121174
async def func(request: Request) -&gt; Union[SessionContainer, None]:
122-
baseRequest = FastApiRequest(request)
175+
base_req = FastApiRequest(request)
123176
recipe = SessionRecipe.get_instance()
124177
session = await recipe.verify_session(
125-
baseRequest,
178+
base_req,
126179
anti_csrf_check,
127180
session_required,
128181
check_database,
@@ -132,10 +185,10 @@ <h2 class="section-title" id="header-functions">Functions</h2>
132185
if session is None:
133186
if session_required:
134187
raise Exception(&#34;Should never come here&#34;)
135-
baseRequest.set_session_as_none()
188+
base_req.set_session_as_none()
136189
else:
137-
baseRequest.set_session(session)
138-
return baseRequest.get_session()
190+
base_req.set_session(session)
191+
return base_req.get_session()
139192

140193
return func</code></pre>
141194
</details>
@@ -158,6 +211,7 @@ <h2>Index</h2>
158211
</li>
159212
<li><h3><a href="#header-functions">Functions</a></h3>
160213
<ul class="">
214+
<li><code><a title="supertokens_python.recipe.session.framework.fastapi.session_exception_handler" href="#supertokens_python.recipe.session.framework.fastapi.session_exception_handler">session_exception_handler</a></code></li>
161215
<li><code><a title="supertokens_python.recipe.session.framework.fastapi.verify_session" href="#supertokens_python.recipe.session.framework.fastapi.verify_session">verify_session</a></code></li>
162216
</ul>
163217
</li>

html/supertokens_python/recipe/session/framework/flask/index.html

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.flask
4242
from functools import wraps
4343
from typing import Any, Callable, Dict, TypeVar, Union, cast, List, Optional
4444

45+
from supertokens_python import Supertokens
4546
from supertokens_python.async_to_sync_wrapper import sync
4647
from supertokens_python.framework.flask.flask_request import FlaskRequest
48+
from supertokens_python.framework.flask.flask_response import FlaskResponse
4749
from supertokens_python.recipe.session import SessionRecipe, SessionContainer
50+
from supertokens_python.exceptions import SuperTokensError
4851
from supertokens_python.recipe.session.interfaces import SessionClaimValidator
4952
from supertokens_python.types import MaybeAwaitable
5053

@@ -71,27 +74,39 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.flask
7174
def wrapped_function(*args: Any, **kwargs: Any):
7275
from flask import make_response, request
7376

74-
baseRequest = FlaskRequest(request)
75-
recipe = SessionRecipe.get_instance()
76-
session = sync(
77-
recipe.verify_session(
78-
baseRequest,
79-
anti_csrf_check,
80-
session_required,
81-
check_database,
82-
override_global_claim_validators,
83-
user_context,
77+
base_req = FlaskRequest(request)
78+
79+
try:
80+
recipe = SessionRecipe.get_instance()
81+
session = sync(
82+
recipe.verify_session(
83+
base_req,
84+
anti_csrf_check,
85+
session_required,
86+
check_database,
87+
override_global_claim_validators,
88+
user_context,
89+
)
8490
)
85-
)
86-
if session is None:
87-
if session_required:
88-
raise Exception(&#34;Should never come here&#34;)
89-
baseRequest.set_session_as_none()
90-
else:
91-
baseRequest.set_session(session)
91+
if session is None:
92+
if session_required:
93+
raise Exception(&#34;Should never come here&#34;)
94+
base_req.set_session_as_none()
95+
else:
96+
base_req.set_session(session)
9297

93-
response = f(*args, **kwargs)
94-
return make_response(response) if response is not None else None
98+
response = f(*args, **kwargs)
99+
return make_response(response) if response is not None else None
100+
except SuperTokensError as e:
101+
response = FlaskResponse(make_response())
102+
result = sync(
103+
Supertokens.get_instance().handle_supertokens_error(
104+
base_req, e, response
105+
)
106+
)
107+
if isinstance(result, FlaskResponse):
108+
return result.response
109+
raise Exception(&#34;Should never come here&#34;)
95110

96111
return cast(_T, wrapped_function)
97112

@@ -134,27 +149,39 @@ <h2 class="section-title" id="header-functions">Functions</h2>
134149
def wrapped_function(*args: Any, **kwargs: Any):
135150
from flask import make_response, request
136151

137-
baseRequest = FlaskRequest(request)
138-
recipe = SessionRecipe.get_instance()
139-
session = sync(
140-
recipe.verify_session(
141-
baseRequest,
142-
anti_csrf_check,
143-
session_required,
144-
check_database,
145-
override_global_claim_validators,
146-
user_context,
152+
base_req = FlaskRequest(request)
153+
154+
try:
155+
recipe = SessionRecipe.get_instance()
156+
session = sync(
157+
recipe.verify_session(
158+
base_req,
159+
anti_csrf_check,
160+
session_required,
161+
check_database,
162+
override_global_claim_validators,
163+
user_context,
164+
)
165+
)
166+
if session is None:
167+
if session_required:
168+
raise Exception(&#34;Should never come here&#34;)
169+
base_req.set_session_as_none()
170+
else:
171+
base_req.set_session(session)
172+
173+
response = f(*args, **kwargs)
174+
return make_response(response) if response is not None else None
175+
except SuperTokensError as e:
176+
response = FlaskResponse(make_response())
177+
result = sync(
178+
Supertokens.get_instance().handle_supertokens_error(
179+
base_req, e, response
180+
)
147181
)
148-
)
149-
if session is None:
150-
if session_required:
151-
raise Exception(&#34;Should never come here&#34;)
152-
baseRequest.set_session_as_none()
153-
else:
154-
baseRequest.set_session(session)
155-
156-
response = f(*args, **kwargs)
157-
return make_response(response) if response is not None else None
182+
if isinstance(result, FlaskResponse):
183+
return result.response
184+
raise Exception(&#34;Should never come here&#34;)
158185

159186
return cast(_T, wrapped_function)
160187

html/supertokens_python/recipe/session/session_request_functions.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.session_request
181181
do_anti_csrf_check = normalise_http_method(request.method()) != &#34;get&#34;
182182
if request_transfer_method == &#34;header&#34;:
183183
do_anti_csrf_check = False
184+
if request_access_token is None:
185+
do_anti_csrf_check = False
184186

185187
if do_anti_csrf_check and config.anti_csrf == &#34;VIA_CUSTOM_HEADER&#34;:
186188
if config.anti_csrf == &#34;VIA_CUSTOM_HEADER&#34;:
@@ -691,6 +693,8 @@ <h2 class="section-title" id="header-functions">Functions</h2>
691693
do_anti_csrf_check = normalise_http_method(request.method()) != &#34;get&#34;
692694
if request_transfer_method == &#34;header&#34;:
693695
do_anti_csrf_check = False
696+
if request_access_token is None:
697+
do_anti_csrf_check = False
694698

695699
if do_anti_csrf_check and config.anti_csrf == &#34;VIA_CUSTOM_HEADER&#34;:
696700
if config.anti_csrf == &#34;VIA_CUSTOM_HEADER&#34;:

0 commit comments

Comments
 (0)