U
    
ڲgC                     @   sr   d Z ddlZddlZddlmZ dd ZG dd dZG dd	 d	ZG d
d dZG dd dZ	G dd dZ
dS )z Firebase user import sub module.    N)_auth_utilsc                 C   s   t |  S N)base64urlsafe_b64encodedecode)Zbytes_value r   ?/tmp/pip-unpacked-wheel-p0r7i5ii/firebase_admin/_user_import.py
b64_encode   s    r	   c                   @   s   e Zd ZdZdddZedd Zejdd Zedd	 Zejd
d	 Zedd Z	e	jdd Z	edd Z
e
jdd Z
edd Zejdd Zdd ZdS )UserProvideraD  Represents a user identity provider that can be associated with a Firebase user.

    One or more providers can be specified in an ``ImportUserRecord`` when importing users via
    ``auth.import_users()``.

    Args:
        uid: User's unique ID assigned by the identity provider.
        provider_id: ID of the identity provider. This can be a short domain name or the identifier
            of an OpenID identity provider.
        email: User's email address (optional).
        display_name: User's display name (optional).
        photo_url: User's photo URL (optional).
    Nc                 C   s"   || _ || _|| _|| _|| _d S r   )uidprovider_idemaildisplay_name	photo_url)selfr   r   r   r   r   r   r   r   __init__*   s
    zUserProvider.__init__c                 C   s   | j S r   _uidr   r   r   r   r   1   s    zUserProvider.uidc                 C   s   t j|dd| _d S NTrequiredr   Zvalidate_uidr   r   r   r   r   r   r   5   s    c                 C   s   | j S r   )_provider_idr   r   r   r   r   9   s    zUserProvider.provider_idc                 C   s   t j|dd| _d S r   )r   Zvalidate_provider_idr   )r   r   r   r   r   r   =   s    c                 C   s   | j S r   _emailr   r   r   r   r   A   s    zUserProvider.emailc                 C   s   t || _d S r   r   Zvalidate_emailr   r   r   r   r   r   r   E   s    c                 C   s   | j S r   _display_namer   r   r   r   r   I   s    zUserProvider.display_namec                 C   s   t || _d S r   r   Zvalidate_display_namer    r   r   r   r   r   r   M   s    c                 C   s   | j S r   
_photo_urlr   r   r   r   r   Q   s    zUserProvider.photo_urlc                 C   s   t || _d S r   r   Zvalidate_photo_urlr$   r   r   r   r   r   r   U   s    c                 C   s,   | j | j| j| j| jd}dd | D S )N)ZrawIdZ
providerIddisplayNamer   photoUrlc                 S   s   i | ]\}}|d k	r||qS r   r   .0kvr   r   r   
<dictcomp>a   s       z(UserProvider.to_dict.<locals>.<dictcomp>)r   r   r   r   r   itemsr   payloadr   r   r   to_dictY   s    zUserProvider.to_dict)NNN)__name__
__module____qualname____doc__r   propertyr   setterr   r   r   r   r1   r   r   r   r   r
      s.   










r
   c                   @   s&  e Zd ZdZd%ddZedd Zejdd Zedd	 Zejd
d	 Zedd Z	e	jdd Z	edd Z
e
jdd Z
edd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zed d! Zejd"d! Zd#d$ ZdS )&ImportUserRecordar  Represents a user account to be imported to Firebase Auth.

    Must specify the ``uid`` field at a minimum. A sequence of ``ImportUserRecord`` objects can be
    passed to the ``auth.import_users()`` function, in order to import those users into Firebase
    Auth in bulk. If the ``password_hash`` is set on a user, a hash configuration must be
    specified when calling ``import_users()``.

    Args:
        uid: User's unique ID. Must be a non-empty string not longer than 128 characters.
        email: User's email address (optional).
        email_verified: A boolean indicating whether the user's email has been verified (optional).
        display_name: User's display name (optional).
        phone_number: User's phone number (optional).
        photo_url: User's photo URL (optional).
        disabled: A boolean indicating whether this user account has been disabled (optional).
        user_metadata: An ``auth.UserMetadata`` instance with additional user metadata (optional).
        provider_data: A list of ``auth.UserProvider`` instances (optional).
        custom_claims: A ``dict`` of custom claims to be set on the user account (optional).
        password_hash: User's password hash as a ``bytes`` sequence (optional).
        password_salt: User's password salt as a ``bytes`` sequence (optional).

    Raises:
        ValueError: If provided arguments are invalid.
    Nc                 C   sL   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _d S r   )r   r   r   phone_numberr   password_hashpassword_saltemail_verifieddisableduser_metadataprovider_datacustom_claims)r   r   r   r<   r   r9   r   r=   r>   r?   r@   r:   r;   r   r   r   r   ~   s    zImportUserRecord.__init__c                 C   s   | j S r   r   r   r   r   r   r      s    zImportUserRecord.uidc                 C   s   t j|dd| _d S r   r   r   r   r   r   r      s    c                 C   s   | j S r   r   r   r   r   r   r      s    zImportUserRecord.emailc                 C   s   t || _d S r   r   r   r   r   r   r      s    c                 C   s   | j S r   r   r   r   r   r   r      s    zImportUserRecord.display_namec                 C   s   t || _d S r   r!   r"   r   r   r   r      s    c                 C   s   | j S r   )_phone_numberr   r   r   r   r9      s    zImportUserRecord.phone_numberc                 C   s   t || _d S r   )r   Zvalidate_phonerA   )r   r9   r   r   r   r9      s    c                 C   s   | j S r   r#   r   r   r   r   r      s    zImportUserRecord.photo_urlc                 C   s   t || _d S r   r%   r&   r   r   r   r      s    c                 C   s   | j S r   )_password_hashr   r   r   r   r:      s    zImportUserRecord.password_hashc                 C   s   t |d| _d S )Nr:   )r   validate_bytesrB   )r   r:   r   r   r   r:      s    c                 C   s   | j S r   )_password_saltr   r   r   r   r;      s    zImportUserRecord.password_saltc                 C   s   t |d| _d S )Nr;   )r   rC   rD   )r   r;   r   r   r   r;      s    c                 C   s   | j S r   )_user_metadatar   r   r   r   r>      s    zImportUserRecord.user_metadatac                 C   sJ   |d k	r|j nd }|d k	r |jnd }t|d| _t|d| _|| _d S )Ncreation_timestamplast_sign_in_timestamp)rF   rG   r   Zvalidate_timestamp_created_at_last_login_atrE   )r   r>   Z
created_atZlast_login_atr   r   r   r>      s     c                 C   s   | j S r   )_provider_datar   r   r   r   r?      s    zImportUserRecord.provider_datac                 C   sN   |d k	rDzt dd |D r$tdW n tk
rB   tdY nX || _d S )Nc                 S   s   g | ]}t |t qS r   )
isinstancer
   r*   pr   r   r   
<listcomp>   s     z2ImportUserRecord.provider_data.<locals>.<listcomp>z0One or more provider data instances are invalid.zprovider_data must be iterable.)any
ValueError	TypeErrorrJ   )r   r?   r   r   r   r?      s    c                 C   s   | j S r   )_custom_claimsr   r   r   r   r@      s    zImportUserRecord.custom_claimsc                 C   s.   t |trt|n|}t|| _|| _d S r   )rK   dictjsondumpsr   Zvalidate_custom_claims_custom_claims_strrR   )r   r@   Zjson_claimsr   r   r   r@      s     c                 C   s   | j | j| j| j| j| jdk	r(t| jnd| jdk	r>t| jnd| j| j	| j
| jr\t| jnd| jrnt| jndd}| jrdd | jD |d< dd | D S )zAReturns a dict representation of the user. For internal use only.N)ZlocalIdr   r'   ZphoneNumberr(   ZemailVerifiedr=   ZcustomAttributesZ	createdAtZlastLoginAtZpasswordHashZsaltc                 S   s   g | ]}|  qS r   )r1   rL   r   r   r   rN      s     z,ImportUserRecord.to_dict.<locals>.<listcomp>ZproviderUserInfoc                 S   s   i | ]\}}|d k	r||qS r   r   r)   r   r   r   r-      s       z,ImportUserRecord.to_dict.<locals>.<dictcomp>)r   r   r   r9   r   r<   boolr=   rV   rH   rI   r:   r	   r;   r?   r.   r/   r   r   r   r1      s$    zImportUserRecord.to_dict)NNNNNNNNNNN)r2   r3   r4   r5   r   r6   r   r7   r   r   r9   r   r:   r;   r>   r?   r@   r1   r   r   r   r   r8   d   sf                


















	

r8   c                   @   s   e Zd ZdZd#ddZdd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zed$ddZedd  Zed!d" ZdS )%UserImportHasha  Represents a hash algorithm used to hash user passwords.

    An instance of this class must be specified when importing users with passwords via the
    ``auth.import_users()`` API. Use one of the provided class methods to obtain new
    instances when required. Refer to `documentation`_ for more details.

    .. _documentation: https://firebase.google.com/docs/auth/admin/import-users
    Nc                 C   s   || _ || _d S r   )_name_data)r   namedatar   r   r   r     s    zUserImportHash.__init__c                 C   s    d| j i}| jr|| j |S )NZhashAlgorithm)rY   rZ   updater/   r   r   r   r1     s    
zUserImportHash.to_dictc                 C   s"   dt tj|dddi}t||S )N	signerKeykeyTr   )r	   r   rC   rX   )clsr[   r_   r\   r   r   r   _hmac  s     zUserImportHash._hmacc                 C   s   |  d|S )zCreates a new HMAC SHA512 algorithm instance.

        Args:
            key: Signer key as a byte sequence.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        ZHMAC_SHA512ra   r`   r_   r   r   r   hmac_sha512  s    
zUserImportHash.hmac_sha512c                 C   s   |  d|S )zCreates a new HMAC SHA256 algorithm instance.

        Args:
            key: Signer key as a byte sequence.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        ZHMAC_SHA256rb   rc   r   r   r   hmac_sha256)  s    
zUserImportHash.hmac_sha256c                 C   s   |  d|S )zCreates a new HMAC SHA1 algorithm instance.

        Args:
            key: Signer key as a byte sequence.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        Z	HMAC_SHA1rb   rc   r   r   r   	hmac_sha15  s    
zUserImportHash.hmac_sha1c                 C   s   |  d|S )zCreates a new HMAC MD5 algorithm instance.

        Args:
            key: Signer key as a byte sequence.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        ZHMAC_MD5rb   rc   r   r   r   hmac_md5A  s    
zUserImportHash.hmac_md5c              	   C   s   t ddt|dddiS )zCreates a new MD5 algorithm instance.

        Args:
            rounds: Number of rounds. Must be an integer between 0 and 8192.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        MD5roundsr       rX   r   validate_intr`   ri   r   r   r   md5M  s    
zUserImportHash.md5c              	   C   s   t ddt|dddiS )zCreates a new SHA1 algorithm instance.

        Args:
            rounds: Number of rounds. Must be an integer between 1 and 8192.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        SHA1ri      rj   rk   rm   r   r   r   sha1[  s    
zUserImportHash.sha1c              	   C   s   t ddt|dddiS )zCreates a new SHA256 algorithm instance.

        Args:
            rounds: Number of rounds. Must be an integer between 1 and 8192.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        SHA256ri   rp   rj   rk   rm   r   r   r   sha256i  s    
zUserImportHash.sha256c              	   C   s   t ddt|dddiS )zCreates a new SHA512 algorithm instance.

        Args:
            rounds: Number of rounds. Must be an integer between 1 and 8192.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        SHA512ri   rp   rj   rk   rm   r   r   r   sha512w  s    
zUserImportHash.sha512c              	   C   s   t ddt|dddiS )zCreates a new PBKDF SHA1 algorithm instance.

        Args:
            rounds: Number of rounds. Must be an integer between 0 and 120000.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        Z
PBKDF_SHA1ri   r    rk   rm   r   r   r   
pbkdf_sha1  s    
zUserImportHash.pbkdf_sha1c              	   C   s   t ddt|dddiS )zCreates a new PBKDF2 SHA256 algorithm instance.

        Args:
            rounds: Number of rounds. Must be an integer between 0 and 120000.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        ZPBKDF2_SHA256ri   r   rv   rk   rm   r   r   r   pbkdf2_sha256  s    
zUserImportHash.pbkdf2_sha256c                 C   sV   t tj|dddt|dddt|dddd	}|rLt t|d
|d< td|S )a$  Creates a new Scrypt algorithm instance.

        This is the modified Scrypt algorithm used by Firebase Auth. See ``standard_scrypt()``
        function for the standard Scrypt algorith,

        Args:
            key: Signer key as a byte sequence.
            rounds: Number of rounds. Must be an integer between 1 and 8.
            memory_cost: Memory cost as an integer between 1 and 14.
            salt_separator: Salt separator as a byte sequence (optional).

        Returns:
            UserImportHash: A new ``UserImportHash``.
        r_   Tr   ri   rp      memory_cost   )r^   ri   Z
memoryCostsalt_separatorZsaltSeparatorZSCRYPT)r	   r   rC   rl   rX   )r`   r_   ri   rz   r|   r\   r   r   r   scrypt  s     
zUserImportHash.scryptc                 C   s   t dS )zyCreates a new Bcrypt algorithm instance.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        ZBCRYPT)rX   )r`   r   r   r   bcrypt  s    zUserImportHash.bcryptc                 C   sH   t j|dddt j|dddt j|dddt j|dddd}td|S )	a  Creates a new standard Scrypt algorithm instance.

        Args:
            memory_cost: CPU Memory cost as a non-negative integer.
            parallelization: Parallelization as a non-negative integer.
            block_size: Block size as a non-negative integer.
            derived_key_length: Derived key length as a non-negative integer.

        Returns:
            UserImportHash: A new ``UserImportHash``.
        rz   r   )lowparallelization
block_sizederived_key_length)Z
cpuMemCostr   Z	blockSizeZdkLenZSTANDARD_SCRYPT)r   rl   rX   )r`   rz   r   r   r   r\   r   r   r   standard_scrypt  s    zUserImportHash.standard_scrypt)N)N)r2   r3   r4   r5   r   r1   classmethodra   rd   re   rf   rg   rn   rq   rs   ru   rw   rx   r}   r~   r   r   r   r   r   rX     s>   	












rX   c                   @   s0   e Zd ZdZdd Zedd Zedd ZdS )		ErrorInfozRepresents an error encountered while performing a batch operation such
    as importing users or deleting multiple user accounts.
    c                 C   s   |d | _ |d | _d S )Nindexmessage)_index_reason)r   errorr   r   r   r     s    
zErrorInfo.__init__c                 C   s   | j S r   )r   r   r   r   r   r     s    zErrorInfo.indexc                 C   s   | j S r   )r   r   r   r   r   reason  s    zErrorInfo.reasonN)r2   r3   r4   r5   r   r6   r   r   r   r   r   r   r     s   
r   c                   @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Zd
S )UserImportResultzrRepresents the result of a bulk user import operation.

    See ``auth.import_users()`` API for more details.
    c                 C   s8   | dg }|t| | _t|| _dd |D | _d S )Nr   c                 S   s   g | ]}t |qS r   )r   )r*   errr   r   r   rN     s     z-UserImportResult.__init__.<locals>.<listcomp>)getlen_success_count_failure_count_errors)r   resulttotalerrorsr   r   r   r     s    
zUserImportResult.__init__c                 C   s   | j S )z2Returns the number of users successfully imported.)r   r   r   r   r   success_count  s    zUserImportResult.success_countc                 C   s   | j S )z7Returns the number of users that failed to be imported.)r   r   r   r   r   failure_count   s    zUserImportResult.failure_countc                 C   s   | j S )zQReturns a list of ``auth.ErrorInfo`` instances describing the errors encountered.)r   r   r   r   r   r     s    zUserImportResult.errorsN)	r2   r3   r4   r5   r   r6   r   r   r   r   r   r   r   r     s   

r   )r5   r   rT   Zfirebase_adminr   r	   r
   r8   rX   r   r   r   r   r   r   <module>   s   I  Y