
    i[f                     P    d dl mZ d dlmZ d dlmZ  G d d          Zd Zd ZdS )	    )default_json_headersgenerate_token)add_params_to_uric                   `    e Zd ZdZdZg dZdZdZdZd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZdS )DeviceAuthorizationEndpointaY	  This OAuth 2.0 [RFC6749] protocol extension enables OAuth clients to
    request user authorization from applications on devices that have
    limited input capabilities or lack a suitable browser.  Such devices
    include smart TVs, media consoles, picture frames, and printers,
    which lack an easy input method or a suitable browser required for
    traditional OAuth interactions. Here is the authorization flow::

        +----------+                                +----------------+
        |          |>---(A)-- Client Identifier --->|                |
        |          |                                |                |
        |          |<---(B)-- Device Code,      ---<|                |
        |          |          User Code,            |                |
        |  Device  |          & Verification URI    |                |
        |  Client  |                                |                |
        |          |  [polling]                     |                |
        |          |>---(E)-- Device Code       --->|                |
        |          |          & Client Identifier   |                |
        |          |                                |  Authorization |
        |          |<---(F)-- Access Token      ---<|     Server     |
        +----------+   (& Optional Refresh Token)   |                |
              v                                     |                |
              :                                     |                |
             (C) User Code & Verification URI       |                |
              :                                     |                |
              v                                     |                |
        +----------+                                |                |
        | End User |                                |                |
        |    at    |<---(D)-- End user reviews  --->|                |
        |  Browser |          authorization request |                |
        +----------+                                +----------------+

    This DeviceAuthorizationEndpoint is the implementation of step (A) and (B).

    (A) The client requests access from the authorization server and
        includes its client identifier in the request.

    (B) The authorization server issues a device code and an end-user
        code and provides the end-user verification URI.
    device_authorization)client_secret_basicclient_secret_postnonestringi     c                     || _         d S N)server)selfr   s     S/var/www/piapp/venv/lib/python3.11/site-packages/authlib/oauth2/rfc8628/endpoint.py__init__z$DeviceAuthorizationEndpoint.__init__<   s        c                 ,    |                      |          S r   )create_endpoint_responser   requests     r   __call__z$DeviceAuthorizationEndpoint.__call__?   s     ,,W555r   c                 6    | j                             |          S r   )r   create_oauth2_requestr   s     r   create_endpoint_requestz3DeviceAuthorizationEndpoint.create_endpoint_requestD   s    {00999r   c                 `    | j                             || j        | j                  }||_        |S )a  client_id is REQUIRED **if the client is not** authenticating with the
        authorization server as described in Section 3.2.1. of [RFC6749].

        This means the endpoint support "none" authentication method. In this case,
        this endpoint's auth methods are:

        - client_secret_basic
        - client_secret_post
        - none

        Developers change the value of ``CLIENT_AUTH_METHODS`` in subclass. For
        instance::

            class MyDeviceAuthorizationEndpoint(DeviceAuthorizationEndpoint):
                # only support ``client_secret_basic`` auth method
                CLIENT_AUTH_METHODS = ['client_secret_basic']
        )r   authenticate_clientCLIENT_AUTH_METHODSENDPOINT_NAMEclient)r   r   r"   s      r   r   z/DeviceAuthorizationEndpoint.authenticate_clientG   s6    $ 00T-t/AC Cr   c                    |                      |           | j                            |j                   |                                 }|                                 }|                                 }t          |d|fg          }||||| j        | j	        d}| 
                    |j        |j        |           d|t          fS )N	user_code)device_coder$   verification_uriverification_uri_complete
expires_ininterval   )r   r   validate_requested_scopescopegenerate_device_codegenerate_user_codeget_verification_urir   
EXPIRES_ININTERVALsave_device_credential	client_idr   )r   r   r%   r$   r&   r'   datas          r   r   z4DeviceAuthorizationEndpoint.create_endpoint_response^   s     	  ))),,W];;;//11++--	4466$5Y78%: %:! '" 0)B/
 
 	##G$5w}dKKKD...r   c                 P    | j         dk    rt                      S t                      S )zA method to generate ``user_code`` value for device authorization
        endpoint. This method will generate a random string like MQNA-JPOZ.
        Developers can rewrite this  method to create their own ``user_code``.
        digital)USER_CODE_TYPEcreate_digital_user_codecreate_string_user_coder   s    r   r.   z.DeviceAuthorizationEndpoint.generate_user_codev   s*     )+++---&(((r   c                      t          d          S )zA method to generate ``device_code`` value for device authorization
        endpoint. This method will generate a random string of 42 characters.
        Developers can rewrite this method to create their own ``device_code``.
        *   r   r:   s    r   r-   z0DeviceAuthorizationEndpoint.generate_device_code   s    
 b!!!r   c                     t                      )zDefine the ``verification_uri`` of device authorization endpoint.
        Developers MUST implement this method in subclass::

            def get_verification_uri(self):
                return 'https://your-company.com/active'
        NotImplementedErrorr:   s    r   r/   z0DeviceAuthorizationEndpoint.get_verification_uri   s     "###r   c                     t                      )av  Save device token into database for later use. Developers MUST
        implement this method in subclass::

            def save_device_credential(self, client_id, scope, data):
                item = DeviceCredential(
                    client_id=client_id,
                    scope=scope,
                    **data
                )
                item.save()
        r>   )r   r3   r,   r4   s       r   r2   z2DeviceAuthorizationEndpoint.save_device_credential   s     "###r   N)__name__
__module____qualname____doc__r!   r    r7   r0   r1   r   r   r   r   r   r.   r-   r/   r2    r   r   r   r      s        & &P +MOOO N J H  6 6 6
: : :  ./ / /0) ) )" " "$ $ $$ $ $ $ $r   r   c                  l    d} d                     t          d|           t          d|           g          S )NBCDFGHJKLMNPQRSTVWXZ-   joinr   bases    r   r9   r9      s2    !D88^At,,nQ.E.EFGGGr   c                      d} d                     t          d|           t          d|           t          d|           g          S )N
0123456789rH      rJ   rL   s    r   r8   r8      sI    D88q$q$q$   r   N)	authlib.constsr   authlib.common.securityr   authlib.common.urlsr   r   r9   r8   rE   r   r   <module>rT      s    / / / / / / 2 2 2 2 2 2 1 1 1 1 1 1V$ V$ V$ V$ V$ V$ V$ V$rH H H
    r   