Skip to content

Commit cf23d49

Browse files
tirandmulder
andcommitted
LDAPUrl now treats urlscheme cases insensitive
Fixes: #280 Signed-off-by: Christian Heimes <cheimes@redhat.com> Co-authored-by: David Mulder <dmulder@suse.com>
1 parent 39ea8e5 commit cf23d49

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
@@ -66,6 +66,11 @@ A :py:class:`LDAPUrl` object represents a complete LDAP URL.
6666
.. autoclass:: ldapurl.LDAPUrl
6767
:members:
6868

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

7075
LDAP URL extensions
7176
^^^^^^^^^^^^^^^^^^^

Lib/ldapurl.py

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

4848

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

5954

6055
def ldapUrlEscape(s):
@@ -234,7 +229,7 @@ def __init__(
234229
extensions=None,
235230
who=None,cred=None
236231
):
237-
self.urlscheme=urlscheme
232+
self.urlscheme=urlscheme.lower()
238233
self.hostport=hostport
239234
self.dn=dn
240235
self.attrs=attrs
@@ -269,9 +264,7 @@ def _parse(self,ldap_url):
269264
if not isLDAPUrl(ldap_url):
270265
raise ValueError('Value %s for ldap_url does not seem to be a LDAP URL.' % (repr(ldap_url)))
271266
scheme,rest = ldap_url.split('://',1)
272-
self.urlscheme = scheme.strip()
273-
if not self.urlscheme in ['ldap','ldaps','ldapi']:
274-
raise ValueError('LDAP URL contains unsupported URL scheme %s.' % (self.urlscheme))
267+
self.urlscheme = scheme.lower()
275268
slash_pos = rest.find('/')
276269
qemark_pos = rest.find('?')
277270
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
@@ -38,18 +38,23 @@ class TestIsLDAPUrl(unittest.TestCase):
3838
'ldap://host.com:6666/o=University%20of%20Michigan,':1,
3939
'ldap://ldap.itd.umich.edu/c=GB?objectClass?one':1,
4040
'ldap://ldap.question.com/o=Question%3f,c=US?mail':1,
41-
'ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04)':1,
41+
'ldap://ldap.netscape.com/o=Babsco,c=US???(int=%5c00%5c00%5c00%5c04)':1,
4242
'ldap:///??sub??bindname=cn=Manager%2co=Foo':1,
4343
'ldap:///??sub??!bindname=cn=Manager%2co=Foo':1,
4444
# More examples from various sources
4545
'ldap://ldap.nameflow.net:1389/c%3dDE':1,
4646
'ldap://root.openldap.org/dc=openldap,dc=org':1,
47-
'ldap://root.openldap.org/dc=openldap,dc=org':1,
47+
'ldaps://root.openldap.org/dc=openldap,dc=org':1,
4848
'ldap://x500.mh.se/o=Mitthogskolan,c=se????1.2.752.58.10.2=T.61':1,
4949
'ldp://root.openldap.org/dc=openldap,dc=org':0,
5050
'ldap://localhost:1389/ou%3DUnstructured%20testing%20tree%2Cdc%3Dstroeder%2Cdc%3Dcom??one':1,
5151
'ldaps://ldap.example.com/c%3dDE':1,
5252
'ldapi:///dc=stroeder,dc=de????x-saslmech=EXTERNAL':1,
53+
'LDAP://localhost': True,
54+
'LDAPS://localhost': True,
55+
'LDAPI://%2Frun%2Fldap.sock': True,
56+
' ldap://space.example': False,
57+
'ldap ://space.example': False,
5358
}
5459

5560
def test_isLDAPUrl(self):
@@ -61,6 +66,11 @@ def test_isLDAPUrl(self):
6166
ldap_url, result, expected,
6267
)
6368
)
69+
if expected:
70+
LDAPUrl(ldapUrl=ldap_url)
71+
else:
72+
with self.assertRaises(Exception):
73+
LDAPUrl(ldapUrl=ldap_url)
6474

6575

6676
class TestParseLDAPUrl(unittest.TestCase):
@@ -149,6 +159,22 @@ class TestParseLDAPUrl(unittest.TestCase):
149159
dn='dc=stroeder,dc=com',
150160
),
151161
),
162+
(
163+
'LDAPS://localhost:12345/dc=stroeder,dc=com',
164+
LDAPUrl(
165+
urlscheme='ldaps',
166+
hostport='localhost:12345',
167+
dn='dc=stroeder,dc=com',
168+
),
169+
),
170+
(
171+
'ldaps://localhost:12345/dc=stroeder,dc=com',
172+
LDAPUrl(
173+
urlscheme='LDAPS',
174+
hostport='localhost:12345',
175+
dn='dc=stroeder,dc=com',
176+
),
177+
),
152178
(
153179
'ldapi://%2ftmp%2fopenldap2-1389/dc=stroeder,dc=com',
154180
LDAPUrl(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy