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

Method _request_authentication

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

Source from the content-addressed store, hash-verified

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