import base64


def extract_name_from_ldapquery(ldapquery):
    """
    Parses the output of the query_ldap_user_info function and returns the
    person's name in the "Firstname Surname" format.
    """
    common_name = ''
    if b'cn:: ' in ldapquery:  # base64 encoding
        # Extract base64 encoded common name from output
        common_name_b64 = ldapquery.split(b'cn:: ')[1].splitlines()[0]
        # Decode common name
        common_name = base64.b64decode(common_name_b64).decode('utf-8')
    else:
        common_name = ldapquery.split(
                b'cn: ')[1].splitlines()[0].decode('utf-8')

    return common_name