
    ?h@                         d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZ d dlmZmZ d dl  G d d	e      Zy)
    )print_functionunicode_literalsabsolute_importdivisionN)ensure_textensure_binaryvalidate_channelvalidate_socket_idvalidate_user_datachannel_name_re)Client)GETPOSTRequestrequest_method)signverify)*c                   J     e Zd Z	 	 	 	 	 	 	 	 	 	 d fd	ZddZddZd Z xZS )AuthenticationClientc                 F    t        t        | 
  |||||||||	|
|||fi | y N)superr   __init__)selfapp_idkeysecretsslhostporttimeoutclusterencryption_master_keyencryption_master_key_base64json_encoderjson_decoderbackendbackend_options	__class__s                  ]/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/pusher/authentication_client.pyr   zAuthenticationClient.__init__"   sF    " 	"D2!(	 	    c                    t        |      }t        j                  |      st        d|z        t	        |      }|r!t        j                  || j                        }|d|}|r|d|z  z  }t        | j                  |      }| j                  d|}d|i}t        |      r:t        t        |d      | j                        }t        j                   |      }	|	|d<   |r||d<   |S )	a  Used to generate delegated client subscription token.

        :param channel: name of the channel to authorize subscription to
        :param socket_id: id of the socket that requires authorization
        :param custom_data: used on presence channels to provide user info
        z*Channel should be a valid channel, got: %scls:z:%sauthchannelshared_secretchannel_data)r	   r   match
ValueErrorr
   jsondumps_json_encoderr   r   r   is_encrypted_channelgenerate_shared_secretr   _encryption_master_keybase64	b64encode)
r   r2   	socket_idcustom_datastring_to_sign	signaturer1   response_payloadr3   shared_secret_b64s
             r+   authenticatez!AuthenticationClient.authenticateC   s     #7+$$W-IGSTT&y1	**[d6H6HIK$-w7ek11Nn5	((I."D>(2gy143N3NPM & 0 0 ?0A_-/:^,r,   c                     t        |       t        |      }t        j                  || j                        }|d|}t        | j                  |      }| j                  d|}||d}|S )zCreates a user authentication signature.

        :param socket_id: id of the socket that requires authorization
        :param user_data: used to provide user info
        r.   z::user::r0   )r1   	user_data)r   r
   r7   r8   r9   r   r   r   )r   r?   rG   user_data_encodedrA   rB   auth_responserC   s           r+   authenticate_userz&AuthenticationClient.authenticate_useri   sh     	9%&y1	 JJyd6H6HI+46GHn5	#'88Y7$1@QRr,   c                 v   t        |d      }t        |d      }t        |d      }|| j                  k7  ryt        | j                  ||      sy	 t	        j
                  || j                        }|j                  d      }|syt        t        j                         dz  |z
        dkD  ry|S # t        $ r Y yw xY w)	a<  Used to validate incoming webhook messages. When used it guarantees
        that the sender is Pusher and not someone else impersonating it.

        :param key: key used to sign the body
        :param signature: signature that was given with the body
        :param body: content that needs to be verified
        r   rB   bodyNr.   time_msi  i )r   r   r   r   r7   loads_json_decoderr6   getabstime)r   r   rB   rL   	body_datarM   s         r+   validate_webhookz%AuthenticationClient.validate_webhook}   s     #u%	;7	4($((?dkk43	

4T-?-?@I
 --	*tyy{T!G+,v5  		s   !B, ,	B87B8)
TNN   NNNNNNr   )__name__
__module____qualname__r   rE   rJ   rT   __classcell__)r*   s   @r+   r   r   !   s9     "%)B$ L (r,   r   ) 
__future__r   r   r   r   collectionshashlibr7   osresixrR   r=   pusher.utilr   r   r	   r
   r   r   pusher.clientr   pusher.httpr   r   r   r   pusher.signaturer   r   pusher.cryptor    r,   r+   <module>rf      sR        	 	 
    ! : : ) {6 {r,   