import base64


def auth_header_value(user_name, password, realm=None):
    """ Construct a value for the Authorization header based on the
    credentials supplied.

    :param user_name: the user name
    :param password: the password
    :param realm: the realm (optional)
    :return: string to be included in Authorization header
    """
    credentials = (user_name + ":" + password).encode("UTF-8")
    if realm:
        value = 'Basic realm="' + realm + '" '
    else:
        value = 'Basic '
    value += base64.b64encode(credentials).decode("ASCII")
    return value