8000 s/plugin_map/auth_plugin_map/ · pkdevboxy/PyMySQL@a126c4e · GitHub
[go: up one dir, main page]

Skip to content

Commit a126c4e

Browse files
committed
s/plugin_map/auth_plugin_map/
1 parent 4ce7d7e commit a126c4e

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

pymysql/connections.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def __init__(self, host=None, user=None, password="",
536536
compress=None, named_pipe=None, no_delay=None,
537537
autocommit=False, db=None, passwd=None, local_infile=False,
538538
max_allowed_packet=16*1024*1024, defer_connect=False,
539-
plugin_map={}):
539+
auth_plugin_map={}):
540540
"""
541541
Establish a connection to the MySQL database. Accepts several
542542
arguments:
@@ -572,12 +572,11 @@ def __init__(self, host=None, user=None, password="",
572572
max_allowed_packet: Max size of packet sent to server in bytes. (default: 16MB)
573573
defer_connect: Don't explicitly connect on contruction - wait for connect call.
574574
(default: False)
575-
plugin_map: Map of plugin names to a class that processes that plugin. The class
576-
will take the Connection object as the argument to the constructor. The class
577-
needs an authenticate method taking an authentication packet as an argument.
578-
For the dialog plugin, a prompt(echo, prompt) method can be used (if no
579-
authenticate method) for returning a string from the user.
580-
575+
auth_plugin_map: A dict of plugin names to a class that processes that plugin.
576+
The class will take the Connection object as the argument to the constructor.
577+
The class needs an authenticate method taking an authentication packet as
578+
an argument. For the dialog plugin, a prompt(echo, prompt) method can be used
579+
(if no authenticate method) for returning a string from the user. (experimental)
581580
db: Alias for database. (for compatibility to MySQLdb)
582581
passwd: Alias for password. (for compatibility to MySQLdb)
583582
"""
@@ -673,7 +672,7 @@ def _config(key, arg):
673672
self.sql_mode = sql_mode
674673
self.init_command = init_command
675674
self.max_allowed_packet = max_allowed_packet
676-
self.plugin_map = plugin_map
675+
self._auth_plugin_map = auth_plugin_map
677676
if defer_connect:
678677
self.socket = None
679678
else:
@@ -1075,18 +1074,15 @@ def _request_authentication(self):
10751074

10761075
data = data_init + self.user + b'\0'
10771076

1078-
authresp = ''
1077+
authresp = b''
10791078
if self._auth_plugin_name == 'mysql_native_password':
10801079
authresp = _scramble(self.password.encode('latin1'), self.salt)
10811080

10821081
if self.server_capabilities & CLIENT.PLUGIN_AUTH_LENENC_CLIENT_DATA:
1083-
data += lenenc_int(len(authresp))
1084-
data += authresp
1082+
data += lenenc_int(len(authresp)) + authresp
10851083
elif self.server_capabilities & CLIENT.SECURE_CONNECTION:
1086-
length = len(authresp)
1087-
data += struct.pack('B', length)
1088-
data += authresp
1089-
else: # pragma: no cover - not testing against servers without secure auth (>=5.0)
1084+
data += struct.pack('B', len(authresp)) + authresp
1085+
else: # pragma: no cover - not testing against servers without secure auth (>=5.0)
10901086
data += authresp + b'\0'
10911087

10921088
if self.db and self.server_capabilities & CLIENT.CONNECT_WITH_DB:
@@ -1117,10 +1113,13 @@ def _request_authentication(self):
11171113
self.write_packet(data)
11181114
auth_packet = self._read_packet()
11191115

1116+
#TODO: ok packet or error packet?
1117+
1118+
11201119
def _process_auth(self, plugin_name, auth_packet):
1121-
plugin_class = self.plugin_map.get(plugin_name)
1120+
plugin_class = self._auth_plugin_map.get(plugin_name)
11221121
if not plugin_class:
1123-
plugin_class = self.plugin_map.get(plugin_name.decode('ascii'))
1122+
plugin_class = self._auth_plugin_map.get(plugin_name.decode('ascii'))
11241123
if plugin_class:
11251124
try:
11261125
handler = plugin_class(self)

pymysql/tests/test_connection.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def realTestDialogAuthTwoQuestions(self):
199199
self.databases[0]['db'], 'two_questions', 'notverysecret') as u:
200200
with self.assertRaises(pymysql.err.OperationalError):
201201
pymysql.connect(user='pymysql_2q', **self.db)
202-
pymysql.connect(user='pymysql_2q', plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
202+
pymysql.connect(user='pymysql_2q', auth_plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
203203

204204
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
205205
@unittest2.skipIf(three_attempts_found, "three_attempts plugin already installed")
@@ -226,21 +226,21 @@ def realTestDialogAuthThreeAttempts(self):
226226
TestAuthentication.Dialog.fail=True # fail just once. We've got three attempts after all
227227
with TempUser(self.connections[0].cursor(), 'pymysql_3a@localhost',
228228
self.databases[0]['db'], 'three_attempts', 'stillnotverysecret') as u:
229-
pymysql.connect(user='pymysql_3a', plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
230-
pymysql.connect(user='pymysql_3a', plugin_map={b'dialog': TestAuthentication.DialogHandler}, **self.db)
229+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
230+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': TestAuthentication.DialogHandler}, **self.db)
231231
with self.assertRaises(pymysql.err.OperationalError):
232-
pymysql.connect(user='pymysql_3a', plugin_map={b'dialog': object}, **self.db)
232+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': object}, **self.db)
233233

234234
with self.assertRaises(pymysql.err.OperationalError):
235-
pymysql.connect(user='pymysql_3a', plugin_map={b'dialog': TestAuthentication.DefectiveHandler}, **self.db)
235+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': TestAuthentication.DefectiveHandler}, **self.db)
236236
with self.assertRaises(pymysql.err.OperationalError):
237-
pymysql.connect(user='pymysql_3a', plugin_map={b'notdialogplugin': TestAuthentication.Dialog}, **self.db)
237+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'notdialogplugin': TestAuthentication.Dialog}, **self.db)
238238
TestAuthentication.Dialog.m = {b'Password, please:': b'I do not know'}
239239
with self.assertRaises(pymysql.err.OperationalError):
240-
pymysql.connect(user='pymysql_3a', plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
240+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
241241
TestAuthentication.Dialog.m = {b'Password, please:': None}
242242
with self.assertRaises(pymysql.err.OperationalError):
243-
pymysql.connect(user='pymysql_3a', plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
243+
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
244244

245245
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
246246
@unittest2.skipIf(pam_found, "pam plugin already installed")
@@ -286,12 +286,16 @@ def realTestPamAuth(self):
286286
c = pymysql.connect(user=TestAuthentication.osuser, **db)
287287
db['password'] = 'very bad guess at password'
288288
with self.assertRaises(pymysql.err.OperationalError):
289-
pymysql.connect(user=TestAuthentication.osuser, plugin_map={b'mysql_cleartext_password': TestAuthentication.DefectiveHandler}, **self.db)
289+
pymysql.connect(user=TestAuthentication.osuser,
290+
auth_plugin_map={b'mysql_cleartext_password': TestAuthentication.DefectiveHandler},
291+
**self.db)
290292
except pymysql.OperationalError as e:
291293
self.assertEqual(1045, e.args[0])
292294
# we had 'bad guess at password' work with pam. Well at least we get a permission denied here
293295
with self.assertRaises(pymysql.err.OperationalError):
294-
pymysql.connect(user=TestAuthentication.osuser, plugin_map={b'mysql_cleartext_password': TestAuthentication.DefectiveHandler}, **self.db)
296+
pymysql.connect(user=TestAuthentication.osuser,
297+
auth_plugin_map={b'mysql_cleartext_password': TestAuthentication.DefectiveHandler},
298+
**self.db)
295299
if grants:
296300
# recreate the user
297301
cur.execute(grants)

0 commit comments

Comments
 (0)
0