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

Method _request_authentication

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

Source from the content-addressed store, hash-verified

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