MCPcopy Create free account
hub / github.com/PyMySQL/PyMySQL / caching_sha2_password_auth

Function caching_sha2_password_auth

pymysql/_auth.py:211–270  ·  view source on GitHub ↗
(conn, pkt)

Source from the content-addressed store, hash-verified

209
210
211def caching_sha2_password_auth(conn, pkt):
212 # No password fast path
213 if not conn.password:
214 return _roundtrip(conn, b"")
215
216 if pkt.is_auth_switch_request():
217 # Try from fast auth
218 conn.salt = pkt.read_all()
219 if conn.salt.endswith(b"\0"): # str.removesuffix is available in 3.9
220 conn.salt = conn.salt[:-1]
221 if DEBUG:
222 print(f"caching sha2: Trying fast path. salt={conn.salt.hex()!r}")
223 scrambled = scramble_caching_sha2(conn.password, conn.salt)
224 pkt = _roundtrip(conn, scrambled)
225 # else: fast auth is tried in initial handshake
226
227 if not pkt.is_extra_auth_data():
228 raise OperationalError(
229 "caching sha2: Unknown packet for fast auth: %s" % pkt._data[:1]
230 )
231
232 # magic numbers:
233 # 2 - request public key
234 # 3 - fast auth succeeded
235 # 4 - need full auth
236
237 pkt.advance(1)
238 n = pkt.read_uint8()
239
240 if n == 3:
241 if DEBUG:
242 print("caching sha2: succeeded by fast path.")
243 pkt = conn._read_packet()
244 pkt.check_error() # pkt must be OK packet
245 return pkt
246
247 if n != 4:
248 raise OperationalError("caching sha2: Unknown result for fast auth: %s" % n)
249
250 if DEBUG:
251 print("caching sha2: Trying full auth...")
252
253 if conn._secure:
254 if DEBUG:
255 print("caching sha2: Sending plain password via secure connection")
256 return _roundtrip(conn, conn.password + b"\0")
257
258 if not conn.server_public_key:
259 pkt = _roundtrip(conn, b"\x02") # Request public key
260 if not pkt.is_extra_auth_data():
261 raise OperationalError(
262 "caching sha2: Unknown packet for public key: %s" % pkt._data[:1]
263 )
264
265 conn.server_public_key = pkt._data[1:]
266 if DEBUG:
267 print(conn.server_public_key.decode("ascii"))
268

Callers

nothing calls this directly

Calls 11

_roundtripFunction · 0.85
scramble_caching_sha2Function · 0.85
OperationalErrorClass · 0.85
sha2_rsa_encryptFunction · 0.85
read_allMethod · 0.80
is_extra_auth_dataMethod · 0.80
advanceMethod · 0.80
read_uint8Method · 0.80
_read_packetMethod · 0.80
check_errorMethod · 0.80

Tested by

no test coverage detected