o
    Rhf&                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dlm	Z
 d dlmZmZmZmZ 	 	 G dd dZ	 G dd	 d	Z	 	 	 dddZG dd deZG dd dZG dd deZdS )    N)	timedelta)RequestValidatorServerDeviceApplicationServer)errors)AccessDeniedAuthorizationPendingErrorExpiredTokenErrorSlowDownErrorc                   @   sB   e Zd ZG dd dejZdZdZdZdZ	dZ
dZdZdZdZdS )Devicec                   @   s   e Zd ZdZdZdZdZdS )zDevice.DeviceFlowStatus
AuthorizedAuthorization_pendingExpiredDeniedN)__name__
__module____qualname__
AUTHORIZEDAUTHORIZATION_PENDINGEXPIREDDENIED r   r   [/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/examples/device_code_flow.pyDeviceFlowStatus-   s
    r   .N)r   r   r   enumEnumr   iddevice_code	user_codescopeintervalexpiresstatus	client_idlast_checkedr   r   r   r   r   ,   s    r   c                   @   s    e Zd Zedd Zdd ZdS )DeviceAuthorizationEndpointc                 C   s   t ddd}|| S )N   zhttps://example.com/device)r    verification_uri)r   $create_device_authorization_response)requestserverr   r   r   r(   `   s   
z@DeviceAuthorizationEndpoint.create_device_authorization_responsec                 C   s   |  |\}}}d}|S )N.)r(   )selfr)   headersdatar"   device_responser   r   r   poste   s   z DeviceAuthorizationEndpoint.postN)r   r   r   staticmethodr(   r/   r   r   r   r   r%   _   s    
r%   1/5sc                    s    fdd}|S )Nc                     sJ   t } tjjtjd}|t| j }| }|| _|k r t   }|S )N)tz)	r   datetimenowUTCr   r$   total_secondsdevice_flow_errorsr
   )devicer4   diff$total_seconds_since_last_device_pollresultfuncrater   r   wrapper   s   zrate_limit.<locals>.wrapperr   )r=   r>   r?   r   r<   r   
rate_limit   s   r@   c                   @      e Zd Zdd ZdS )ExampleRequestValidatorc                 C   s   d S Nr   )r+   r   r   r   create_token_response   s    z-ExampleRequestValidator.create_token_responseN)r   r   r   rD   r   r   r   r   rB      s    rB   c                   @   rA   )ServerSetupForTokenEndpointc                 C   s   t }t|| _d S rC   )rB   r   r*   )r+   	validatorr   r   r   __init__   s   z$ServerSetupForTokenEndpoint.__init__N)r   r   r   rG   r   r   r   r   rE      s    rE   c                   @   s(   e Zd Zdd Zedd Zdd ZdS )TokenEndpointc                 C   s(   | j |\}}}}t|d}|S )Naccess_token)r*   rD   jsonloadsget)r+   r)   urlr,   bodyr"   rI   r   r   r   default_flow_token_response   s   z)TokenEndpoint.default_flow_token_responsec           	      C   s\   t }|j|jjkrt |j|jjkrt | j|\}}}}t	
|d}|j|_|S )z
        Following the rfc, this will route the device request accordingly and raise
        required errors.

        Remember that unlike other auth flows, the device if polling this endpoint once
        every "interval" amount of seconds.
        rI   )r   r"   r   r   r   r   r   r*   rD   rJ   rK   rL   r   )	r+   r)   r   r8   rM   r,   rN   r"   rI   r   r   r   device_flow_token_response   s   
z(TokenEndpoint.device_flow_token_responsec                 C   s.   |j }|ddkr| ||d S | |S )N
grant_typez,urn:ietf:params:oauth:grant-type:device_coder   )POSTrL   rP   rO   )r+   r)   paramsr   r   r   r/      s   
zTokenEndpoint.postN)r   r   r   rO   r@   rP   r/   r   r   r   r   rH      s
    
rH   )r1   )r   rJ   r3   r   oauthlib.oauth2r   r   r   oauthlib.oauth2.rfc8628r   r7   oauthlib.oauth2.rfc8628.errorsr   r   r	   r
   r   r%   r@   rB   rE   rH   r   r   r   r   <module>   s&    

	