summaryrefslogtreecommitdiff
path: root/cherrypy/lib/auth_basic.py
diff options
context:
space:
mode:
authorSviatoslav Sydorenko <wk@sydorenko.org.ua>2018-04-22 13:14:02 +0200
committerSviatoslav Sydorenko <wk@sydorenko.org.ua>2018-04-22 13:14:02 +0200
commit13e9f4019707722318e27e3574f67c837ecf60c1 (patch)
treeb25c5f954e88f891fa9f12fa79e4a0bdf503aac0 /cherrypy/lib/auth_basic.py
parent1af9a1d6342a50c5dc8ce61dd7b6abf70f94d301 (diff)
parent7247bf76d619197161c0440e15abe384833ffdc5 (diff)
downloadcherrypy-git-14.2.0.tar.gz
Support unicode in login/password during HTTP Authv14.2.0
Ref: :rfc:`7617`
Diffstat (limited to 'cherrypy/lib/auth_basic.py')
-rw-r--r--cherrypy/lib/auth_basic.py53
1 files changed, 41 insertions, 12 deletions
diff --git a/cherrypy/lib/auth_basic.py b/cherrypy/lib/auth_basic.py
index 44eb9b14..59e95f3c 100644
--- a/cherrypy/lib/auth_basic.py
+++ b/cherrypy/lib/auth_basic.py
@@ -1,14 +1,9 @@
# This file is part of CherryPy <http://www.cherrypy.org/>
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab:fileencoding=utf-8
+"""HTTP Basic Authentication tool.
-import binascii
-
-import cherrypy
-from cherrypy._cpcompat import base64_decode
-
-
-__doc__ = """This module provides a CherryPy 3.x tool which implements
+This module provides a CherryPy 3.x tool which implements
the server-side of HTTP Basic Access Authentication, as described in
:rfc:`2617`.
@@ -20,11 +15,19 @@ as the credentials store::
basic_auth = {'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'earth',
'tools.auth_basic.checkpassword': checkpassword,
+ 'tools.auth_basic.accept_charset': 'UTF-8',
}
app_config = { '/' : basic_auth }
"""
+import binascii
+import unicodedata
+
+import cherrypy
+from cherrypy._cpcompat import base64_decode, ntob, ntou, tonative
+
+
__author__ = 'visteya'
__date__ = 'April 2009'
@@ -44,9 +47,10 @@ def checkpassword_dict(user_password_dict):
return checkpassword
-def basic_auth(realm, checkpassword, debug=False):
+def basic_auth(realm, checkpassword, debug=False, accept_charset='utf-8'):
"""A CherryPy tool which hooks at before_handler to perform
- HTTP Basic Access Authentication, as specified in :rfc:`2617`.
+ HTTP Basic Access Authentication, as specified in :rfc:`2617`
+ and :rfc:`7617`.
If the request has an 'authorization' header with a 'Basic' scheme, this
tool attempts to authenticate the credentials supplied in that header. If
@@ -66,6 +70,8 @@ def basic_auth(realm, checkpassword, debug=False):
"""
+ fallback_charset = 'ISO-8859-1'
+
if '"' in realm:
raise ValueError('Realm cannot contain the " (quote) character.')
request = cherrypy.serving.request
@@ -77,15 +83,38 @@ def basic_auth(realm, checkpassword, debug=False):
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)
+ decoded_params = base64_decode(params)
+ decoded_params = ntob(decoded_params)
+
+ last_err = None
+ for charset in (accept_charset, fallback_charset):
+ try:
+ decoded_params = tonative(decoded_params, charset)
+ break
+ except ValueError as ve:
+ last_err = ve
+ else:
+ raise last_err
+
+ decoded_params = ntou(decoded_params)
+ decoded_params = unicodedata.normalize('NFC', decoded_params)
+ decoded_params = tonative(decoded_params)
+ username, password = decoded_params.split(':', 1)
if checkpassword(realm, username, password):
if debug:
cherrypy.log('Auth succeeded', 'TOOLS.AUTH_BASIC')
request.login = username
return # successful authentication
+ charset = accept_charset.upper()
+ charset_declaration = (
+ (', charset="%s"' % charset)
+ if charset != fallback_charset
+ else ''
+ )
# Respond with 401 status and a WWW-Authenticate header
- cherrypy.serving.response.headers[
- 'www-authenticate'] = 'Basic realm="%s"' % realm
+ cherrypy.serving.response.headers['www-authenticate'] = (
+ 'Basic realm="%s"%s' % (realm, charset_declaration)
+ )
raise cherrypy.HTTPError(
401, 'You are not authorized to access that resource')