Merge pull request #871 from Frzk/unauthorized-exception
Simplified the Unauthorized exception __init__ signature.
This commit is contained in:
commit
402c3752c4
|
@ -208,44 +208,39 @@ class Unauthorized(SanicException):
|
||||||
"""
|
"""
|
||||||
Unauthorized exception (401 HTTP status code).
|
Unauthorized exception (401 HTTP status code).
|
||||||
|
|
||||||
|
:param message: Message describing the exception.
|
||||||
:param scheme: Name of the authentication scheme to be used.
|
:param scheme: Name of the authentication scheme to be used.
|
||||||
:param challenge: A dict containing values to add to the WWW-Authenticate
|
|
||||||
header that is generated. This is especially useful when dealing with
|
When present, kwargs is used to complete the WWW-Authentication header.
|
||||||
the Digest scheme. (optional)
|
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
# With a Basic auth-scheme, realm MUST be present:
|
# With a Basic auth-scheme, realm MUST be present:
|
||||||
challenge = {"realm": "Restricted Area"}
|
raise Unauthorized("Auth required.", "Basic", realm="Restricted Area")
|
||||||
raise Unauthorized("Auth required.", "Basic", challenge)
|
|
||||||
|
|
||||||
# With a Digest auth-scheme, things are a bit more complicated:
|
# With a Digest auth-scheme, things are a bit more complicated:
|
||||||
challenge = {
|
raise Unauthorized("Auth required.",
|
||||||
"realm": "Restricted Area",
|
"Digest",
|
||||||
"qop": "auth, auth-int",
|
realm="Restricted Area",
|
||||||
"algorithm": "MD5",
|
qop="auth, auth-int",
|
||||||
"nonce": "abcdef",
|
algorithm="MD5",
|
||||||
"opaque": "zyxwvu"
|
nonce="abcdef",
|
||||||
}
|
opaque="zyxwvu")
|
||||||
raise Unauthorized("Auth required.", "Digest", challenge)
|
|
||||||
|
|
||||||
# With a Bearer auth-scheme, realm is optional:
|
# With a Bearer auth-scheme, realm is optional so you can write:
|
||||||
challenge = {"realm": "Restricted Area"}
|
raise Unauthorized("Auth required.", "Bearer")
|
||||||
raise Unauthorized("Auth required.", "Bearer", challenge)
|
|
||||||
|
# or, if you want to specify the realm:
|
||||||
|
raise Unauthorized("Auth required.", "Bearer", realm="Restricted Area")
|
||||||
"""
|
"""
|
||||||
pass
|
def __init__(self, message, scheme, **kwargs):
|
||||||
|
|
||||||
def __init__(self, message, scheme, challenge=None):
|
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|
||||||
chal = ""
|
values = ["{!s}={!r}".format(k, v) for k, v in kwargs.items()]
|
||||||
|
challenge = ', '.join(values)
|
||||||
if challenge is not None:
|
|
||||||
values = ["{!s}={!r}".format(k, v) for k, v in challenge.items()]
|
|
||||||
chal = ', '.join(values)
|
|
||||||
|
|
||||||
self.headers = {
|
self.headers = {
|
||||||
"WWW-Authenticate": "{} {}".format(scheme, chal).rstrip()
|
"WWW-Authenticate": "{} {}".format(scheme, challenge).rstrip()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -33,18 +33,17 @@ def exception_app():
|
||||||
|
|
||||||
@app.route('/401/basic')
|
@app.route('/401/basic')
|
||||||
def handler_401_basic(request):
|
def handler_401_basic(request):
|
||||||
raise Unauthorized("Unauthorized", "Basic", {"realm": "Sanic"})
|
raise Unauthorized("Unauthorized", "Basic", realm="Sanic")
|
||||||
|
|
||||||
@app.route('/401/digest')
|
@app.route('/401/digest')
|
||||||
def handler_401_digest(request):
|
def handler_401_digest(request):
|
||||||
challenge = {
|
raise Unauthorized("Unauthorized",
|
||||||
"realm": "Sanic",
|
"Digest",
|
||||||
"qop": "auth, auth-int",
|
realm="Sanic",
|
||||||
"algorithm": "MD5",
|
qop="auth, auth-int",
|
||||||
"nonce": "abcdef",
|
algorithm="MD5",
|
||||||
"opaque": "zyxwvu",
|
nonce="abcdef",
|
||||||
}
|
opaque="zyxwvu")
|
||||||
raise Unauthorized("Unauthorized", "Digest", challenge)
|
|
||||||
|
|
||||||
@app.route('/401/bearer')
|
@app.route('/401/bearer')
|
||||||
def handler_401_bearer(request):
|
def handler_401_bearer(request):
|
||||||
|
@ -122,7 +121,7 @@ def test_forbidden_exception(exception_app):
|
||||||
request, response = exception_app.test_client.get('/403')
|
request, response = exception_app.test_client.get('/403')
|
||||||
assert response.status == 403
|
assert response.status == 403
|
||||||
|
|
||||||
|
|
||||||
def test_unauthorized_exception(exception_app):
|
def test_unauthorized_exception(exception_app):
|
||||||
"""Test the built-in Unauthorized exception"""
|
"""Test the built-in Unauthorized exception"""
|
||||||
request, response = exception_app.test_client.get('/401/basic')
|
request, response = exception_app.test_client.get('/401/basic')
|
||||||
|
@ -132,7 +131,7 @@ def test_unauthorized_exception(exception_app):
|
||||||
|
|
||||||
request, response = exception_app.test_client.get('/401/digest')
|
request, response = exception_app.test_client.get('/401/digest')
|
||||||
assert response.status == 401
|
assert response.status == 401
|
||||||
|
|
||||||
auth_header = response.headers.get('WWW-Authenticate')
|
auth_header = response.headers.get('WWW-Authenticate')
|
||||||
assert auth_header is not None
|
assert auth_header is not None
|
||||||
assert auth_header.startswith('Digest')
|
assert auth_header.startswith('Digest')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user