mirror of https://github.com/tiangolo/fastapi.git
Allow to set custom auto error detail
Allow to set custom auto error detail for HTTP Security dependencies
This commit is contained in:
parent
be87690255
commit
d2cb0f73df
|
|
@ -178,11 +178,24 @@ class HTTPBasic(HTTPBase):
|
|||
"""
|
||||
),
|
||||
] = True,
|
||||
auto_error_detail: Annotated[
|
||||
str,
|
||||
Doc(
|
||||
"""
|
||||
The text to be returned to the client when `auto_error`
|
||||
raises an HTTP exception.
|
||||
|
||||
It useful when you have multiple errors defined: set
|
||||
different detail text to easily differentiate which error was raised.
|
||||
"""
|
||||
),
|
||||
] = "Not authenticated",
|
||||
):
|
||||
self.model = HTTPBaseModel(scheme="basic", description=description)
|
||||
self.scheme_name = scheme_name or self.__class__.__name__
|
||||
self.realm = realm
|
||||
self.auto_error = auto_error
|
||||
self.auto_error_detail = auto_error_detail
|
||||
|
||||
async def __call__( # type: ignore
|
||||
self, request: Request
|
||||
|
|
@ -197,7 +210,7 @@ class HTTPBasic(HTTPBase):
|
|||
if self.auto_error:
|
||||
raise HTTPException(
|
||||
status_code=HTTP_401_UNAUTHORIZED,
|
||||
detail="Not authenticated",
|
||||
detail=self.auto_error_detail,
|
||||
headers=unauthorized_headers,
|
||||
)
|
||||
else:
|
||||
|
|
@ -293,10 +306,23 @@ class HTTPBearer(HTTPBase):
|
|||
"""
|
||||
),
|
||||
] = True,
|
||||
auto_error_detail: Annotated[
|
||||
str,
|
||||
Doc(
|
||||
"""
|
||||
The text to be returned to the client when `auto_error`
|
||||
raises an HTTP exception.
|
||||
|
||||
It useful when you have multiple errors defined: set
|
||||
different detail text to easily differentiate which error was raised.
|
||||
"""
|
||||
),
|
||||
] = "Not authenticated",
|
||||
):
|
||||
self.model = HTTPBearerModel(bearerFormat=bearerFormat, description=description)
|
||||
self.scheme_name = scheme_name or self.__class__.__name__
|
||||
self.auto_error = auto_error
|
||||
self.auto_error_detail = auto_error_detail
|
||||
|
||||
async def __call__(
|
||||
self, request: Request
|
||||
|
|
@ -306,7 +332,8 @@ class HTTPBearer(HTTPBase):
|
|||
if not (authorization and scheme and credentials):
|
||||
if self.auto_error:
|
||||
raise HTTPException(
|
||||
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
|
||||
status_code=HTTP_403_FORBIDDEN,
|
||||
detail=self.auto_error_detail,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
|
@ -395,10 +422,23 @@ class HTTPDigest(HTTPBase):
|
|||
"""
|
||||
),
|
||||
] = True,
|
||||
auto_error_detail: Annotated[
|
||||
str,
|
||||
Doc(
|
||||
"""
|
||||
The text to be returned to the client when `auto_error`
|
||||
raises an HTTP exception.
|
||||
|
||||
It useful when you have multiple errors defined: set
|
||||
different detail text to easily differentiate which error was raised.
|
||||
"""
|
||||
),
|
||||
] = "Not authenticated",
|
||||
):
|
||||
self.model = HTTPBaseModel(scheme="digest", description=description)
|
||||
self.scheme_name = scheme_name or self.__class__.__name__
|
||||
self.auto_error = auto_error
|
||||
self.auto_error_detail = auto_error_detail
|
||||
|
||||
async def __call__(
|
||||
self, request: Request
|
||||
|
|
@ -408,7 +448,8 @@ class HTTPDigest(HTTPBase):
|
|||
if not (authorization and scheme and credentials):
|
||||
if self.auto_error:
|
||||
raise HTTPException(
|
||||
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
|
||||
status_code=HTTP_403_FORBIDDEN,
|
||||
detail=self.auto_error_detail,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
|
|
|||
Loading…
Reference in New Issue