Added a Unauthorized exception.

Also added a few tests related to this new exception.
This commit is contained in:
François KUBLER
2017-06-23 16:12:15 +02:00
parent b5369e611c
commit cf1713b085
2 changed files with 64 additions and 1 deletions

View File

@@ -198,6 +198,34 @@ class InvalidRangeType(ContentRangeError):
pass
@add_status_code(401)
class Unauthorized(SanicException):
"""
Unauthorized exception (401 HTTP status code).
:param scheme: Name of the authentication scheme to be used.
:param realm: Description of the protected area. (optional)
:param others: A dict containing values to add to the WWW-Authenticate
header that is generated. This is especially useful when dealing with the
Digest scheme. (optional)
"""
pass
def __init__(self, message, scheme, realm="", others=None):
super().__init__(message)
adds = ""
if others is not None:
values = ["{!s}={!r}".format(k, v) for k, v in others.items()]
adds = ', '.join(values)
adds = ', {}'.format(adds)
self.headers = {
"WWW-Authenticate": "{} realm='{}'{}".format(scheme, realm, adds)
}
def abort(status_code, message=None):
"""
Raise an exception based on SanicException. Returns the HTTP response