U
    42‹i\
  ã                   @   s    d dl mZ G dd„ deƒZdS )é   )ÚApiKeyIncludedExceptionc                   @   s.   e Zd ZdZdZd
dd„Zdd„ Zdd	„ ZdS )ÚValidateApiKeyz;Validates content to ensure SendGrid API key is not presentNTc                 C   sR   ddl }tƒ | _|dk	r4|D ]}| j | |¡¡ q|rNd}| j | |¡¡ dS )zôCreate an API key validator

            :param regex_strings: list of regex strings
            :type regex_strings: list(str)
            :param use_default: Whether or not to include default regex
            :type use_default: bool
        é    NzSG\.[0-9a-zA-Z]+\.[0-9a-zA-Z]+)ÚreÚsetÚregexesÚaddÚcompile)ÚselfZregex_stringsZuse_defaultr   Zregex_stringZdefault_regex_string© r   úD/tmp/pip-unpacked-wheel-clku7wca/sendgrid/helpers/mail/validators.pyÚ__init__	   s    	zValidateApiKey.__init__c                 C   sx   t |tƒr|  |¡ n^t |tƒrt| dtƒ ¡}|D ]@}|dk	r2| d¡dks\t | d¡tƒr2| dd¡}|  |¡ q2dS )aÂ  With the JSON dict that will be sent to SendGrid's API,
            check the content for SendGrid API keys - throw exception if found.

           :param request_body: The JSON dict that will be sent to SendGrid's
                                API.
           :type request_body: JSON serializable structure
           :raise ApiKeyIncludedException: If any content in request_body
                                           matches regex
        ÚcontentNÚtypez	text/htmlÚvalueÚ )Ú
isinstanceÚstrÚvalidate_message_textÚdictÚgetÚlist)r
   Úrequest_bodyÚcontentsr   Zmessage_textr   r   r   Úvalidate_message_dict   s    

ÿz$ValidateApiKey.validate_message_dictc                 C   s.   t |tƒr*| jD ]}| |¡dk	rtƒ ‚qdS )af  With a message string, check to see if it contains a SendGrid API Key
            If a key is found, throw an exception

           :param message_string: message that will be sent
           :type message_string: string
           :raises ApiKeyIncludedException: If message_string matches a regex
                                            string
        N)r   r   r   Úmatchr   )r
   Zmessage_stringÚregexr   r   r   r   9   s    	

z$ValidateApiKey.validate_message_text)NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   
r   N)Ú
exceptionsr   Úobjectr   r   r   r   r   Ú<module>   s   