U
    [e%                     @   s   d dl Z d dlZd dlZd dlZzd dlZW n ek
rH   d dlZY nX zd dlmZ W n  ek
rz   d dl	mZ Y nX d dl
m
Z
 d dlT G dd deZdS )    N)parse_qs)datetime)*c                   @   sn   e Zd ZdZdZdZdZdZdddZdd Z	dd Z
ee
Z
d	d
 ZG dd deZG dd deZeZdS )SignedRequestz
    Facebook uses "signed requests" to communicate with applications on the Facebook platform. See `Facebook's
    documentation on authentication <https://developers.facebook.com/docs/authentication/signed_request/>`_
    for more information.
    Nc                 C   sl  || _ || _|| _|| _| ||| _| jdd| _d| jkrt| j| jd d| jd d| jd ddnd| _	d| jkr| 
  | j| jd	| jd d
d| jd ddd| jd krt| jd d d d| jd d kr| jd d d d ndndd| jkr^| jj| jd t| jd | jd dkrVt| jd nddndd| _dS )z
        Initialize a signed request.

        :param signed_request: A string describing a signed request.
        :param application_secret_key: A string describing a Facebook application's secret key.
        app_dataNpageidlikedadminr   is_likedis_adminuseruser_idlocalecountryageminmax   d   oauth_token	issued_atexpiresr   tokenr   
expires_atr   r   r   r   r   )signed_requestapplication_secret_keyapplication_idapi_versionparserawgetdataPager   fetch_user_data_and_tokenUserrange
OAuthTokenr   fromtimestampr   )selfr   r   r    r!    r-   W/var/www/html/services/stratfitenv/lib/python3.8/site-packages/facepy/signed_request.py__init__)   sD    

,
"zSignedRequest.__init__c                 C   s   ddl m}m} || j| j| jd}||| jd}|jd| jd d| j| jd}|d	 | jd
< t |d  | jd< || jd | jd< d S )Nr   )GraphAPIget_application_access_token)r!   )versionzoauth/access_tokencode )r3   Zredirect_uriZ	client_idZclient_secretZaccess_tokenr   
expires_inr   r   r   )	r4   r0   r1   r    r   r!   r$   r#   time)r,   r0   r1   Z	app_tokengraphqsr-   r-   r.   r'   Q   s    z'SignedRequest.fetch_user_data_and_tokenc           	   	   C   s   dd }z:dd | ddD \}}||}t||d}W n  ttfk
rb   tdY nX |d	d
 dkrtdt	j
|d|dtjd }||krtd|S )zFParse a signed request, returning a dictionary describing its payload.c                 S   s   dt | d  }t| | S )N=   )lenbase64urlsafe_b64decode)encodedpaddingr-   r-   r.   decode^   s    z#SignedRequest.parse.<locals>.decodec                 s   s   | ]}t |V  qd S N)str).0stringr-   r-   r.   	<genexpr>c   s     z&SignedRequest.parse.<locals>.<genexpr>.   utf-8z$Signed request had a corrupt payload	algorithmr4   HMAC-SHA256z,Signed request is using an unknown algorithm)msg	digestmodz!Signed request signature mismatch)splitjsonloadsr@   	TypeError
ValueErrorSignedRequestErrorr$   upperhmacnewencodehashlibsha256digest)	clsr   r   r@   encoded_signatureencoded_payload	signatureZsigned_request_dataZexpected_signaturer-   r-   r.   r"   \   s    
zSignedRequest.parsec                 C   s  ddi}| j r| j |d< | jrni |d< | jjr>| jj|d d< | jjrV| jj|d d< | jjrn| jj|d d< | jrXi |d< | jjr| jj|d d	< | jjr| jj|d d
< | jjr| jjd | jjd d|d d< | jj	rX| jj	j
r| jj	j
|d< | jj	jdkrd|d< ntt| jj	j |d< | jj	jrXtt| jj	j |d< | jjrn| jj|d< ttj|ddd}tt| jd|tj }d||d S )z-Generate a signed request from this instance.rI   rJ   r   r   r   r	   r
   r   r   r   r   )r   r   r   r   Nr5   r   r   ),:)
separatorsrH   z%(signature)s.%(payload)s)r]   payload)r%   r   r   r   r   r   r   r   r   r   r   r   intr6   mktime	timetupler   r<   urlsafe_b64encoderN   dumpsrV   rT   rU   r   rW   rX   rY   )r,   rb   r\   r[   r-   r-   r.   generateu   sZ     








zSignedRequest.generatec                   @   s6   e Zd ZdZdZdZdZdZdddZe	dd ZdS )	zSignedRequest.PagezA
        A ``Page`` instance represents a Facebook page.
        NFc                 C   s   |||  | _ | _| _d S rA   r   )r,   r   r   r   r-   r-   r.   r/      s    zSignedRequest.Page.__init__c                 C   s
   d| j  S )Nhttp://facebook.com/%sr   r,   r-   r-   r.   url   s    zSignedRequest.Page.url)FF)
__name__
__module____qualname____doc__r   r   r   rl   r/   propertyr-   r-   r-   r.   r&      s   
r&   c                   @   sV   e Zd ZdZdZdZdZdZdZdddZ	e
dd Ze
dd ZG d	d
 d
eZdS )zSignedRequest.UserzA
        A ``User`` instance represents a Facebook user.
        Nc                 C   s"   || _ || _|| _|| _|| _d S rA   r   )r,   r   r   r   r   r   r-   r-   r.   r/      s
    zSignedRequest.User.__init__c                 C   s
   d| j  S )z;A string describing the URL to the user's Facebook profile.ri   rj   rk   r-   r-   r.   profile_url   s    zSignedRequest.User.profile_urlc                 C   s
   t | jS )zEA boolean describing whether the user has authorized the application.)boolr   rk   r-   r-   r.   has_authorized_application   s    z-SignedRequest.User.has_authorized_applicationc                   @   s0   e Zd ZdZdZdZdZdd Zedd Z	dS )zSignedRequest.User.OAuthTokenz
            An OAuth token represents an access token that may be used to query
            Facebook's Graph API on behalf of the user that issued it.
            Nc                 C   s   |||  | _ | _| _d S rA   r   )r,   r   r   r   r-   r-   r.   r/     s    z&SignedRequest.User.OAuthToken.__init__c                 C   s    | j dkrdS | j t k S dS )z:A boolean describing whether the access token has expired.NF)r   r   nowrk   r-   r-   r.   has_expired  s    
z)SignedRequest.User.OAuthToken.has_expired)
rm   rn   ro   rp   r   r   r   r/   rq   rv   r-   r-   r-   r.   r*      s   r*   )NNNN)rm   rn   ro   rp   r   r   r   r   r   r/   rq   rr   rt   objectr*   r-   r-   r-   r.   r(      s   


r(   )NNNN)rm   rn   ro   rp   r   r%   r   r#   r/   r'   r"   classmethodrh   rw   r&   r(   rR   Errorr-   r-   r-   r.   r      s   
(C@r   )r<   rW   rT   r6   Z
simplejsonrN   ImportErrorurllib.parser   urlparser   Zfacepy.exceptionsrw   r   r-   r-   r-   r.   <module>   s   