summaryrefslogtreecommitdiff
path: root/cherrypy/lib/auth_basic.py
diff options
context:
space:
mode:
Diffstat (limited to 'cherrypy/lib/auth_basic.py')
-rw-r--r--cherrypy/lib/auth_basic.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/cherrypy/lib/auth_basic.py b/cherrypy/lib/auth_basic.py
index 5ba16f7f..44eb9b14 100644
--- a/cherrypy/lib/auth_basic.py
+++ b/cherrypy/lib/auth_basic.py
@@ -2,6 +2,12 @@
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab:fileencoding=utf-8
+import binascii
+
+import cherrypy
+from cherrypy._cpcompat import base64_decode
+
+
__doc__ = """This module provides a CherryPy 3.x tool which implements
the server-side of HTTP Basic Access Authentication, as described in
:rfc:`2617`.
@@ -22,10 +28,6 @@ as the credentials store::
__author__ = 'visteya'
__date__ = 'April 2009'
-import binascii
-from cherrypy._cpcompat import base64_decode
-import cherrypy
-
def checkpassword_dict(user_password_dict):
"""Returns a checkpassword function which checks credentials
@@ -70,7 +72,9 @@ def basic_auth(realm, checkpassword, debug=False):
auth_header = request.headers.get('authorization')
if auth_header is not None:
- try:
+ # split() error, base64.decodestring() error
+ msg = 'Bad Request'
+ with cherrypy.HTTPError.handle((ValueError, binascii.Error), 400, msg):
scheme, params = auth_header.split(' ', 1)
if scheme.lower() == 'basic':
username, password = base64_decode(params).split(':', 1)
@@ -79,12 +83,9 @@ def basic_auth(realm, checkpassword, debug=False):
cherrypy.log('Auth succeeded', 'TOOLS.AUTH_BASIC')
request.login = username
return # successful authentication
- # split() error, base64.decodestring() error
- except (ValueError, binascii.Error):
- raise cherrypy.HTTPError(400, 'Bad Request')
# Respond with 401 status and a WWW-Authenticate header
cherrypy.serving.response.headers[
'www-authenticate'] = 'Basic realm="%s"' % realm
raise cherrypy.HTTPError(
- 401, "You are not authorized to access that resource")
+ 401, 'You are not authorized to access that resource')