[docs]classLdapAuthenticator(Authenticator):""" Authenticator for authenticating using an LDAP server. This requires the following extra parameters in the server configuration: * ``ldap_server`` - the URI of the LDAP server * ``ldap_bind`` - the bind string for the LDAP authentication (formatted to replace {username} with username) * ``ldap_query_user`` - the bind string for the LDAP query * ``ldap_query_password`` - the password for the LDAP query * ``ldap_query_base`` - the base point for the LDAP query * ``ldap_query_filter`` - the filter to apply to the LDAP query (formatted to replace {username} with username) """Name="LDAP"
[docs]defauthenticate(self,config:Config,request:Request)->Optional[User]:ldap_host=config.get_option("authentication.ldap_server")try:conn=ldap.initialize(ldap_host)exceptldap.LDAPErroraserr:# ty: ignore[unresolved-attribute]raiseAuthenticationError("failed to connect to ldap server")fromerrauth=request.authorizationifnotauth:returnNoneusername=auth.usernamepassword=auth.passwordldap_bind:str=config.get_string_option("authentication.ldap_bind")try:conn.simple_bind_s(ldap_bind.format(username=username),password)exceptldap.INVALID_CREDENTIALS:# ty: ignore[unresolved-attribute]returnNoneldap_query_user=config.get_option("authentication.ldap_query_user",default=None)ldap_query_password=config.get_option("authentication.ldap_query_password",default=None)ifldap_query_userisnotNone:conn.unbind_s()try:conn=ldap.initialize(ldap_host)exceptldap.LDAPErroraserr:# ty: ignore[unresolved-attribute]raiseAuthenticationError("failed to connect to ldap server")fromerrtry:conn.simple_bind_s(ldap_query_user,ldap_query_password)exceptldap.INVALID_CREDENTIALSaserr:# ty: ignore[unresolved-attribute]raiseAuthenticationError("failed to bind to LDAP server for user query")fromerrldap_query_base=config.get_option("authentication.ldap_query_base")ldap_query_filter=str(config.get_option("authentication.ldap_query_filter"))ldap_query_uid=config.get_option("authentication.ldap_query_uid",default="uid")ldap_query_mail=config.get_option("authentication.ldap_query_mail",default="mail")results=conn.search_s(ldap_query_base,ldap.SCOPE_SUBTREE,# ty: ignore[unresolved-attribute]ldap_query_filter.format(username=username),)try:user=results[0][1][ldap_query_uid][0].decode()mail=results[0][1][ldap_query_mail][0].decode()returnUser(user,mail)exceptExceptionaserr:raiseAuthenticationError("failed to find user in LDAP query")fromerr