Models
- class nginx_ldap_auth.app.models.UserManager[source]
Manage users in the LDAP directory.
- settings
The application settings
- pool: TimeLimitedAIOConnectionPool | None
The LDAP connection pool
- client() LDAPClient[source]
Return a new LDAP client instance.
If
nginx_ldap_auth.settings.Settings.ldap_starttlsisTrue, the client will be configured to use TLS.
- async authenticate(username: str, password: str) bool[source]
Authenticate a user against the LDAP server.
If
nginx_ldap_auth.settings.Settings.ldap_user_basednis set, we will prepend the username with that value to create the DN to bind with like so: “{username}{ldap_user_base_dn}. Otherwise, we will use the value ofnginx_ldap_auth.settings.Settings.ldap_username_attributeto create the DN as{username_attribute}={username},{ldap_basedn}.- Parameters:
username – the username to authenticate
password – the password to authenticate with
- Raises:
LDAPError – if an error occurs while communicating with the LDAP server
- Returns:
Trueif the user is authenticated,Falseotherwise
- async exists(username: str) bool[source]
Return
Trueif the user exists in the LDAP directory,Falseotherwise.- Parameters:
username – the username to check
- Raises:
LDAPError – if an error occurred while communicating with the LDAP server
AuthenticationError – if the LDAP server rejects the credentials of
nginx_ldap_auth.settings.Settings.ldap_binddnandnginx_ldap_auth.settings.Settings.ldap_password
- Returns:
Trueif the user exists in the LDAP directory,Falseotherwise
- async is_authorized(username: str, ldap_authorization_filter: str | None) bool[source]
Test whether the user is authorized to log in. This is done by performing an LDAP search using the filter specified in a header or
nginx_ldap_auth.settings.Settings.ldap_authorization_filter. If the value isNone, the user is considered authorized.- Parameters:
username – the username to check
ldap_authorization_filter – LDAP authorization filter (optional)
- Raises:
LDAPError – if an error occurred while communicating with the LDAP server
AuthenticationError – if the LDAP server rejects the credentials of
nginx_ldap_auth.settings.Settings.ldap_binddnandnginx_ldap_auth.settings.Settings.ldap_password
- Returns:
Trueif the user is authorized to log in,Falseotherwise.
- async get(username: str) User | None[source]
Get a user from the LDAP directory, and return it as a
User. When getting the user, we will use the LDAP search filter specified innginx_ldap_auth.settings.Settings.ldap_get_user_filter.- Parameters:
username – the username for which to get user information
- Raises:
LDAPError – if an error occurred while communicating with the LDAP server
AuthenticationError – if the LDAP server rejects the credentials of
nginx_ldap_auth.settings.Settings.ldap_binddnandnginx_ldap_auth.settings.Settings.ldap_password
- Returns:
The user information as a
Userinstance, orNoneif the user is not returned by the LDAP search filter
- class nginx_ldap_auth.app.models.User(*, uid: str, full_name: str)[source]
Used to represent a user in the LDAP directory. It is constructed from the LDAP response, and is used to authenticate the user against the LDAP server.
- async authenticate(password: str) bool[source]
Authenticate this user against the LDAP server.
- Parameters:
password – the password to authenticate with
- Returns:
Trueif the user is authenticated,Falseotherwise
- classmethod parse_ldap(data: dict[str, list[str]]) User[source]
Parse the LDAP response, and extract the uid and full name from the LDAP server to use in constructing this class.
We use
nginx_ldap_auth.settings.Settings.ldap_username_attributeto determine which LDAP attribute ondataholds ouruidvalue, andnginx_ldap_auth.settings.Settings.ldap_full_name_attributeto determine which LDAP attribute holds ourfull_namevalue.- Parameters:
data – the raw LDAP data
- Returns:
A configured
Userobject
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].