U eO@sdZddlmZddlZddlmZddlmZmZGddde Z Gd d d e Z Gd d d e Z Gd dde Z ddZdS)z5Implementing support for MySQL Authentication Plugins)sha1N)errors)PY2isstrc@s2eZdZdZdZdZd ddZddZd d ZdS) BaseAuthPluginaBase class for authentication plugins Classes inheriting from BaseAuthPlugin should implement the method prepare_password(). When instantiating, auth_data argument is required. The username, password and database are optional. The ssl_enabled argument can be used to tell the plugin whether SSL is active or not. The method auth_response() method is used to retrieve the password which was prepared by prepare_password(). FNcCs"||_||_||_||_||_dS)ZInitializationN) _auth_dataZ _username _passwordZ _database _ssl_enabled)self auth_datausernamepasswordZdatabaseZ ssl_enabledrs/opt/hc_python/lib64/python3.8/site-packages/../../../lib/python3.8/site-packages/mysql/connector/authentication.py__init__2s zBaseAuthPlugin.__init__cCstdS)zPrepares and returns password to be send to MySQL This method needs to be implemented by classes inheriting from this class. It is used by the auth_response() method. Raises NotImplementedError. N)NotImplementedErrorr rrrprepare_password;szBaseAuthPlugin.prepare_passwordcCs(|jr |js tdj|jd|S)zReturns the prepared password to send to MySQL Raises InterfaceError on errors. For example, when SSL is required by not enabled. Returns str z{name} requires SSL)name) requires_sslr rInterfaceErrorformat plugin_namerrrrr auth_responseEs  zBaseAuthPlugin.auth_response)NNNF) __name__ __module__ __qualname____doc__rrrrrrrrrr!s   rc@s eZdZdZdZdZddZdS)MySQLNativePasswordAuthPluginzBClass implementing the MySQL Native Password authentication pluginFZmysql_native_passwordc Cs*|jstd|jsdS|j}t|jr8|jd}n|j}trzt|}zt|j}Wqtk rvtdYqXn |}|j}d}zht | }t | }t || }trddt ||D}nddt ||D}t j d |}Wn4tk r$}ztd |W5d}~XYnX|S) z;Prepares and returns password as native MySQL 4.1+ passwordz"Missing authentication data (seed)zutf-8zAuthentication data incorrectNcSs g|]\}}t|t|AqSr)ord.0Zh1Zh3rrr wszBMySQLNativePasswordAuthPlugin.prepare_password..cSsg|]\}}||AqSrrr#rrrr%ys20BzFailed scrambling password; {0})r&)r rrr rencoderbuffer TypeErrorrdigestzipstructpack Exceptionr) r rr Zhash4Zhash1Zhash2Zhash3ZxoredexcrrrrYs<    z.MySQLNativePasswordAuthPlugin.prepare_passwordNrrrrrrrrrrrr Ssr c@s eZdZdZdZdZddZdS)MySQLClearPasswordAuthPluginzAClass implementing the MySQL Clear Password authentication pluginTZmysql_clear_passwordcCsF|js dS|j}tr*t|tr>|d}nt|tr>|d}|dSz!Returns password as as clear textutf8r r isinstanceunicoder'strr rrrrrs    z-MySQLClearPasswordAuthPlugin.prepare_passwordNr0rrrrr1sr1c@s eZdZdZdZdZddZdS)MySQLSHA256PasswordAuthPluginzClass implementing the MySQL SHA256 authentication plugin Note that encrypting using RSA is not supported since the Python Standard Library does not provide this OpenSSL functionality. TZsha256_passwordcCsF|js dS|j}tr*t|tr>|d}nt|tr>|d}|dSr2r5r9rrrrs    z.MySQLSHA256PasswordAuthPlugin.prepare_passwordNr0rrrrr:sr:cCs4tD]}|j|kr|Sqtd|dS)a.Return authentication class based on plugin name This function returns the class for the authentication plugin plugin_name. The returned class is a subclass of BaseAuthPlugin. Raises errors.NotSupportedError when plugin_name is not supported. Returns subclass of BaseAuthPlugin. z,Authentication plugin '{0}' is not supportedN)r__subclasses__rrZNotSupportedErrorr)rZ authclassrrrget_auth_plugins   r<)rhashlibrr,rrZcatch23rrobjectrr r1r:r<rrrrs  2/