o
    ^[2hT"                     @   sr   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 dZdS )    )default_json_headers)	JoseError   )AccessDeniedError)InvalidClientError)InvalidRequestError)UnauthorizedClientError)InvalidClientMetadataError)ClientMetadataClaimsc                   @   s   e Zd ZdZd%ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdS )&ClientConfigurationEndpointclient_configurationNc                 C   s   || _ |ptg| _d S N)serverr
   claims_classes)selfr   r    r   p/home/skpark/git/infrasmart_work/infrasmart/venv/lib/python3.10/site-packages/authlib/oauth2/rfc7592/endpoint.py__init__   s   z$ClientConfigurationEndpoint.__init__c                 C   s
   |  |S r   )create_configuration_responser   requestr   r   r   __call__   s   
z$ClientConfigurationEndpoint.__call__c                 C   s   |  |}|s
t ||_| |}|s | || tddd| ||s,tddd||_|j	dkr:| 
||S |j	dkrE| ||S |j	dkrP| ||S d S )	Ni  z)The client does not exist on this server.)status_codedescriptioni  z7The client does not have permission to read its record.GETDELETEPUT)authenticate_tokenr   
credentialauthenticate_clientrevoke_access_tokenr   check_permissionr   clientmethodcreate_read_client_responsecreate_delete_client_responsecreate_update_client_response)r   r   tokenr"   r   r   r   r      s.   




z9ClientConfigurationEndpoint.create_configuration_responsec                 C   s   | j |S r   )r   create_json_requestr   r   r   r   create_endpoint_request:   s   z3ClientConfigurationEndpoint.create_endpoint_requestc                 C   s&   |  |}|| || d|tfS )N   )introspect_clientupdate!generate_client_registration_infor   )r   r"   r   bodyr   r   r   r$   =   s   

z7ClientConfigurationEndpoint.create_read_client_responsec                 C   s   |  || ddg}dd|fS )N)zCache-Controlzno-store)Pragmazno-cache    )delete_client)r   r"   r   headersr   r   r   r%   B   s
   
z9ClientConfigurationEndpoint.create_delete_client_responsec                 C   s   d}|D ]}||j jv rt q|j jd}|st || kr%t d|j jv r7||j jd s7t | |}| |||}| ||S )N)registration_access_tokenregistration_client_uriclient_secret_expires_atclient_id_issued_at	client_idclient_secret)	payloaddatar   getget_client_idcheck_client_secretextract_client_metadataupdate_clientr$   )r   r"   r   must_not_includekr8   client_metadatar   r   r   r&   J   s    
z9ClientConfigurationEndpoint.create_update_client_responsec           	      C   s   |j j }i }|  }| jD ]:}t|dr|r||ni }||i ||}z|  W n ty> } zt	|j
|d }~ww |jdi |  q|S )Nget_claims_optionsr   )r:   r;   copyget_server_metadatar   hasattrrD   validater   r	   r   r,   get_registered_claims)	r   r   	json_datarC   server_metadataclaims_classoptionsclaimserrorr   r   r   r?   j   s&   
z3ClientConfigurationEndpoint.extract_client_metadatac                 C   s   i |j |jS r   )client_inforC   )r   r"   r   r   r   r+   }   s   z-ClientConfigurationEndpoint.introspect_clientc                 C      t  )a  Generate ```registration_client_uri`` and ``registration_access_token``
        for RFC7592. By default this method returns the values sent in the current
        request. Developers MUST rewrite this method to return different registration
        information.::

            def generate_client_registration_info(self, client, request):{
                access_token = request.headers['Authorization'].split(' ')[1]
                return {
                    'registration_client_uri': request.uri,
                    'registration_access_token': access_token,
                }

        :param client: the instance of OAuth client
        :param request: formatted request instance
        NotImplementedErrorr   r"   r   r   r   r   r-         z=ClientConfigurationEndpoint.generate_client_registration_infoc                 C   rQ   )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
        rR   r   r   r   r   r         
z.ClientConfigurationEndpoint.authenticate_tokenc                 C   rQ   )a<  Read a client from the request payload.
        Developers MUST implement this method in subclass::

            def authenticate_client(self, request):
                client_id = request.payload.data.get("client_id")
                return Client.get(client_id=client_id)

        :return: client instance
        rR   r   r   r   r   r      rV   z/ClientConfigurationEndpoint.authenticate_clientc                 C   rQ   )a  Revoke a token access in case an invalid client has been requested.
        Developers MUST implement this method in subclass::

            def revoke_access_token(self, token, request):
                token.revoked = True
                token.save()

        rR   )r   r'   r   r   r   r   r          	z/ClientConfigurationEndpoint.revoke_access_tokenc                 C   rQ   )a  Checks whether the current client is allowed to be accessed, edited
        or deleted. Developers MUST implement it in subclass, e.g.::

            def check_permission(self, client, request):
                return client.editable

        :return: boolean
        rR   rT   r   r   r   r!      rW   z,ClientConfigurationEndpoint.check_permissionc                 C   rQ   )a2  Delete authorization code from database or cache. Developers MUST
        implement it in subclass, e.g.::

            def delete_client(self, client, request):
                client.delete()

        :param client: the instance of OAuth client
        :param request: formatted request instance
        rR   rT   r   r   r   r2      rV   z)ClientConfigurationEndpoint.delete_clientc                 C   rQ   )a:  Update the client in the database. Developers MUST implement this method
        in subclass::

            def update_client(self, client, client_metadata, request):
                client.set_client_metadata(
                    {**client.client_metadata, **client_metadata}
                )
                client.save()
                return client

        :param client: the instance of OAuth client
        :param client_metadata: a dict of the client claims to update
        :param request: formatted request instance
        :return: client instance
        rR   )r   r"   rC   r   r   r   r   r@      rU   z)ClientConfigurationEndpoint.update_clientc                 C   rQ   )zeReturn server metadata which includes supported grant types,
        response types and etc.
        rR   )r   r   r   r   rF      s   z/ClientConfigurationEndpoint.get_server_metadata)NN)__name__
__module____qualname__ENDPOINT_NAMEr   r   r   r)   r$   r%   r&   r?   r+   r-   r   r   r    r!   r2   r@   rF   r   r   r   r   r      s&    
$ r   N)authlib.constsr   authlib.joser   rfc6749r   r   r   r   rfc7591r	   rfc7591.claimsr
   r   r   r   r   r   <module>   s    