Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup additional LDAP user info #103

Merged
merged 7 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ If set to True, escape special chars in userdn when authenticating in LDAP.
On some LDAP servers, when userdn contains chars like '(', ')', '\' authentication may fail when those chars
are not escaped.

#### `LDAPAuthenticator.auth_state_attributes` ####

An optional list of attributes to be fetched for a user after login.
If found these will be returned as `auth_state`.
manics marked this conversation as resolved.
Show resolved Hide resolved

#### `LDAPAuthenticator.use_lookup_dn_username` ####

If set to True (the default) the username used to build the DN string is returned as the username when `lookup_dn` is True.
Expand Down
26 changes: 22 additions & 4 deletions ldapauthenticator/ldapauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ def _server_port_default(self):

attributes = List(config=True, help="List of attributes to be searched")

auth_state_attributes = List(
config=True, help="List of attributes to be returned in auth_state for a user"
)

use_lookup_dn_username = Bool(
True,
config=True,
Expand Down Expand Up @@ -287,6 +291,16 @@ def get_connection(self, userdn, password):
)
return conn

def get_user_attributes(self, conn, userdn):
attrs = {}
if self.auth_state_attributes:
found = conn.search(
userdn, "(objectClass=*)", attributes=self.auth_state_attributes
)
if found:
attrs = conn.entries[0].entry_attributes_as_dict
return attrs

@gen.coroutine
def authenticate(self, handler, data):
username = data["username"]
Expand Down Expand Up @@ -410,10 +424,14 @@ def authenticate(self, handler, data):
self.log.warning(msg.format(username=username))
return None

if self.use_lookup_dn_username:
return username
else:
return data["username"]
if not self.use_lookup_dn_username:
username = data["username"]

user_info = self.get_user_attributes(conn, userdn)
if user_info:
self.log.debug("username:%s attributes:%s", username, user_info)
return {"name": username, "auth_state": user_info}
return username


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions ldapauthenticator/tests/test_ldapauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ async def test_ldap_auth_search_filter(authenticator):
None, {"username": "zoidberg", "password": "zoidberg"}
)
assert authorized is None


async def test_ldap_auth_state_attributes(authenticator):
authenticator.auth_state_attributes = ["employeeType"]
# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
None, {"username": "fry", "password": "fry"}
)
assert authorized["name"] == "fry"
assert authorized["auth_state"] == {"employeeType": ["Delivery boy"]}
manics marked this conversation as resolved.
Show resolved Hide resolved