import re


def split_auth_header(header):
    """Split comma-separate key=value fields from a WWW-Authenticate header"""
    fields = {}
    remaining = header.strip()
    while remaining:
        matches = re.match(r'^([0-9a-zA-Z]+)="([^"]+)"(?:,\s*(.+))?$', remaining)
        if not matches:
            # Without quotes
            matches = re.match(r'^([0-9a-zA-Z]+)=([^",]+)(?:,\s*(.+))?$', remaining)
        if not matches:
            raise ValueError("Unable to parse auth header {}".format(repr(header)))
        key, value, remaining = matches.groups()
        fields[key] = value
    return fields