def parse_authstring(authstring):
    """Parse an auth string into a dict

    Given an authentication header string [RFC2617], parse the fields and
    return a dict object of each key/pair
    """

    # Ensure the string starts with 'MAC '
    if not authstring or not authstring.startswith('MAC '):
        raise Exception("Could not parse authstring")

    pairs = authstring[4:].strip().split(',')

    avars = {}
    for pair in pairs:
        key, value = pair.strip().split("=", 1)
        avars[key] = value.strip('"')

    return avars