Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
__all__ = ('AuthenticationStrategy',
'AuthenticationHandler',
'AuthenticateChallenge',
'get_authentication_middleware',
'handle_authentication_challenge')
def get_authentication_middleware(strategy: AuthenticationStrategy):
async def authentication_middleware(request, handler):
await strategy.authenticate(request, getattr(handler, 'auth_schemes', None))
return await handler(request)
return authentication_middleware
class AuthenticateChallenge(AuthorizationError):
header_name = b'WWW-Authenticate'
def __init__(self,
scheme: str,
realm: Optional[str],
parameters: Optional[Dict[str, str]]):
self.scheme = scheme
self.realm = realm
self.parameters = parameters
def _get_header_value(self) -> bytes:
if not self.realm and not self.parameters:
return self.scheme.encode()
parts = bytearray(self.scheme.encode())
if self.realm: