
    i[f                         d dl Z d dlm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  e j        e          Z G d d	e
          Z	dS )
    N)import_string)Response)request)OAuth1RequestAuthorizationServergenerate_token)
url_encodec                        e Zd ZdZd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 fd	Zd fd	Zd Zd Z x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                 ~    || _         || _        || _        d d d d d d d| _        ||                     |           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   s       j/var/www/piapp/venv/lib/python3.11/site-packages/authlib/integrations/flask_oauth1/authorization_server.py__init__zAuthorizationServer.__init__   sa    (. !+/(,+/-1'+
 
 ?MM# ?    c                     ||| _         ||| _        | j        |                     |          | _        |j                            d          }|r#t          |t          t          f          r|| _        || _	        d S )N"OAUTH1_SUPPORTED_SIGNATURE_METHODS)
r   r   create_token_generatorconfigget
isinstancelisttupleSUPPORTED_SIGNATURE_METHODSr   )r   r   r   r   methodss        r   r   zAuthorizationServer.init_app/   s    # ,D&#2D '#'#>#>s#C#CD *..!EFF 	7z'D%=99 	7/6D,r   c                 J    || j         vrt          d          || j         |<   d S )NzInvalid "name" of hook)r   
ValueError)r   namefuncs      r   register_hookz!AuthorizationServer.register_hook>   s0    t{""5666 Dr   c                    |j                             d          t          t                    rt	                    n |j                             dd          fd|j                             d          t          t                    rt	                    n |j                             dd          fdfd	}|S )
NOAUTH1_TOKEN_GENERATOROAUTH1_TOKEN_LENGTH*   c                  "    t                     S Nr   lengths   r   r   zCAuthorizationServer.create_token_generator.<locals>.token_generatorK       %f---r   OAUTH1_TOKEN_SECRET_GENERATOROAUTH1_TOKEN_SECRET_LENGTH0   c                  "    t                     S r0   r   r1   s   r   secret_generatorzDAuthorizationServer.create_token_generator.<locals>.secret_generatorT   r3   r   c                  .                               dS )N)oauth_tokenoauth_token_secret )r8   r   s   r   create_tokenz@AuthorizationServer.create_token_generator.<locals>.create_tokenW   s)    .00&6&6&8&8  r   )r   r    r!   strr   )r   r   r=   r2   r8   r   s      @@@r   r   z*AuthorizationServer.create_token_generatorC   s    *..)ABBos++ 	.+O<<OOZ^^$92>>F. . . . . :>>*IJJ&,, 	.,-=>>Z^^$@"EEF. . . . .	 	 	 	 	 	
 r   c                 ,    |                      |          S r0   )r   )r   	client_ids     r   get_client_by_idz$AuthorizationServer.get_client_by_id^   s      +++r   c                     | j         d         }t          |          r#|j        }|j        }|j        } |||||          S t          d          )Nr   z "exists_nonce" hook is required.)r   callable	timestampr@   tokenRuntimeError)r   noncer   r)   rD   r@   rE   s          r   r   z AuthorizationServer.exists_noncea   sX    {>*D>> 	<)I)IME4y)U;;;=>>>r   c                     | j         d         }t          |          r+|                                 } |||j        |j                  S t          d          )Nr   z/"create_temporary_credential" hook is required.)r   rC   r   r@   redirect_urirF   )r   r   r)   rE   s       r   r   z/AuthorizationServer.create_temporary_credentialk   s^    {89D>> 	H((**E4w0'2FGGG=
 
 	
r   c                 x    | j         d         }t          |          r ||j                  S t          d          )Nr   z,"get_temporary_credential" hook is required.r   rC   rE   rF   r   r   r)   s      r   r   z,AuthorizationServer.get_temporary_credentialt   sD    {56D>> 	'4&&&:
 
 	
r   c                 x    | j         d         }t          |          r ||j                  S t          d          )Nr   z/"delete_temporary_credential" hook is required.rK   rL   s      r   r   z/AuthorizationServer.delete_temporary_credential}   sD    {89D>> 	'4&&&=
 
 	
r   c                     | j         d         }t          |          r(t          d          } ||j        |j        |           |S t          d          )Nr   $   z1"create_authorization_verifier" hook is required.)r   rC   r	   
credentialuserrF   )r   r   r)   verifiers       r   r   z1AuthorizationServer.create_authorization_verifier   s]    {:;D>> 	%b))HD#W\8<<<O?
 
 	
r   c                     | j         d         }t          |          r'|j        }|                                 } |||          S t	          d          )Nr   z+"create_token_credential" hook is required.)r   rC   rP   r   rF   )r   r   r)   temporary_credentialrE   s        r   r   z+AuthorizationServer.create_token_credential   s_    {45D>> 	5#*#5 ((**E434449
 
 	
r   c                 Z    |                      d           }|                     |           |S r0   )create_oauth1_requestvalidate_authorization_request)r   reqs     r   check_authorization_requestz/AuthorizationServer.check_authorization_request   s.    ((..++C000
r   c                 H    t                                          ||          S r0   )supercreate_authorization_response)r   r   
grant_user	__class__s      r   r\   z1AuthorizationServer.create_authorization_response   s     ww**7J??	@r   c                 F    t                                          |          S r0   )r[   create_token_response)r   r   r^   s     r   r`   z)AuthorizationServer.create_token_response   s    ww,,W555r   c                     |t           }|j        dv r|j                            d          }nd }t	          |j        |j        ||j                  S )N)POSTPUTT)flat)	flask_reqmethodformto_dictr   urlheaders)r   r   bodys      r   rV   z)AuthorizationServer.create_oauth1_request   sR    ?G>_,,<''T'22DDDW^W[$PPPr   c                 @    t          t          |          ||          S )N)statusrj   )r   r
   )r   status_codepayloadrj   s       r   handle_responsez#AuthorizationServer.handle_response   s*    w
 
 
 	
r   )NNN)NNr0   )__name__
__module____qualname____doc__r   r   r*   r   rA   r   r   r   r   r   r   rY   r\   r`   rV   rp   __classcell__)r^   s   @r   r   r      sJ               ! ! !
  6, , ,? ? ?
 
 

 
 

 
 
	
 	
 	
	
 	
 	
  
@ @ @ @ @ @6 6 6 6 6 6Q Q Q
 
 
 
 
 
 
r   r   )loggingwerkzeug.utilsr   flaskr   r   re   authlib.oauth1r   r   _AuthorizationServerauthlib.common.securityr	   authlib.common.urlsr
   	getLoggerrq   logr<   r   r   <module>r      s     ( ( ( ( ( (       & & & & & &        3 2 2 2 2 2 * * * * * *g!!g
 g
 g
 g
 g
. g
 g
 g
 g
 g
r   