o
    bD$                     @   sL   d dl mZ d dlmZmZmZmZ d dlmZ G dd de	Z
dd ZdS )	   )ClientAuthentication)OAuth2ErrorInvalidGrantErrorInvalidScopeErrorUnsupportedGrantTypeError)scope_to_listc                   @   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dZd(ddZdd Zdd Zdd Zd(dd Zd'd!d"Zd(d#d$Zd%d& ZdS ))AuthorizationServera  Authorization server that handles Authorization Endpoint and Token
    Endpoint.

    :param query_client: A function to get client by client_id. The client
        model class MUST implement the methods described by
        :class:`~authlib.oauth2.rfc6749.ClientMixin`.
    :param save_token: A method to save tokens.
    :param generate_token: A method to generate tokens.
    :param metadata: A dict of Authorization Server Metadata
    Nc                 C   s4   || _ || _|| _|| _d | _g | _g | _i | _d S N)query_client
save_tokengenerate_tokenmetadata_client_auth_authorization_grants_token_grants
_endpoints)selfr
   r   r   r    r   j/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/oauth2/rfc6749/authorization_server.py__init__   s   
zAuthorizationServer.__init__c                 C   s(   | j du r| jrt| j| _ |  ||S )zAuthenticate client via HTTP request information with the given
        methods, such as ``client_secret_basic``, ``client_secret_post``.
        N)r   r
   r   )r   requestmethodsr   r   r   authenticate_client!   s   z'AuthorizationServer.authenticate_clientc                 C   s.   | j du r| jrt| j| _ | j || dS )af  Add more client auth method. The default methods are:

        * none: The client is a public client and does not have a client secret
        * client_secret_post: The client uses the HTTP POST parameters
        * client_secret_basic: The client uses HTTP Basic

        :param method: Name of the Auth method
        :param func: Function to authenticate the client

        The auth method accept two parameters: ``query_client`` and ``request``,
        an example for this method::

            def authenticate_client_via_custom(query_client, request):
                client_id = request.headers['X-Client-Id']
                client = query_client(client_id)
                do_some_validation(client)
                return client

            authorization_server.register_client_auth_method(
                'custom', authenticate_client_via_custom)
        N)r   r
   r   register)r   methodfuncr   r   r   register_client_auth_method)   s   z/AuthorizationServer.register_client_auth_methodc                 C      dS )zwReturn a translations instance used for i18n error messages.
        Framework SHOULD implement this function.
        Nr   r   r   r   r   r   get_translationsD      z$AuthorizationServer.get_translationsc                 C   r   )z_Return a dict of error uris mapping. Framework SHOULD implement
        this function.
        Nr   r   r   r   r   get_error_urisJ   r    z"AuthorizationServer.get_error_urisc                 O   r   )z]Framework integration can re-implement this method to support
        signal system.
        Nr   )r   nameargskwargsr   r   r   send_signalP   r    zAuthorizationServer.send_signalc                 C      t  )zThis method MUST be implemented in framework integrations. It is
        used to create an OAuth2Request instance.

        :param request: the "request" instance in framework
        :return: OAuth2Request instance
        NotImplementedErrorr   r   r   r   create_oauth2_requestV      z)AuthorizationServer.create_oauth2_requestc                 C   r&   )zThis method MUST be implemented in framework integrations. It is
        used to create an HttpRequest instance.

        :param request: the "request" instance in framework
        :return: HttpRequest instance
        r'   r   r   r   r   create_json_request_   r*   z'AuthorizationServer.create_json_requestc                 C   r&   )z=Return HTTP response. Framework MUST implement this function.r'   )r   statusbodyheadersr   r   r   handle_responseh   s   z#AuthorizationServer.handle_responsec                 C   sN   |r| j r!| j d}tt|}|r#t||s%t|ddS dS dS dS )zValidate if requested scope is supported by Authorization Server.
        Developers CAN re-write this method to meet your needs.
        scopes_supported)stateN)r   getsetr   
issupersetr   )r   scoper1   r0   Zscopesr   r   r   validate_requested_scopel   s   

z,AuthorizationServer.validate_requested_scopec                 C   s<   t |dr| j||f t |dr| j||f dS dS )a  Register a grant class into the endpoint registry. Developers
        can implement the grants in ``authlib.oauth2.rfc6749.grants`` and
        register with this method::

            class AuthorizationCodeGrant(grants.AuthorizationCodeGrant):
                def authenticate_user(self, credential):
                    # ...

            authorization_server.register_grant(AuthorizationCodeGrant)

        :param grant_cls: a grant class.
        :param extensions: extensions for the grant class.
        check_authorization_endpointcheck_token_endpointN)hasattrr   appendr   )r   	grant_cls
extensionsr   r   r   register_grantv   s
   

z"AuthorizationServer.register_grantc                 C   s   || | j |j< dS )zAdd extra endpoint to authorization server. e.g.
        RevocationEndpoint::

            authorization_server.register_endpoint(RevocationEndpoint)

        :param endpoint_cls: A endpoint class
        N)r   ZENDPOINT_NAME)r   Zendpoint_clsr   r   r   register_endpoint   s   z%AuthorizationServer.register_endpointc                 C   s<   | j D ]\}}||rt||||   S qtd|j)zFind the authorization grant for current request.

        :param request: OAuth2Request instance.
        :return: grant instance
        z#Response type {!r} is not supported)r   r7   _create_grantr   formatZresponse_typer   r   r;   r<   r   r   r   get_authorization_grant   s   

z+AuthorizationServer.get_authorization_grantc                 C   sH   | j D ]\}}||r|j|jv rt||||   S qtd|j)zFind the token grant for current request.

        :param request: OAuth2Request instance.
        :return: grant instance
        z Grant type {!r} is not supported)r   r8   r   ZTOKEN_ENDPOINT_HTTP_METHODSr?   r   r@   Z
grant_typerA   r   r   r   get_token_grant   s   


z#AuthorizationServer.get_token_grantc              
   C   sn   || j vrtd|| j | }||}z| j|| W S  ty6 } z| ||W  Y d}~S d}~ww )zValidate endpoint request and create endpoint response.

        :param name: Endpoint name
        :param request: HTTP request instance.
        :return: Response
        zThere is no "{}" endpoint.N)r   RuntimeErrorr@   Zcreate_endpoint_requestr/   r   handle_error_response)r   r"   r   Zendpointerrorr   r   r   create_endpoint_response   s   


z,AuthorizationServer.create_endpoint_responsec              
   C   s   |  |}z| |}W n ty$ } z| ||W  Y d}~S d}~ww z| }|||}| j| W S  tyM } z| ||W  Y d}~S d}~ww )zValidate authorization request and create authorization response.

        :param request: HTTP request instance.
        :param grant_user: if granted, it is resource owner. If denied,
            it is None.
        :returns: Response
        N)r)   rB   r   rE   Zvalidate_authorization_requestcreate_authorization_responser/   r   )r   r   Z
grant_usergrantrF   Zredirect_urir#   r   r   r   rH      s   
z1AuthorizationServer.create_authorization_responsec              
   C   s   |  |}z| |}W n ty$ } z| ||W  Y d}~S d}~ww z|  | }| j| W S  tyK } z| ||W  Y d}~S d}~ww )ziValidate token request and create token response.

        :param request: HTTP request instance
        N)r)   rC   r   rE   Zvalidate_token_requestcreate_token_responser/   r   )r   r   rI   rF   r#   r   r   r   rJ      s   
z)AuthorizationServer.create_token_responsec                 C   s   | j || || |d S )N)ZtranslationsZ
error_uris)r/   r   r!   )r   r   rF   r   r   r   rE      s   z)AuthorizationServer.handle_error_response)NNr	   )__name__
__module____qualname____doc__r   r   r   r   r!   r%   r)   r+   r/   r6   r=   r>   rB   rC   rG   rH   rJ   rE   r   r   r   r   r      s(    

		






r   c                 C   s$   | ||}|r|D ]}|| q	|S r	   r   )r;   r<   r   serverrI   extr   r   r   r?      s
   

r?   N)r   r   errorsr   r   r   r   utilr   objectr   r?   r   r   r   r   <module>   s     a