
    ~h;                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 ddl
mZ ddl
mZ d	d
lmZ d	dlmZ d	dlmZ d	dlmZ  G d d      Zy)    Ngenerate_token)default_json_headers)	JoseError)JsonWebToken   )AccessDeniedError)InvalidRequestError   )ClientMetadataClaims)InvalidClientMetadataError)InvalidSoftwareStatementError) UnapprovedSoftwareStatementErrorc                   n    e Zd ZdZdZdZd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 Zy)ClientRegistrationEndpointzThe client registration endpoint is an OAuth 2.0 endpoint designed to
    allow a client to be registered with the authorization server.
    client_registrationNc                 2    || _         |xs t        g| _        y N)serverr   claims_classes)selfr   r   s      [/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/authlib/oauth2/rfc7591/endpoint.py__init__z#ClientRegistrationEndpoint.__init__   s    ,F1E0F    c                 $    | j                  |      S r   )create_registration_responser   requests     r   __call__z#ClientRegistrationEndpoint.__call__!   s    0099r   c                 V   | j                  |      }|s
t               ||_        | j                  |      }| j	                         }i }|j                  |       |j                  |       | j                  |||      }| j                  ||      }|r|j                  |       d|t        fS )N   )	authenticate_tokenr	   
credentialextract_client_metadatagenerate_client_infoupdatesave_client!generate_client_registration_infor   )r   r   tokenclient_metadataclient_infobodyclientregistration_infos           r   r   z7ClientRegistrationEndpoint.create_registration_response$   s    ''0#%%"66w?//1O$K !!+H BB67SKK)*D...r   c                 b   |j                   j                  s
t               |j                   j                  j                         }|j	                  dd       }|r/| j
                  r#| j                  ||      }|j                  |       i }| j                         }| j                  D ]_  }t        |d      r|r|j                  |      ni } ||i ||      }		 |	j                           |j                  di |	j!                          a |S # t        $ r}
t        |
j                        |
d }
~
ww xY w)Nsoftware_statementget_claims_options )payloaddatar
   copypop'software_statement_alg_values_supportedextract_software_statementr&   get_server_metadatar   hasattrr1   validater   r   descriptionget_registered_claims)r   r   	json_datar0   r4   r*   server_metadataclaims_classoptionsclaimserrors              r   r$   z2ClientRegistrationEndpoint.extract_client_metadata6   s.   ##%''OO((--/	&]]+?F$"N"N223EwODT"224 // 	EL <)=>? //@ 
 ")R/JFO! #O""DV%A%A%CD	E 	  O01B1BCNOs   D

	D.D))D.c                     | j                  |      }|s
t               	 t        | j                        }|j	                  ||      }|S # t
        $ r}t               |d }~ww xY wr   )resolve_public_keyr   r   r7   decoder   r   )r   r0   r   keyjwtrB   excs          r   r8   z5ClientRegistrationEndpoint.extract_software_statementQ   sd    %%g.244	;tKKLCZZ 2C8FM 	;/1s:	;s   (A 	A!AA!c                     | j                         }| j                         }t        t        j                               }d}t	        ||||      S )Nr   )	client_idclient_secretclient_id_issued_atclient_secret_expires_at)generate_client_idgenerate_client_secretinttimedict)r   rK   rL   rM   rN   s        r   r%   z/ClientRegistrationEndpoint.generate_client_info^   sN    ++-	335!$))+.#$ ' 3%=	
 	
r   c                      y)zGenerate ```registration_client_uri`` and ``registration_access_token``
        for RFC7592. This method returns ``None`` by default. Developers MAY rewrite
        this method to return registration information.
        Nr2   )r   r-   r   s      r   r(   z<ClientRegistrationEndpoint.generate_client_registration_infok   s    
 r   c                 8    | j                   j                  |      S r   )r   create_json_requestr   s     r   create_endpoint_requestz2ClientRegistrationEndpoint.create_endpoint_requestr   s    {{..w77r   c                     t        d      S )zGenerate ``client_id`` value. Developers MAY rewrite this method
        to use their own way to generate ``client_id``.
        *   r   r   s    r   rO   z-ClientRegistrationEndpoint.generate_client_idu   s     b!!r   c                 p    t        j                  t        j                  d            j	                  d      S )zGenerate ``client_secret`` value. Developers MAY rewrite this method
        to use their own way to generate ``client_secret``.
           ascii)binasciihexlifyosurandomrF   rZ   s    r   rP   z1ClientRegistrationEndpoint.generate_client_secret{   s(     

2/66w??r   c                     t               )zeReturn server metadata which includes supported grant types,
        response types and etc.
        NotImplementedErrorrZ   s    r   r9   z.ClientRegistrationEndpoint.get_server_metadata   s     "##r   c                     t               )aL  Authenticate current credential who is requesting to register a client.
        Developers MUST implement this method in subclass::

            def authenticate_token(self, request):
                auth = request.headers.get("Authorization")
                return get_token_by_auth(auth)

        :return: token instance
        rc   r   s     r   r"   z-ClientRegistrationEndpoint.authenticate_token        "##r   c                     t               )aI  Resolve a public key for decoding ``software_statement``. If
        ``enable_software_statement=True``, developers MUST implement this
        method in subclass::

            def resolve_public_key(self, request):
                return get_public_key_from_user(request.credential)

        :return: JWK or Key string
        rc   r   s     r   rE   z-ClientRegistrationEndpoint.resolve_public_key   rf   r   c                     t               )a  Save client into database. Developers MUST implement this method
        in subclass::

            def save_client(self, client_info, client_metadata, request):
                client = OAuthClient(
                    client_id=client_info['client_id'],
                    client_secret=client_info['client_secret'],
                    ...
                )
                client.save()
                return client
        rc   )r   r+   r*   r   s       r   r'   z&ClientRegistrationEndpoint.save_client   s     "##r   )NN)__name__
__module____qualname____doc__ENDPOINT_NAMEr7   r   r   r   r$   r8   r%   r(   rW   rO   rP   r9   r"   rE   r'   r2   r   r   r   r      s^     *M /3+G:/$6;
8"@$
$
$$r   r   )r^   r`   rR   authlib.common.securityr   authlib.constsr   authlib.joser   r   rfc6749r	   r
   rB   r   errorsr   r   r   r   r2   r   r   <module>rs      s8     	  2 / " % ' ) ( . 1 4Z$ Z$r   