streamlit-ldap-authenticator icon indicating copy to clipboard operation
streamlit-ldap-authenticator copied to clipboard

If dn doesn't contain user_name, it can't get user info

Open fengchutadie opened this issue 1 year ago • 1 comments

I'd like to login with username "foo" and password, but username is not in "dn" of my ldap server. my dn is such as "uid=1234,ou=people,o=example", not "uid=foo,ou=people,o=example".

when I login with username "foo" and password, I should get the correct dn of username "foo", such as "uid=1234,ou=people,o=example", then validate dn and related password with Connect() function.

but in ldap_authenticate.py, server = Server(self.config.server_path, use_ssl=self.config.use_ssl, get_info='ALL') conn = Connection(server, username, password, auto_bind=False, auto_referrals=False, raise_exceptions=False) it connects directly and username can't be replaced by correct dn.

is it possible to implemet similar function such as "_search_for_user_dn" in https://github.com/django-auth-ldap/django-auth-ldap/blob/master/django_auth_ldap/backend.py

Firstly it will get user dn according to username and then validate login.

fengchutadie avatar Nov 15 '24 12:11 fengchutadie

One option is to implement your own getLoginUserName function, then pass it to Authenticate.login:

def map_user_to_uid(username: str) -> int:
    # Your fancy mapping


def get_ldap_user_name(username: str) -> str:
    """Get the LDAP userfrom the given username."""
    uid = map_user_to_uid(username)
    return f"uid={uid},ou=people,o=example"

...
    auth = Authenticate(st.secrets["ldap"], st.secrets["session_state_names"], st.secrets["auth_cookie"])
    user = auth.login(getLoginUserName=get_ldap_user_name)

Dev-iL avatar May 04 '25 15:05 Dev-iL