diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2018-05-29 22:20:04 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2018-05-29 22:32:46 -0400 |
commit | 681045c219624f6e50c6643ef26133bd12cd6b63 (patch) | |
tree | 5597522c8bf937ea4c04b332a3cef0199e618d76 | |
parent | 3eb859974d4a5582fefb978916d1b60c7d3adc27 (diff) | |
download | cherrypy-git-681045c219624f6e50c6643ef26133bd12cd6b63.tar.gz |
Extract _try_decode function
-rw-r--r-- | cherrypy/lib/auth_basic.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/cherrypy/lib/auth_basic.py b/cherrypy/lib/auth_basic.py index 59e95f3c..45f6227d 100644 --- a/cherrypy/lib/auth_basic.py +++ b/cherrypy/lib/auth_basic.py @@ -83,19 +83,10 @@ def basic_auth(realm, checkpassword, debug=False, accept_charset='utf-8'): with cherrypy.HTTPError.handle((ValueError, binascii.Error), 400, msg): scheme, params = auth_header.split(' ', 1) if scheme.lower() == 'basic': + charsets = accept_charset, fallback_charset 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 = _try_decode(decoded_params, charsets) decoded_params = ntou(decoded_params) decoded_params = unicodedata.normalize('NFC', decoded_params) decoded_params = tonative(decoded_params) @@ -118,3 +109,16 @@ def basic_auth(realm, checkpassword, debug=False, accept_charset='utf-8'): ) raise cherrypy.HTTPError( 401, 'You are not authorized to access that resource') + + +def _try_decode(subject, charsets): + last_err = None + for charset in charsets: + try: + subject = tonative(subject, charset) + break + except ValueError as ve: + last_err = ve + else: + raise last_err + return subject |