Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def use_authorization(self, strategy: Optional[AuthorizationStrategy] = None) -> AuthorizationStrategy:
if self.started:
raise RuntimeError('The application is already running, configure authorization '
'before starting the application')
if not strategy:
strategy = AuthorizationStrategy()
if strategy.default_policy is None:
# by default, a default policy is configured with no requirements,
# meaning that request handlers allow anonymous users, unless specified otherwise
# this can be modified, by adding a requirement to the default policy
strategy.default_policy = Policy('default')
self._authorization_strategy = strategy
self.exceptions_handlers[AuthenticateChallenge] = handle_authentication_challenge
self.exceptions_handlers[UnauthorizedError] = handle_unauthorized
return strategy