generated from jCloud/repository-template
Bug fix: HTTPException should not be used in a middleware, so use JSONResponse instead
This commit is contained in:
@@ -16,6 +16,7 @@ import hmac
|
|||||||
import hashlib
|
import hashlib
|
||||||
from fastapi import Request, HTTPException
|
from fastapi import Request, HTTPException
|
||||||
from starlette.middleware.base import BaseHTTPMiddleware
|
from starlette.middleware.base import BaseHTTPMiddleware
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'GiteaSignatureMiddleware'
|
'GiteaSignatureMiddleware'
|
||||||
@@ -45,7 +46,10 @@ class GiteaSignatureMiddleware(BaseHTTPMiddleware):
|
|||||||
signature = request.headers.get('X-Gitea-Signature')
|
signature = request.headers.get('X-Gitea-Signature')
|
||||||
|
|
||||||
if not signature:
|
if not signature:
|
||||||
raise HTTPException(status_code = 401, detail = 'Missing signature')
|
return JSONResponse(
|
||||||
|
status_code = 401,
|
||||||
|
content = {'detail': 'Invalid signature'}
|
||||||
|
)
|
||||||
|
|
||||||
body = await request.body()
|
body = await request.body()
|
||||||
|
|
||||||
@@ -57,6 +61,9 @@ class GiteaSignatureMiddleware(BaseHTTPMiddleware):
|
|||||||
).hexdigest()}',
|
).hexdigest()}',
|
||||||
signature
|
signature
|
||||||
):
|
):
|
||||||
raise HTTPException(status_code = 401, detail = 'Invalid signature')
|
return JSONResponse(
|
||||||
|
status_code = 401,
|
||||||
|
content = {'detail': 'Invalid signature'}
|
||||||
|
)
|
||||||
|
|
||||||
return await call_next(request)
|
return await call_next(request)
|
||||||
+4
-2
@@ -104,6 +104,8 @@ async def test_GiteaSignatureMiddleware_valid_signature(body, secret):
|
|||||||
res = await middleware.dispatch(req, call_next)
|
res = await middleware.dispatch(req, call_next)
|
||||||
assert hasattr(res, 'called')
|
assert hasattr(res, 'called')
|
||||||
|
|
||||||
|
import pprint
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@pytest.mark.parametrize('signature,body,secret', [
|
@pytest.mark.parametrize('signature,body,secret', [
|
||||||
(make_signature(b'body', b''), b'body', b'\x42'),
|
(make_signature(b'body', b''), b'body', b'\x42'),
|
||||||
@@ -139,5 +141,5 @@ async def test_GiteaSignatureMiddleware_invalid_signature(signature, body, secre
|
|||||||
headers
|
headers
|
||||||
)
|
)
|
||||||
|
|
||||||
with pytest.raises(HTTPException):
|
res = await middleware.dispatch(req, call_next)
|
||||||
await middleware.dispatch(req, call_next)
|
assert res.status_code == 401
|
||||||
Reference in New Issue
Block a user