import base64


def check_basic_auth(request, expected_auth_string):
    """
    Returns True if request has Basic Authorization header matching
    expected_auth_string.

    Given a request and an expected auth string (user:pass) in an HTTP
    Authorization header.
    """
    if 'HTTP_AUTHORIZATION' not in request.META:
        return False

    parts = request.META['HTTP_AUTHORIZATION'].split(' ')
    if len(parts) != 2:
        return False

    (auth_type, auth_string) = parts

    if auth_type.lower() != 'basic':
        return False

    try:
        return base64.b64decode(auth_string) == expected_auth_string
    except TypeError:
        return False