Skip to content

Commit 0ef6175

Browse files
tirandmulder
andauthored
LDAPUrl now treats urlscheme cases insensitive
#347 Fixes: #280 Signed-off-by: Christian Heimes <cheimes@redhat.com> Co-authored-by: David Mulder <dmulder@suse.com>
1 parent a66dcc3 commit 0ef6175

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

Doc/reference/ldapurl.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ A :py:class:`LDAPUrl` object represents a complete LDAP URL.
6565
.. autoclass:: ldapurl.LDAPUrl
6666
:members:
6767

68+
.. versionchanged:: 3.4.0
69+
70+
The urlscheme is now case insensitive and always converted to lower
71+
case. ``LDAP://localhost`` is equivalent to ``ldap://localhost``.
72+
6873

6974
LDAP URL extensions
7075
^^^^^^^^^^^^^^^^^^^

Lib/ldapurl.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,9 @@
4848

4949

5050
def isLDAPUrl(s):
51+
"""Returns True if s is a LDAP URL, else False
5152
"""
52-
Returns 1 if s is a LDAP URL, 0 else
53-
"""
54-
s_lower = s.lower()
55-
return \
56-
s_lower.startswith('ldap://') or \
57-
s_lower.startswith('ldaps://') or \
58-
s_lower.startswith('ldapi://')
53+
return s.lower().startswith(('ldap://', 'ldaps://', 'ldapi://'))
5954

6055

6156
def ldapUrlEscape(s):
@@ -235,7 +230,7 @@ def __init__(
235230
extensions=None,
236231
who=None,cred=None
237232
):
238-
self.urlscheme=urlscheme
233+
self.urlscheme=urlscheme.lower()
239234
self.hostport=hostport
240235
self.dn=dn
241236
self.attrs=attrs
@@ -270,9 +265,7 @@ def _parse(self,ldap_url):
270265
if not isLDAPUrl(ldap_url):
271266
raise ValueError('Value %s for ldap_url does not seem to be a LDAP URL.' % (repr(ldap_url)))
272267
scheme,rest = ldap_url.split('://',1)
273-
self.urlscheme = scheme.strip()
274-
if not self.urlscheme in ['ldap','ldaps','ldapi']:
275-
raise ValueError('LDAP URL contains unsupported URL scheme %s.' % (self.urlscheme))
268+
self.urlscheme = scheme.lower()
276269
slash_pos = rest.find('/')
277270
qemark_pos = rest.find('?')
278271
if (slash_pos==-1) and (qemark_pos==-1):

Tests/t_ldapurl.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@ class TestIsLDAPUrl(unittest.TestCase):
3333
'ldap://host.com:6666/o=University%20of%20Michigan,':1,
3434
'ldap://ldap.itd.umich.edu/c=GB?objectClass?one':1,
3535
'ldap://ldap.question.com/o=Question%3f,c=US?mail':1,
36-
'ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04)':1,
36+
'ldap://ldap.netscape.com/o=Babsco,c=US???(int=%5c00%5c00%5c00%5c04)':1,
3737
'ldap:///??sub??bindname=cn=Manager%2co=Foo':1,
3838
'ldap:///??sub??!bindname=cn=Manager%2co=Foo':1,
3939
# More examples from various sources
4040
'ldap://ldap.nameflow.net:1389/c%3dDE':1,
4141
'ldap://root.openldap.org/dc=openldap,dc=org':1,
42-
'ldap://root.openldap.org/dc=openldap,dc=org':1,
42+
'ldaps://root.openldap.org/dc=openldap,dc=org':1,
4343
'ldap://x500.mh.se/o=Mitthogskolan,c=se????1.2.752.58.10.2=T.61':1,
4444
'ldp://root.openldap.org/dc=openldap,dc=org':0,
4545
'ldap://localhost:1389/ou%3DUnstructured%20testing%20tree%2Cdc%3Dstroeder%2Cdc%3Dcom??one':1,
4646
'ldaps://ldap.example.com/c%3dDE':1,
4747
'ldapi:///dc=stroeder,dc=de????x-saslmech=EXTERNAL':1,
48+
'LDAP://localhost': True,
49+
'LDAPS://localhost': True,
50+
'LDAPI://%2Frun%2Fldap.sock': True,
51+
' ldap://space.example': False,
52+
'ldap ://space.example': False,
4853
}
4954

5055
def test_isLDAPUrl(self):
@@ -56,6 +61,11 @@ def test_isLDAPUrl(self):
5661
ldap_url, result, expected,
5762
)
5863
)
64+
if expected:
65+
LDAPUrl(ldapUrl=ldap_url)
66+
else:
67+
with self.assertRaises(ValueError):
68+
LDAPUrl(ldapUrl=ldap_url)
5969

6070

6171
class TestParseLDAPUrl(unittest.TestCase):
@@ -144,6 +154,22 @@ class TestParseLDAPUrl(unittest.TestCase):
144154
dn='dc=stroeder,dc=com',
145155
),
146156
),
157+
(
158+
'LDAPS://localhost:12345/dc=stroeder,dc=com',
159+
LDAPUrl(
160+
urlscheme='ldaps',
161+
hostport='localhost:12345',
162+
dn='dc=stroeder,dc=com',
163+
),
164+
),
165+
(
166+
'ldaps://localhost:12345/dc=stroeder,dc=com',
167+
LDAPUrl(
168+
urlscheme='LDAPS',
169+
hostport='localhost:12345',
170+
dn='dc=stroeder,dc=com',
171+
),
172+
),
147173
(
148174
'ldapi://%2ftmp%2fopenldap2-1389/dc=stroeder,dc=com',
149175
LDAPUrl(

0 commit comments

Comments
 (0)