B
    ufC                 @   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   ]/home/ankuromar296_gmail_com/myenv/lib/python3.7/site-packages/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 )N)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 )N)_uid)r   r   r   r   r
   1   s    zUserProvider.uidc             C   s   t j|dd| _d S )NT)required)r   validate_uidr   )r   r
   r   r   r   r
   5   s    c             C   s   | j S )N)_provider_id)r   r   r   r   r   9   s    zUserProvider.provider_idc             C   s   t j|dd| _d S )NT)r   )r   Zvalidate_provider_idr   )r   r   r   r   r   r   =   s    c             C   s   | j S )N)_email)r   r   r   r   r   A   s    zUserProvider.emailc             C   s   t || _d S )N)r   validate_emailr   )r   r   r   r   r   r   E   s    c             C   s   | j S )N)_display_name)r   r   r   r   r   I   s    zUserProvider.display_namec             C   s   t || _d S )N)r   validate_display_namer   )r   r   r   r   r   r   M   s    c             C   s   | j S )N)
_photo_url)r   r   r   r   r   Q   s    zUserProvider.photo_urlc             C   s   t || _d S )N)r   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 )Nr   ).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   r#   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 )N)r
   r   r   phone_numberr   password_hashpassword_saltemail_verifieddisableduser_metadataprovider_datacustom_claims)r   r
   r   r.   r   r+   r   r/   r0   r1   r2   r,   r-   r   r   r   r   ~   s    zImportUserRecord.__init__c             C   s   | j S )N)r   )r   r   r   r   r
      s    zImportUserRecord.uidc             C   s   t j|dd| _d S )NT)r   )r   r   r   )r   r
   r   r   r   r
      s    c             C   s   | j S )N)r   )r   r   r   r   r      s    zImportUserRecord.emailc             C   s   t || _d S )N)r   r   r   )r   r   r   r   r   r      s    c             C   s   | j S )N)r   )r   r   r   r   r      s    zImportUserRecord.display_namec             C   s   t || _d S )N)r   r   r   )r   r   r   r   r   r      s    c             C   s   | j S )N)_phone_number)r   r   r   r   r+      s    zImportUserRecord.phone_numberc             C   s   t || _d S )N)r   Zvalidate_phoner3   )r   r+   r   r   r   r+      s    c             C   s   | j S )N)r   )r   r   r   r   r      s    zImportUserRecord.photo_urlc             C   s   t || _d S )N)r   r   r   )r   r   r   r   r   r      s    c             C   s   | j S )N)_password_hash)r   r   r   r   r,      s    zImportUserRecord.password_hashc             C   s   t |d| _d S )Nr,   )r   validate_bytesr4   )r   r,   r   r   r   r,      s    c             C   s   | j S )N)_password_salt)r   r   r   r   r-      s    zImportUserRecord.password_saltc             C   s   t |d| _d S )Nr-   )r   r5   r6   )r   r-   r   r   r   r-      s    c             C   s   | j S )N)_user_metadata)r   r   r   r   r0      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)r8   r9   r   Zvalidate_timestamp_created_at_last_login_atr7   )r   r0   Z
created_atZlast_login_atr   r   r   r0      s    
c             C   s   | j S )N)_provider_data)r   r   r   r   r1      s    zImportUserRecord.provider_datac             C   sN   |d k	rDy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	TypeErrorr<   )r   r1   r   r   r   r1      s    c             C   s   | j S )N)_custom_claims)r   r   r   r   r2      s    zImportUserRecord.custom_claimsc             C   s.   t |trt|n|}t|| _|| _d S )N)r=   dictjsondumpsr   Zvalidate_custom_claims_custom_claims_strrC   )r   r2   Zjson_claimsr   r   r   r2      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passwordHashsaltc             S   s   g | ]}|  qS r   )r#   )r   r>   r   r   r   r?      s    z,ImportUserRecord.to_dict.<locals>.<listcomp>ZproviderUserInfoc             S   s   i | ]\}}|d k	r||qS )Nr   )r   r   r   r   r   r   r       s    z,ImportUserRecord.to_dict.<locals>.<dictcomp>)r
   r   r   r+   r   r.   boolr/   rG   r:   r;   r,   r   r-   r1   r!   )r   r"   r   r   r   r#      s    zImportUserRecord.to_dict)NNNNNNNNNNN)r$   r%   r&   r'   r   r(   r
   r)   r   r   r+   r   r,   r-   r0   r1   r2   r#   r   r   r   r   r*   d   s2     
	
r*   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 )N)_name_data)r   namedatar   r   r   r     s    zUserImportHash.__init__c             C   s    d| j i}| jr|| j |S )NZhashAlgorithm)rK   rL   update)r   r"   r   r   r   r#     s    
zUserImportHash.to_dictc             C   s"   dt tj|dddi}t||S )N	signerKeykeyT)r   )r   r   r5   rJ   )clsrM   rQ   rN   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)rS   )rR   rQ   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_SHA256)rS   )rR   rQ   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_SHA1)rS   )rR   rQ   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_MD5)rS   )rR   rQ   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   i    )rJ   r   validate_int)rR   rY   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``.
        SHA1rY      i    )rJ   r   rZ   )rR   rY   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``.
        SHA256rY   r]   i    )rJ   r   rZ   )rR   rY   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``.
        SHA512rY   r]   i    )rJ   r   rZ   )rR   rY   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_SHA1rY   r   i )rJ   r   rZ   )rR   rY   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_SHA256rY   r   i )rJ   r   rZ   )rR   rY   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``.
        rQ   T)r   rY   r]      memory_cost   )rP   rY   Z
memoryCostsalt_separatorZsaltSeparatorZSCRYPT)r   r   r5   rZ   rJ   )rR   rQ   rY   rf   rh   rN   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)rJ   )rR   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``.
        rf   r   )lowparallelization
block_sizederived_key_length)Z
cpuMemCostrl   Z	blockSizeZdkLenZSTANDARD_SCRYPT)r   rZ   rJ   )rR   rf   rl   rm   rn   rN   r   r   r   standard_scrypt  s
    zUserImportHash.standard_scrypt)N)N)r$   r%   r&   r'   r   r#   classmethodrS   rT   rU   rV   rW   r[   r^   r`   rb   rc   rd   ri   rj   ro   r   r   r   r   rJ     s$   
	rJ   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 )N)rt   )r   r   r   r   rr     s    zErrorInfo.indexc             C   s   | j S )N)ru   )r   r   r   r   reason  s    zErrorInfo.reasonN)r$   r%   r&   r'   r   r(   rr   rw   r   r   r   r   rq     s   rq   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 )Nrv   c             S   s   g | ]}t |qS r   )rq   )r   errr   r   r   r?     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)	r$   r%   r&   r'   r   r(   r   r   r   r   r   r   r   rx     s
   rx   )r'   r   rE   Zfirebase_adminr   r   r	   r*   rJ   rq   rx   r   r   r   r   <module>   s   I  Y