Skip to content

Instantly share code, notes, and snippets.

@nilsdebruin
Created April 15, 2019 11:33
Show Gist options
  • Select an option

  • Save nilsdebruin/bb0b8e5924adef7fc7082cd26b1bb65f to your computer and use it in GitHub Desktop.

Select an option

Save nilsdebruin/bb0b8e5924adef7fc7082cd26b1bb65f to your computer and use it in GitHub Desktop.
class BasicAuth(SecurityBase):
def __init__(self, scheme_name: str = None, auto_error: bool = True):
self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error
async def __call__(self, request: Request) -> Optional[str]:
authorization: str = request.headers.get("Authorization")
scheme, param = get_authorization_scheme_param(authorization)
if not authorization or scheme.lower() != "basic":
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
)
else:
return None
return param
basic_auth = BasicAuth(auto_error=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment