
    kj	
                    L    d Z ddlmZ ddlZddlZddlmZ ddlZ G d d      Zy)z}
A standalone, minimal htpasswd parser.

This implementation currently supports bcrypt and SHA1 passwords. SHA1 is insecure.
    )annotationsN)Pathc                  .    e Zd ZddZedd       ZddZy)HtpasswdFilec                x   i | _         |j                         D ]  }|j                         }|r|j                  d      r'd|vrt	        d|      |j                  dd      \  }}|st	        d|      |j                  d      }|j                  d      }|s|st	        d|      || j                   |<    y)	z6
        Create a HtpasswdFile from a string.
        #:zMalformed htpasswd line:    {SHA})z$2y$z$2b$z$2a$z%Unsupported htpasswd format for user N)users
splitlinesstrip
startswith
ValueErrorsplit)selfcontentlineuserpwhashis_sha	is_bcrypts          a/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/utils/htpasswd.py__init__zHtpasswdFile.__init__   s     &(
&&( 	&D::<D4??3/$ #<TH!EFF::c1-LD& #<TH!EFF&&w/F))*BCI) #H!QRR%DJJt	&    c                n    	 |j                  d      } | |      S # t        $ r t        d|       dw xY w)z
        Initializes and loads an htpasswd file.

        Args:
            path: The path to the htpasswd file.

        Raises:
            OSError: If the file cannot be read.
            ValueError: If the file is malformed.
        utf-8zHtpasswd file not found: N)	read_textFileNotFoundErrorOSError)clspathr   s      r   	from_filezHtpasswdFile.from_file'   sJ    	HnnW-G 7| ! 	H5dV<=4G	Hs    4c                   | j                   j                  |      }|y|j                  dd      d   }|j                  d      r^t	        j
                  |j                  d            j                         }t        j                  |      j                  d      }|d	d |k(  S t        j                  |j                  d      |j                  d            S )
a   
        Checks if a username and password combination is valid.

        Args:
            username: The username to check.
            password: The password to check.

        Returns:
            True if the password is valid, False otherwise.
        NFr	   r
   r   r   r   ascii   )r   getr   r   hashlibsha1encodedigestbase64	b64encodedecodebcryptcheckpw)r   usernamepasswordr   r+   expecteds         r   check_passwordzHtpasswdFile.check_password9   s     )>c1%a(W% \\(//'":;BBDF''/66w?H!":))>>(//'":FMM'<RSSr   N)r   str)r"   r   returnr   )r1   r5   r2   r5   r6   bool)__name__
__module____qualname__r   classmethodr#   r4    r   r   r   r      s!    &,  "Tr   r   )	__doc__
__future__r   r,   r(   pathlibr   r/   r   r<   r   r   <module>r@      s)    #    AT ATr   