mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
fix(FastAPI): Use HTTPStatus instead of raw number
Signed-off-by: Steven Guikal <void@fluix.one>
This commit is contained in:
parent
0435c56a41
commit
42701514e7
3 changed files with 14 additions and 7 deletions
|
@ -1,14 +1,18 @@
|
||||||
|
from http import HTTPStatus
|
||||||
|
|
||||||
from aurweb.templates import make_context, render_template
|
from aurweb.templates import make_context, render_template
|
||||||
|
|
||||||
|
|
||||||
async def not_found(request, exc):
|
async def not_found(request, exc):
|
||||||
context = make_context(request, "Page Not Found")
|
context = make_context(request, "Page Not Found")
|
||||||
return render_template(request, "errors/404.html", context, 404)
|
return render_template(request, "errors/404.html", context,
|
||||||
|
HTTPStatus.NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
async def service_unavailable(request, exc):
|
async def service_unavailable(request, exc):
|
||||||
context = make_context(request, "Service Unavailable")
|
context = make_context(request, "Service Unavailable")
|
||||||
return render_template(request, "errors/503.html", context, 503)
|
return render_template(request, "errors/503.html", context,
|
||||||
|
HTTPStatus.SERVICE_UNAVAILABLE)
|
||||||
|
|
||||||
# Maps HTTP errors to functions
|
# Maps HTTP errors to functions
|
||||||
exceptions = {
|
exceptions = {
|
||||||
|
|
|
@ -221,4 +221,4 @@ async def metrics(request: Request):
|
||||||
|
|
||||||
@router.get("/raisefivethree", response_class=HTMLResponse)
|
@router.get("/raisefivethree", response_class=HTMLResponse)
|
||||||
async def raise_service_unavailable(request: Request):
|
async def raise_service_unavailable(request: Request):
|
||||||
raise HTTPException(status_code=503)
|
raise HTTPException(status_code=HTTPStatus.SERVICE_UNAVAILABLE)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
from http import HTTPStatus
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
|
@ -59,7 +60,8 @@ def open_session(request, conn, user_id):
|
||||||
"""
|
"""
|
||||||
if is_account_suspended(conn, user_id):
|
if is_account_suspended(conn, user_id):
|
||||||
_ = get_translator_for_request(request)
|
_ = get_translator_for_request(request)
|
||||||
raise HTTPException(status_code=403, detail=_('Account suspended'))
|
raise HTTPException(status_code=HTTPStatus.FORBIDDEN,
|
||||||
|
detail=_('Account suspended'))
|
||||||
# TODO This is a terrible message because it could imply the attempt at
|
# TODO This is a terrible message because it could imply the attempt at
|
||||||
# logging in just caused the suspension.
|
# logging in just caused the suspension.
|
||||||
|
|
||||||
|
@ -104,7 +106,7 @@ async def authenticate(request: Request, redirect: str = None, conn=Depends(aurw
|
||||||
if is_ip_banned(conn, request.client.host):
|
if is_ip_banned(conn, request.client.host):
|
||||||
_ = get_translator_for_request(request)
|
_ = get_translator_for_request(request)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=403,
|
status_code=HTTPStatus.FORBIDDEN,
|
||||||
detail=_('The login form is currently disabled for your IP address, '
|
detail=_('The login form is currently disabled for your IP address, '
|
||||||
'probably due to sustained spam attacks. Sorry for the '
|
'probably due to sustained spam attacks. Sorry for the '
|
||||||
'inconvenience.'))
|
'inconvenience.'))
|
||||||
|
@ -117,13 +119,14 @@ async def authenticate(request: Request, redirect: str = None, conn=Depends(aurw
|
||||||
# Let’s give attackers as little information as possible.
|
# Let’s give attackers as little information as possible.
|
||||||
_ = get_translator_for_request(request)
|
_ = get_translator_for_request(request)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
detail=_('Bad OAuth token. Please retry logging in from the start.'))
|
detail=_('Bad OAuth token. Please retry logging in from the start.'))
|
||||||
|
|
||||||
sub = user.get("sub") # this is the SSO account ID in JWT terminology
|
sub = user.get("sub") # this is the SSO account ID in JWT terminology
|
||||||
if not sub:
|
if not sub:
|
||||||
_ = get_translator_for_request(request)
|
_ = get_translator_for_request(request)
|
||||||
raise HTTPException(status_code=400, detail=_("JWT is missing its `sub` field."))
|
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail=_("JWT is missing its `sub` field."))
|
||||||
|
|
||||||
aur_accounts = conn.execute(select([Users.c.ID]).where(Users.c.SSOAccountID == sub)) \
|
aur_accounts = conn.execute(select([Users.c.ID]).where(Users.c.SSOAccountID == sub)) \
|
||||||
.fetchall()
|
.fetchall()
|
||||||
|
|
Loading…
Add table
Reference in a new issue