
    bf                         d dl Z d dlZd dlZddlmZmZ ddl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mZ  G d	 d
e          ZdS )    N   )_DICT_VERSION_PUSH_DICT_VERSION_PUSH_NOTIF)MastodonIllegalArgumentError)api_version)IMPL_HAS_CRYPTOecserializationdefault_backend)IMPL_HAS_ECEhttp_ece)Mastodonc                      e Zd Z edde          d             Z edde          	 	 	 	 d
d            Z edde          	 	 	 	 dd            Z eddd          d             Zd Z	 edde
          d	             ZdS )r   z2.4.0c                 .    |                      dd          S )z
        Fetch the current push subscription the logged-in user has for this app.

        Returns a :ref:`push subscription dict <push subscription dict>`.
        GET/api/v1/push/subscription_Mastodon__api_requestselfs    [/home/feoh/.local/pipx/venvs/mastodon-archive/lib/python3.11/site-packages/mastodon/push.pypush_subscriptionzMastodon.push_subscription   s     !!%)DEEE    Nallc                    |
dvrt          d          t                              |          }t          j        |d                   }t          j        |d                   }||||
d}|||d<   |||d<   |||d	<   |||d
<   |||d<   |||d<   ||	|d<   |                     |          }|                     dd|          S )aX  
        Sets up or modifies the push subscription the logged-in user has for this app.

        `endpoint` is the endpoint URL mastodon should call for pushes. Note that mastodon
        requires https for this URL. `encrypt_params` is a dict with key parameters that allow
        the server to encrypt data for you: A public key `pubkey` and a shared secret `auth`.
        You can generate this as well as the corresponding private key using the
        :ref:`push_subscription_generate_keys() <push_subscription_generate_keys()>` function.

        `policy` controls what sources will generate webpush events. Valid values are
        `all`, `none`, `follower` and `followed`.

        The rest of the parameters controls what kind of events you wish to subscribe to.

        Returns a :ref:`push subscription dict <push subscription dict>`.
        )r   nonefollowerfollowedzDValid values for policy are 'all', 'none', 'follower' or 'followed'.pubkeyauth)zsubscription[endpoint]zsubscription[keys][p256dh]zsubscription[keys][auth]policyNdata[alerts][follow]data[alerts][favourite]data[alerts][reblog]data[alerts][mention]data[alerts][poll]data[alerts][follow_request]zdata[alerts][status]POSTr   )r   r   _Mastodon__protocolizebase64	b64encode_Mastodon__generate_paramsr   )r   endpointencrypt_paramsfollow_eventsfavourite_eventsreblog_eventsmention_eventspoll_eventsfollow_request_eventsstatus_eventsr!   push_pubkey_b64push_auth_b64paramss                 r   push_subscription_setzMastodon.push_subscription_set    s   * @@@./uvvv))(33 *>(+CDD()?@@ '/*9(5	
 
 $-:F)*'0@F,-$-:F)*%.<F*+"+6F'( ,5JF12 ,-:F)* ''//!!&*EvNNNr   c                     i }|||d<   |||d<   |||d<   |||d<   |||d<   |||d<   |                      |          }|                     dd	|          S )
z
        Modifies what kind of events the app wishes to subscribe to.

        Returns the updated :ref:`push subscription dict <push subscription dict>`.
        Nr"   r#   r$   r%   r&   r'   PUTr   )r,   r   )r   r/   r0   r1   r2   r3   r4   r8   s           r   push_subscription_updatez!Mastodon.push_subscription_update^   s     $-:F)*'0@F,-$-:F)*%.<F*+"+6F'( ,5JF12 ''//!!%)DfMMMr   c                 2    |                      dd           dS )z[
        Remove the current push subscription the logged-in user has for this app.
        DELETEr   Nr   r   s    r   push_subscription_deletez!Mastodon.push_subscription_delete   s!    
 	8%@AAAAAr   c                    t           st          d          t          j        t          j                    t                                }|                                j        }	 |                                	                    t          j        j        t          j        j                  }n?#  |                                                                                                }Y nxY wt#          j        d          }||d}||d}||fS )z
        Generates a private key, public key and shared secret for use in webpush subscriptions.

        Returns two dicts: One with the private key and shared secret and another with the
        public key and shared secret.
        ITo use the crypto tools, please install the webpush feature dependencies.   )privkeyr    )r   r    )r   NotImplementedErrorr	   generate_private_key	SECP256R1r   private_numbersprivate_value
public_keypublic_bytesr
   EncodingX962PublicFormatUncompressedPointpublic_numbersencode_pointosurandom)r   push_key_pairpush_key_privpush_key_pubpush_shared_secret	priv_dictpub_dicts          r   push_subscription_generate_keysz(Mastodon.push_subscription_generate_keys   s     	]%[] ] ] /@Q@QRR%5577E	V(3355BB&+*< LL	V(3355DDFFSSUULLLZ^^ %&
 
	 #&
 

 (""s   #AB* *:C&c           
      
   t           rt          st          d          |                     |                    d          d                                                   }|                     |                    d          d                             d          d                                                   }|                     |                    d          d                                                   }t          j        |d         t          j                    t                                }t          j        ||||||d	         d
d          }	t          j        |	                    d          t          j                  S )a  
        Decrypts `data` received in a webpush request. Requires the private key dict
        from :ref:`push_subscription_generate_keys() <push_subscription_generate_keys()>` (`decrypt_params`) as well as the
        Encryption and server Crypto-Key headers from the received webpush

        Returns the decoded webpush as a :ref:`push notification dict <push notification dict>`.
        rA   zsalt=r   zdh=;r   z
p256ecdsa=rC   r    zP-256aesgcm)saltkeyprivate_keydhauth_secretkeylabelversionzutf-8)object_hook)r   r   rD   _Mastodon__decode_webpush_b64splitstripr	   derive_private_keyrF   r   r   decryptjsonloadsdecoder   _Mastodon__json_hooks)
r   datadecrypt_paramsencryption_headercrypto_key_headerr]   dhparams	p256ecdsadec_key	decrypteds
             r   push_subscription_decrypt_pushz'Mastodon.push_subscription_decrypt_push   s`     	]o 	]%[] ] ] (():)@)@)I)I!)L)R)R)T)TUU,,->-D-DU-K-KA-N-T-TUX-Y-YZ[-\-b-b-d-dee--.?.E.El.S.STU.V.\.\.^.^__	'y(A2<>>SbSdSdee$&v.	
 	
 	
	 z)**733AVWWWWr   )NNNNNNNr   )NNNNNN)__name__
__module____qualname__r   r   r   r9   r<   r?   rY   r   rv    r   r   r   r      s1        ['#566F F 76F ['#566LPCG?CUZ;O ;O ;O 76;Oz ['#56659FJBF7; N  N  N 76 ND ['7++B B ,+B!# !# !#F ['#;<<X X =<X X Xr   r   )r*   rQ   rj   versionsr   r   errorsr   utilityr   compatr   r	   r
   r   r   r   	internalsr   	Internalsrz   r   r   <module>r      s     				  B B B B B B B B 0 0 0 0 0 0             G G G G G G G G G G G G * * * * * * * * , , , , , ,zX zX zX zX zXy zX zX zX zX zXr   