Skip to content

Commit f7533ae

Browse files
fundiesGreg Williamson
authored andcommitted
Add login_template and group_required variables
1 parent cb664b1 commit f7533ae

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ To configure LDAP, create a yaml file with a dictionary containing another dicti
7070
``timeout``
7171
The timeout for connections to the LDAP server. Defaults to 10 seconds.
7272

73+
``login_template``
74+
Template to insert the result from ``user_search`` into before attempting login
75+
76+
``group_required``
77+
Require the group search to be successful for authentication
78+
7379
The ``user_search`` and ``group_search`` settings are dictionaries with the following options:
7480

7581
``base``
@@ -90,7 +96,7 @@ The ``user_search`` and ``group_search`` settings are dictionaries with the foll
9096
password. ``devpi-ldap`` will extract this attribute from the search results and attempt to
9197
bind to the LDAP server using this DN and the password supplied by the user. If this bind
9298
succeeds, access is granted.
93-
99+
94100
``userdn``
95101
The distinguished name of the user which should be used for the search operation.
96102
For ``user_search``, if you don't have anonymous user search or for ``group_search`` if the users can't search their own groups, then you need to set this to a user which has the necessary rights.

devpi_ldap/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def __init__(self, path):
8080
'referrals',
8181
'reject_as_unknown',
8282
'tls',
83+
'login_template',
84+
'group_required'
8385
))
8486
unknown_keys = set(self.keys()) - known_keys
8587
if unknown_keys:
@@ -256,7 +258,11 @@ def validate(self, username, password):
256258
else:
257259
threadlog.debug("Validating user '%s' against LDAP at %s." % (username, self['url']))
258260
username = escape(username)
259-
userdn = self._userdn(username)
261+
if 'login_template' in self:
262+
userdn = self['login_template'].format(self._userdn(username))
263+
else:
264+
userdn = self._userdn(username)
265+
260266
if not userdn:
261267
return dict(status="unknown")
262268
if not password.strip():
@@ -268,6 +274,9 @@ def validate(self, username, password):
268274
if not config:
269275
return dict(status="ok")
270276
groups = self._search(conn, config, username=username, userdn=userdn)
277+
group_required = self.get('group_required', False)
278+
if group_required and len(groups) < 1:
279+
return self._rejection()
271280
return dict(status="ok", groups=groups)
272281

273282

@@ -342,3 +351,4 @@ def main(argv=None):
342351
raise SystemExit(2)
343352

344353
print("Authentication successful, the user is member of the following groups: %s" % ', '.join(result.get("groups", [])))
354+

0 commit comments

Comments
 (0)