Debian Codegraph @

test/testRebindOperation.py

raw
1 """
2 """
3
4 # Created on 2013.06.06
5 #
6 # Author: Giovanni Cannata
7 #
8 # Copyright 2013 - 2020 Giovanni Cannata
9 #
10 # This file is part of ldap3.
11 #
12 # ldap3 is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU Lesser General Public License as published
14 # by the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # ldap3 is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU Lesser General Public License for more details.
21 #
22 # You should have received a copy of the GNU Lesser General Public License
23 # along with ldap3 in the COPYING and COPYING.LESSER files.
24 # If not, see <http://www.gnu.org/licenses/>.
25
26 import unittest
27
28 from ldap3 import ANONYMOUS, SASL, NTLM, DIGEST_MD5
29 from test.config import test_sasl_user, test_sasl_password, random_id, get_connection, drop_connection, test_sasl_realm, \
30 test_server_type, test_ntlm_user, test_ntlm_password, test_secondary_user, test_secondary_password, \
31 test_sasl_secondary_user, test_sasl_secondary_password, test_sasl_secondary_user_dn, test_sasl_user_dn
32
33
34 class Test(unittest.TestCase):
35 def test_bind_clear_text_to_secondary_user(self):
36 connection = get_connection()
37 self.assertTrue(connection.bound)
38 connection.rebind(test_secondary_user, test_secondary_password)
39 if test_server_type == 'EDIR':
40 bound_dn = connection.extend.novell.get_bind_dn()
41 else:
42 bound_dn = connection.extend.standard.who_am_i()
43
44 if bound_dn:
45 if '\\' in bound_dn: # for Active Directory
46 domain, _, name = bound_dn.replace('u:', '').partition('\\')
47 self.assertTrue(domain in test_secondary_user)
48 self.assertTrue(name in test_secondary_user)
49 else:
50 self.assertTrue(test_secondary_user in bound_dn)
51 else:
52 self.fail('no user dn in extended response')
53
54 drop_connection(connection)
55 self.assertFalse(connection.bound)
56
57 def test_bind_anonymous_to_secondary_user(self):
58 connection = get_connection(bind=True, lazy_connection=False, authentication=ANONYMOUS)
59 self.assertTrue(connection.bound)
60 connection.rebind(test_secondary_user, test_secondary_password)
61 if test_server_type == 'EDIR':
62 bound_dn = connection.extend.novell.get_bind_dn()
63 else:
64 bound_dn = connection.extend.standard.who_am_i()
65
66 if bound_dn:
67 if '\\' in bound_dn: # for Active Directory
68 domain, _, name = bound_dn.replace('u:', '').partition('\\')
69 self.assertTrue(domain in test_secondary_user)
70 self.assertTrue(name in test_secondary_user)
71 else:
72 self.assertTrue(test_secondary_user in bound_dn)
73 else:
74 self.fail('no user dn in extended response')
75
76 drop_connection(connection)
77 self.assertFalse(connection.bound)
78
79 def test_bind_sasl_digest_md5_to_secondary_sasl_user(self):
80 if test_server_type not in ['AD', 'SLAPD']:
81 connection = get_connection(bind=False, authentication=SASL, sasl_mechanism=DIGEST_MD5, sasl_credentials=(test_sasl_realm, test_sasl_user, test_sasl_password, None))
82 connection.open()
83 connection.bind()
84 self.assertTrue(connection.bound)
85 if test_server_type == 'EDIR':
86 connected_user = connection.extend.novell.get_bind_dn()
87 else:
88 connected_user = str(connection.extend.standard.who_am_i())
89 self.assertEqual(connected_user, test_sasl_user_dn)
90
91 if connection.rebind(authentication=SASL, sasl_mechanism=DIGEST_MD5, sasl_credentials=(test_sasl_realm, test_sasl_secondary_user, test_sasl_secondary_password, None)):
92 if test_server_type == 'EDIR':
93 connected_user = connection.extend.novell.get_bind_dn()
94 else:
95 connected_user = connection.extend.standard.who_am_i()
96
97 self.assertEqual(connected_user, test_sasl_secondary_user_dn)
98 else:
99 self.fail('secondary user sasl authentication failed')
100
101 drop_connection(connection)
102 self.assertFalse(connection.bound)
103
104 def test_ntlm(self):
105 if test_server_type == 'AD':
106 connection = get_connection(bind=False, authentication=NTLM, ntlm_credentials=(test_ntlm_user, test_ntlm_password))
107 connection.open()
108 connection.bind()
109 self.assertTrue(connection.bound)
110 connected_user = str(connection.extend.standard.who_am_i())[2:]
111 self.assertEqual(connected_user, test_ntlm_user)
112 drop_connection(connection)
113 self.assertFalse(connection.bound)