import zmq.auth
from zmq.auth.thread import ThreadAuthenticator

from clarabot_nano.common.zmq.router_base import AbstractZmqRouterBaseThreadMixin


def setup_server_auth(auth: ThreadAuthenticator, server: AbstractZmqRouterBaseThreadMixin):
    if server.security == "ec":
        # allow-any is fine since the server's public key will be secretly sent to the clients
        auth.configure_curve(domain='*', location=zmq.auth.CURVE_ALLOW_ANY)
    elif server.security == "basic":
        auth.configure_plain(domain='*', passwords={server.auth_username: server.auth_password})
    else:  # pragma: no cover
        raise Exception(f"unknown security setting: {server.security}")
