How to use the guardpost.authorization.AuthorizationError function in guardpost

To help you get started, we’ve selected a few guardpost examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RobertoPrevato / BlackSheep / blacksheep / server / authentication.py View on Github external
__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: