File tree 4 files changed +38
-3
lines changed
4 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 2
2
branch = True
3
3
source =
4
4
pymysql
5
+ tests
5
6
omit = pymysql/tests/*
6
7
pymysql/tests/thirdparty/test_MySQLdb/*
7
8
Original file line number Diff line number Diff line change @@ -166,6 +166,8 @@ def sha256_password_auth(conn, pkt):
166
166
167
167
if pkt .is_auth_switch_request ():
168
168
conn .salt = pkt .read_all ()
169
+ if conn .salt .endswith (b"\0 " ):
170
+ conn .salt = conn .salt [:- 1 ]
169
171
if not conn .server_public_key and conn .password :
170
172
# Request server public key
171
173
if DEBUG :
@@ -215,9 +217,11 @@ def caching_sha2_password_auth(conn, pkt):
215
217
216
218
if pkt .is_auth_switch_request ():
217
219
# Try from fast auth
218
- if DEBUG :
219
- print ("caching sha2: Trying fast path" )
220
220
conn .salt = pkt .read_all ()
221
+ if conn .salt .endswith (b"\0 " ): # str.removesuffix is available in 3.9
222
+ conn .salt = conn .salt [:- 1 ]
223
+ if DEBUG :
224
+ print (f"caching sha2: Trying fast path. salt={ conn .salt .hex ()!r} " )
221
225
scrambled = scramble_caching_sha2 (conn .password , conn .salt )
222
226
pkt = _roundtrip (conn , scrambled )
223
227
# else: fast auth is tried in initial handshake
Original file line number Diff line number Diff line change 47
47
DEFAULT_USER = None
48
48
49
49
DEBUG = False
50
+ _DEFAULT_AUTH_PLUGIN = None # if this is not None, use it instead of server's default.
50
51
51
52
TEXT_TYPES = {
52
53
FIELD_TYPE .BIT ,
@@ -1158,6 +1159,9 @@ def _get_server_information(self):
1158
1159
else :
1159
1160
self ._auth_plugin_name = data [i :server_end ].decode ("utf-8" )
1160
1161
1162
+ if _DEFAULT_AUTH_PLUGIN is not None : # for tests
1163
+ self ._auth_plugin_name = _DEFAULT_AUTH_PLUGIN
1164
+
1161
1165
def get_server_info (self ):
1162
1166
return self .server_version
1163
1167
Original file line number Diff line number Diff line change @@ -71,6 +71,19 @@ def test_caching_sha2_password():
71
71
con .query ("FLUSH PRIVILEGES" )
72
72
con .close ()
73
73
74
+ # Fast path after auth_switch_request
75
+ pymysql .connections ._DEFAULT_AUTH_PLUGIN = "mysql_native_password"
76
+ con = pymysql .connect (
77
+ user = "user_caching_sha2" ,
78
+ password = pass_caching_sha2 ,
79
+ host = host ,
80
+ port = port ,
81
+ ssl = ssl ,
82
+ )
83
+ con .query ("FLUSH PRIVILEGES" )
84
+ con .close ()
85
+ pymysql .connections ._DEFAULT_AUTH_PLUGIN = None
86
+
74
87
75
88
def test_caching_sha2_password_ssl ():
76
89
con = pymysql .connect (
@@ -88,7 +101,20 @@ def test_caching_sha2_password_ssl():
88
101
password = pass_caching_sha2 ,
89
102
host = host ,
90
103
port = port ,
91
- ssl = None ,
104
+ ssl = ssl ,
105
+ )
106
+ con .query ("FLUSH PRIVILEGES" )
107
+ con .close ()
108
+
109
+ # Fast path after auth_switch_request
110
+ pymysql .connections ._DEFAULT_AUTH_PLUGIN = "mysql_native_password"
111
+ con = pymysql .connect (
112
+ user = "user_caching_sha2" ,
113
+ password = pass_caching_sha2 ,
114
+ host = host ,
115
+ port = port ,
116
+ ssl = ssl ,
92
117
)
93
118
con .query ("FLUSH PRIVILEGES" )
94
119
con .close ()
120
+ pymysql .connections ._DEFAULT_AUTH_PLUGIN = None
You can’t perform that action at this time.
0 commit comments