o
    b                     @   sr   d dl Z d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ ddlmZ e eZG d	d
 d
eZdS )    N)import_string)Response)OAuth1RequestAuthorizationServergenerate_token)
url_encode   )create_oauth_requestc                       s   e Zd ZdZd%d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' fdd	Zdd Zd& fdd	Zd' fdd 	Zd!d" Zd#d$ Z  ZS )(r   ax  Flask implementation of :class:`authlib.rfc5849.AuthorizationServer`.
    Initialize it with Flask app instance, client model class and cache::

        server = AuthorizationServer(app=app, query_client=query_client)
        # or initialize lazily
        server = AuthorizationServer()
        server.init_app(app, query_client=query_client)

    :param app: A Flask app instance
    :param query_client: A function to get client by client_id. The client
        model class MUST implement the methods described by
        :class:`~authlib.oauth1.rfc5849.ClientMixin`.
    :param token_generator: A function to generate token
    Nc                 C   s@   || _ || _|| _d d d d d d d| _|d ur| | d S d S )N)exists_noncecreate_temporary_credentialget_temporary_credentialdelete_temporary_credentialcreate_authorization_verifiercreate_token_credential)appquery_clienttoken_generator_hooksinit_app)selfr   r   r    r   u/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/integrations/flask_oauth1/authorization_server.py__init__   s   zAuthorizationServer.__init__c                 C   s`   |d ur|| _ |d ur|| _| jd u r| || _|jd}|r+t|ttfr+|| _|| _	d S )NZ"OAUTH1_SUPPORTED_SIGNATURE_METHODS)
r   r   create_token_generatorconfigget
isinstancelisttupleZSUPPORTED_SIGNATURE_METHODSr   )r   r   r   r   methodsr   r   r   r   /   s   

zAuthorizationServer.init_appc                 C   s    || j vr	td|| j |< d S )NzInvalid "name" of hook)r   
ValueError)r   namefuncr   r   r   register_hook>   s   
z!AuthorizationServer.register_hookc                    s   |j dttrtn|j dd  fdd|j dttr-tn|j dd  fd	d
fdd}|S )NZOAUTH1_TOKEN_GENERATORZOAUTH1_TOKEN_LENGTH*   c                         t  S Nr   r   lengthr   r   r   K      zCAuthorizationServer.create_token_generator.<locals>.token_generatorZOAUTH1_TOKEN_SECRET_GENERATORZOAUTH1_TOKEN_SECRET_LENGTH0   c                      r&   r'   r   r   r(   r   r   secret_generatorT   r*   zDAuthorizationServer.create_token_generator.<locals>.secret_generatorc                      s      dS )N)Zoauth_tokenZoauth_token_secretr   r   )r,   r   r   r   create_tokenW   s   z@AuthorizationServer.create_token_generator.<locals>.create_token)r   r   r   strr   )r   r   r-   r   )r)   r,   r   r   r   C   s   



z*AuthorizationServer.create_token_generatorc                 C   s
   |  |S r'   )r   )r   	client_idr   r   r   get_client_by_id^      
z$AuthorizationServer.get_client_by_idc                 C   s:   | j d }t|r|j}|j}|j}|||||S td)Nr   z "exists_nonce" hook is required.)r   callable	timestampr/   tokenRuntimeError)r   noncerequestr#   r3   r/   r4   r   r   r   r   a   s   
z AuthorizationServer.exists_noncec                 C   s2   | j d }t|r|  }|||j|jS td)Nr   z/"create_temporary_credential" hook is required.)r   r2   r   r/   Zredirect_urir5   )r   r7   r#   r4   r   r   r   r   k   s   
z/AuthorizationServer.create_temporary_credentialc                 C   $   | j d }t|r||jS td)Nr   z,"get_temporary_credential" hook is required.r   r2   r4   r5   r   r7   r#   r   r   r   r   t      

z,AuthorizationServer.get_temporary_credentialc                 C   r8   )Nr   z/"delete_temporary_credential" hook is required.r9   r:   r   r   r   r   }   r;   z/AuthorizationServer.delete_temporary_credentialc                 C   s6   | j d }t|rtd}||j|j| |S td)Nr   $   z1"create_authorization_verifier" hook is required.)r   r2   r   
credentialuserr5   )r   r7   r#   Zverifierr   r   r   r      s   
z1AuthorizationServer.create_authorization_verifierc                 C   s2   | j d }t|r|j}|  }|||S td)Nr   z+"create_token_credential" hook is required.)r   r2   r=   r   r5   )r   r7   r#   Ztemporary_credentialr4   r   r   r   r      s   

z+AuthorizationServer.create_token_credentialc                       t t| |S r'   )superr   %create_temporary_credentials_responser   r7   	__class__r   r   rA      s   z9AuthorizationServer.create_temporary_credentials_responsec                 C   s   |  d }| | |S r'   )create_oauth1_requestZvalidate_authorization_request)r   reqr   r   r   check_authorization_request   s   

z/AuthorizationServer.check_authorization_requestc                    s   t t| ||S r'   )r@   r   create_authorization_response)r   r7   Z
grant_userrC   r   r   rH      s   z1AuthorizationServer.create_authorization_responsec                    r?   r'   )r@   r   create_token_responserB   rC   r   r   rI      s   z)AuthorizationServer.create_token_responsec                 C   s
   t |tS r'   )r
   r   rB   r   r   r   rE      r1   z)AuthorizationServer.create_oauth1_requestc                 C   s   t t|||dS )N)statusheaders)r   r   )r   status_codepayloadrK   r   r   r   handle_response   s
   z#AuthorizationServer.handle_response)NNN)NNr'   )__name__
__module____qualname____doc__r   r   r$   r   r0   r   r   r   r   r   r   rA   rG   rH   rI   rE   rN   __classcell__r   r   rC   r   r      s&    


			r   )loggingZwerkzeug.utilsr   Zflaskr   Zauthlib.oauth1r   r   Z_AuthorizationServerZauthlib.common.securityr   Zauthlib.common.urlsr   Zflask_helpersr
   	getLoggerrO   logr   r   r   r   <module>   s    
