# 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.

This module provides a CherryPy 3.x tool which implements
the server-side of HTTP Basic Access Authentication, as described in
:rfc:`2617`.

Example usage, using the built-in checkpassword_dict function which uses a dict
as the credentials store::

    userpassdict = {'bird' : 'bebop', 'ornette' : 'wayout'}
    checkpassword = cherrypy.lib.auth_basic.checkpassword_dict(userpassdict)
    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'


def checkpassword_dict(user_password_dict):
    """Returns a checkpassword function which checks credentials
    against a dictionary of the form: {username : password}.

    If you want a simple dictionary-based authentication scheme, use
    checkpassword_dict(my_credentials_dict) as the value for the
    checkpassword argument to basic_auth().
    """
    def checkpassword(realm, user, password):
        p = user_password_dict.get(user)
        return p and p == password or False

    return checkpassword


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`
    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
    the request has no 'authorization' header, or if it does but the scheme is
    not 'Basic', or if authentication fails, the tool sends a 401 response with
    a 'WWW-Authenticate' Basic header.

    realm
        A string containing the authentication realm.

    checkpassword
        A callable which checks the authentication credentials.
        Its signature is checkpassword(realm, username, password). where
        username and password are the values obtained from the request's
        'authorization' header.  If authentication succeeds, checkpassword
        returns True, else it returns False.

    """

    fallback_charset = 'ISO-8859-1'

    if '"' in realm:
        raise ValueError('Realm cannot contain the " (quote) character.')
    request = cherrypy.serving.request

    auth_header = request.headers.get('authorization')
    if auth_header is not None:
        # 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':
                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"%s' % (realm, charset_declaration)
    )
    raise cherrypy.HTTPError(
        401, 'You are not authorized to access that resource')

@@ -3050,46 +3050,51 @@
     
-decoded_params = base64_decode(params)
+charsets = accept_charset, fallback_charset
 %0A   
@@ -3127,29 +3127,30 @@
 s = 
-ntob(
+base64_
 decode
-d_
+(
 params)%0A
 %0A   
@@ -3145,17 +3145,16 @@
 params)%0A
-%0A
         
@@ -3165,124 +3165,46 @@
     
-last_err = None%0A                for charset in (accept_charset, fallback_charset):%0A                    try:%0A        
+decoded_params = ntob(decoded_params)%0A
     
@@ -3224,39 +3224,42 @@
 ecoded_params = 
-tonativ
+_try_decod
 e(decoded_params
@@ -3271,180 +3271,10 @@
 rset
+s
 )
-%0A                        break%0A                    except ValueError as ve:%0A                        last_err = ve%0A                else:%0A                    raise last_err%0A
 %0A   
@@ -4238,8 +4238,286 @@
 ource')%0A
+%0A%0Adef _try_decode(subject, charsets):%0A    last_err = None%0A    for charset in charsets:%0A        try:%0A            subject = tonative(subject, charset)%0A            break%0A        except ValueError as ve:%0A            last_err = ve%0A    else:%0A        raise last_err%0A    return subject%0A

Extract _try_decode function