
    3iP                         d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ d
dl	mZ dZdZ G d de      Zy)zOpenID Authentication Plugin.    N)Path)AnyList)utils   )errors)logger)MySQLSocket   )MySQLAuthPluginMySQLOpenIDConnectAuthPlugini (  c                       e Zd ZU dZ ej
                  d      Zeed<   e	de
fd       Ze	defd       Zede
defd       Zd	ed
edefdZded	ed
edefdZy)r   zBClass implementing the MySQL OpenID Connect Authentication Plugin.r   _openid_capability_flagreturnc                      y)zPlugin official name.$authentication_openid_connect_client selfs    aD:\jyotish\venv\Lib\site-packages\mysql/connector/plugins/authentication_openid_connect_client.pynamez!MySQLOpenIDConnectAuthPlugin.name4   s     6    c                      y)z'Signals whether or not SSL is required.Tr   r   s    r   requires_sslz)MySQLOpenIDConnectAuthPlugin.requires_ssl9   s     r   tokenc                     | j                  d      }t        |      dk7  ryt        j                  d      t	        fd|D              S )a?  Helper method used to validate OpenID Connect token

        The Token is represented as a JSON Web Token (JWT) consists of a
        base64-encoded header, body, and signature, separated by '.' e.g.,
        "Base64url.Base64url.Base64url". The First part of the token contains
        the header, the second part contains payload and the third part contains
        signature. These token parts should be Base64 URLSafe i.e., Token cannot
        contain characters other than a-z, A-Z, 0-9 and special characters '-', '_'.

        Args:
            token (str): Base64url-encoded OpenID connect token fetched from
                         the file path passed via `openid_token_file` connection
                         argument.

        Returns:
            bool: Signal indicating whether the token is valid or not.
        .   Fz^[a-zA-Z0-9-_]*$c              3   ^   K   | ]$  }t        |      xr j                  |      d u & y w)N)lensearch).0
token_parturlsafe_patterns     r   	<genexpr>zFMySQLOpenIDConnectAuthPlugin._validate_openid_token.<locals>.<genexpr>W   s5      "4J JRO$:$::$Fd$RR"4s   *-)splitr    recompileall)r   header_payload_sigr$   s     @r   _validate_openid_tokenz3MySQLOpenIDConnectAuthPlugin._validate_openid_token>   sN    & ).C(8!"a'**%78"4
 	
r   	auth_datakwargsc                    	 | j                   r.| j                  s"t        j                  | j                   d      |j                  dd      }t        |      }|j                         j                  t        kD  rt        j                  d      |j                  d      }|j                         }| j                  |      st        j                  d      | j                  t        j                  t!        |            |j#                         g}dj%                  |      S # t&        t(        t*        t,        f$ r}t        j.                  d	      |d}~ww xY w)
a  Prepares authentication string for the server.
        Args:
            auth_data: Authorization data.
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked.

        Returns:
            packet: Client's authorization response.
            The OpenID Connect authorization response follows the pattern :-
            int<1>           capability flag
            string<lenenc>   id token

        Raises:
            InterfaceError: If the connection is insecure or the OpenID Token is too large,
                            invalid or non-existent.
            ProgrammingError: If the OpenID Token file could not be read.
        z requires SSLopenid_token_fileNz8The OpenID Connect token file size is too large (> 10KB)zutf-8)encodingz#The OpenID Connect Token is invalidr   zCThe OpenID Connect Token File (openid_token_file) could not be read)r   _ssl_enabledr   InterfaceErrorr   getr   statst_sizeOPENID_TOKEN_MAX_SIZE	read_textstripr+   r   r   lc_intr    encodejoinSyntaxError	TypeErrorOSErrorUnicodeErrorProgrammingError)r   r,   r-   token_file_pathr/   openid_tokenauth_responseerrs           r   rC   z*MySQLOpenIDConnectAuthPlugin.auth_response]   s5   $	  ):):++tyyk,GHH $*::.A4#HO&*?&; %%'//2GG++N  !2 ; ;W ; ML'--/L..|<++,QRR ,,S./##%*M
 88M**Y> 	))U	s   DD! !E:EEsockc                     | j                   |fi |}|t        j                  d      t        j                  d|t        |             |j                  |       |j                         }t        j                  d|       t        |      S )a  Handles server's `auth switch request` response.

        Args:
            sock: Pointer to the socket connection.
            auth_data: Plugin provided data (extracted from a packet
                       representing an `auth switch request` response).
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked. The parameters defined here will override the ones
                    defined in the auth plugin itself.

        Returns:
            packet: Last server's response after back-and-forth
                    communication.

        Raises:
            InterfaceError: If a NULL auth response is received from auth_response method.
        zGot a NULL auth responsez# request: %s size: %sz# server response packet: %s)	rC   r   r2   r	   debugr    sendrecvbytes)r   rE   r,   r-   responsepackets         r   auth_switch_responsez1MySQLOpenIDConnectAuthPlugin.auth_switch_response   sx    ( &4%%i:6:''(BCC-xXG		(3V<V}r   N)__name__
__module____qualname____doc__r   	int1storer   rJ   __annotations__propertystrr   boolr   staticmethodr+   r   rC   r
   rM   r   r   r   r   r   /   s    L%4U__Q%7U76c 6 6 d   
c 
d 
 
</u / / /b,1=@	r   )rQ   r'   pathlibr   typingr   r   mysql.connectorr    r   r	   networkr
   r   AUTHENTICATION_PLUGIN_CLASSr6   r   r   r   r   <module>r^      s<   : $ 	   !   ! < ! ~? ~r   