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

Method _request_authentication

pymysql/connections.py:922–1087  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

920 break
921
922 def _request_authentication(self):
923 # https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
924 if int(self.server_version.split(".", 1)[0]) >= 5:
925 self.client_flag |= CLIENT.MULTI_RESULTS
926
927 if self.user is None:
928 raise ValueError("Did not specify a username")
929
930 charset_id = charset_by_name(self.charset).id
931 if isinstance(self.user, str):
932 self.user = self.user.encode(self.encoding)
933
934 # Determine flags for the initial handshake packet.
935 # CLIENT.SSL is added conditionally: for REQUIRED mode it is already set in
936 # self.client_flag, but for PREFERRED mode it is only added when the server
937 # also advertises SSL support.
938 # _do_ssl is set here and checked below for sha256_password auth.
939 client_flags = self.client_flag
940 if self.ssl:
941 if self.server_capabilities & CLIENT.SSL:
942 # SSL upgrade: include CLIENT.SSL flag and wrap the socket.
943 _do_ssl = True
944 client_flags |= CLIENT.SSL
945 elif self._ssl_required:
946 raise err.OperationalError(
947 CR.CR_SSL_CONNECTION_ERROR,
948 "SSL is required but the server doesn't support it",
949 )
950 else:
951 # PREFERRED mode: server doesn't support SSL, fall back to non-SSL.
952 _do_ssl = False
953 else:
954 _do_ssl = False
955
956 data_init = struct.pack(
957 "<iIB23s", client_flags, MAX_PACKET_LEN, charset_id, b""
958 )
959
960 if _do_ssl:
961 self.write_packet(data_init)
962 self._sock = self.ctx.wrap_socket(self._sock, server_hostname=self.host)
963 self._rfile = self._sock.makefile("rb")
964 self._secure = True
965
966 data = data_init + self.user + b"\0"
967
968 authresp = b""
969 plugin_name = None
970
971 if self._auth_plugin_name == "":
972 plugin_name = b""
973 authresp = _auth.scramble_native_password(self.password, self.salt)
974 elif self._auth_plugin_name == "mysql_native_password":
975 plugin_name = b"mysql_native_password"
976 authresp = _auth.scramble_native_password(self.password, self.salt)
977 elif self._auth_plugin_name == "caching_sha2_password":
978 plugin_name = b"caching_sha2_password"
979 if self.password:

Callers 1

connectMethod · 0.95

Calls 11

write_packetMethod · 0.95
_read_packetMethod · 0.95
_process_authMethod · 0.95
_lenenc_intFunction · 0.85
read_uint8Method · 0.80
read_stringMethod · 0.80
is_extra_auth_dataMethod · 0.80
is_ok_packetMethod · 0.80
authenticateMethod · 0.45

Tested by

no test coverage detected