Browse Source

Allow to set custom auto error detail

Allow to set custom auto error detail for HTTP Security dependencies
pull/5163/head
Suren Khorenyan 3 years ago
parent
commit
d2cb0f73df
  1. 47
      fastapi/security/http.py

47
fastapi/security/http.py

@ -178,11 +178,24 @@ class HTTPBasic(HTTPBase):
""" """
), ),
] = True, ] = 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.model = HTTPBaseModel(scheme="basic", description=description)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.realm = realm self.realm = realm
self.auto_error = auto_error self.auto_error = auto_error
self.auto_error_detail = auto_error_detail
async def __call__( # type: ignore async def __call__( # type: ignore
self, request: Request self, request: Request
@ -197,7 +210,7 @@ class HTTPBasic(HTTPBase):
if self.auto_error: if self.auto_error:
raise HTTPException( raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED, status_code=HTTP_401_UNAUTHORIZED,
detail="Not authenticated", detail=self.auto_error_detail,
headers=unauthorized_headers, headers=unauthorized_headers,
) )
else: else:
@ -293,10 +306,23 @@ class HTTPBearer(HTTPBase):
""" """
), ),
] = True, ] = 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.model = HTTPBearerModel(bearerFormat=bearerFormat, description=description)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error = auto_error
self.auto_error_detail = auto_error_detail
async def __call__( async def __call__(
self, request: Request self, request: Request
@ -306,7 +332,8 @@ class HTTPBearer(HTTPBase):
if not (authorization and scheme and credentials): if not (authorization and scheme and credentials):
if self.auto_error: if self.auto_error:
raise HTTPException( raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated" status_code=HTTP_403_FORBIDDEN,
detail=self.auto_error_detail,
) )
else: else:
return None return None
@ -395,10 +422,23 @@ class HTTPDigest(HTTPBase):
""" """
), ),
] = True, ] = 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.model = HTTPBaseModel(scheme="digest", description=description)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error = auto_error
self.auto_error_detail = auto_error_detail
async def __call__( async def __call__(
self, request: Request self, request: Request
@ -408,7 +448,8 @@ class HTTPDigest(HTTPBase):
if not (authorization and scheme and credentials): if not (authorization and scheme and credentials):
if self.auto_error: if self.auto_error:
raise HTTPException( raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated" status_code=HTTP_403_FORBIDDEN,
detail=self.auto_error_detail,
) )
else: else:
return None return None

Loading…
Cancel
Save