
    k7i74                         S SK Jr  S SKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr   " S S\5      rS rg)    )ContinueIteration)	deprecate   )ClientAuthentication)InvalidScopeError)OAuth2Error)UnsupportedGrantTypeError)UnsupportedResponseTypeError)Hookable)hooked)JsonRequest)OAuth2Request)scope_to_listc                      ^  \ rS rSrSrSU 4S jjrS rS r    SS jrS r	SS jr
S	 rS
 rS rS rS\4S jrS\4S jrS rS rSS jrS r\S 5       rSS jrS rSS jr\S S j5       rSS jrS rSrU =r $ )!AuthorizationServer   zAuthorization server that handles Authorization Endpoint and Token
Endpoint.

:param scopes_supported: A list of supported scopes by this authorization server.
c                    > [         TU ]  5         Xl        0 U l        S U l        / U l        / U l        0 U l        / U l        g N)	super__init__scopes_supported_token_generators_client_auth_authorization_grants_token_grants
_endpoints_extensions)selfr   	__class__s     e/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth2/rfc6749/authorization_server.pyr   AuthorizationServer.__init__   sC     0!# %'"    c                     [        5       e)zQuery OAuth client by client_id. The client model class MUST
implement the methods described by
:class:`~authlib.oauth2.rfc6749.ClientMixin`.
NotImplementedError)r   	client_ids     r    query_client AuthorizationServer.query_client!   s    
 "##r"   c                     [        5       e)z:Define function to save the generated token into database.r$   )r   tokenrequests      r    
save_tokenAuthorizationServer.save_token(       !##r"   c           	          U R                   R                  U5      nU(       d  U R                   R                  S5      nU(       d  [        S5      eU" UUUUUUS9$ )a]  Generate the token dict.

:param grant_type: current requested grant_type.
:param client: the client that making the request.
:param user: current authorized user.
:param expires_in: if provided, use this value as expires_in.
:param scope: current requested scope.
:param include_refresh_token: should refresh_token be included.
:return: Token dict
defaultzNo configured token generator)
grant_typeclientuserscope
expires_ininclude_refresh_token)r   getRuntimeError)r   r1   r2   r3   r4   r5   r6   funcs           r    generate_token"AuthorizationServer.generate_token,   sa    ( %%))*5))--i8D>??!!"7
 	
r"   c                      X R                   U'   g)a  Register a function as token generator for the given ``grant_type``.
Developers MUST register a default token generator with a special
``grant_type=default``::

    def generate_bearer_token(
        grant_type,
        client,
        user=None,
        scope=None,
        expires_in=None,
        include_refresh_token=True,
    ):
        token = {"token_type": "Bearer", "access_token": ...}
        if include_refresh_token:
            token["refresh_token"] = ...
        ...
        return token


    authorization_server.register_token_generator(
        "default", generate_bearer_token
    )

If you register a generator for a certain grant type, that generator will only works
for the given grant type::

    authorization_server.register_token_generator(
        "client_credentials",
        generate_bearer_token,
    )

:param grant_type: string name of the grant type
:param func: a function to generate token
N)r   )r   r1   r9   s      r    register_token_generator,AuthorizationServer.register_token_generatorP   s    F .2z*r"   c                     U R                   c+  U R                  (       a  [        U R                  5      U l         U R                  XU5      $ )zAuthenticate client via HTTP request information with the given
methods, such as ``client_secret_basic``, ``client_secret_post``.
)r   r'   r   )r   r+   methodsendpoints       r    authenticate_client'AuthorizationServer.authenticate_clientu   s>     $):): 4T5F5F GD  8<<r"   c                     U R                   c+  U R                  (       a  [        U R                  5      U l         U R                   R                  X5        g)a  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   methodr9   s      r    register_client_auth_method/AuthorizationServer.register_client_auth_method}   s?    0 $):): 4T5F5F GD""60r"   c                 F    U R                   R                  U" U 5      5        g r   )r   append)r   	extensions     r    register_extension&AuthorizationServer.register_extension   s    	$0r"   c                     g)zFReturn a URI for the given error, framework may implement this method.N r   r+   errors      r    get_error_uri!AuthorizationServer.get_error_uri   s    r"   c                     [        5       e)zMFramework integration can re-implement this method to support
signal system.
r$   )r   nameargskwargss       r    send_signalAuthorizationServer.send_signal   s     "##r"   returnc                     [        5       e)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
r$   r   r+   s     r    create_oauth2_request)AuthorizationServer.create_oauth2_request        "##r"   c                     [        5       e)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\   s     r    create_json_request'AuthorizationServer.create_json_request   r_   r"   c                     [        5       e)z=Return HTTP response. Framework MUST implement this function.r$   )r   statusbodyheaderss       r    handle_response#AuthorizationServer.handle_response   r.   r"   c                     U(       aZ  U R                   (       aH  [        [        U5      5      n[        U R                   5      R                  U5      (       d
  [	        5       eggg)zzValidate if requested scope is supported by Authorization Server.
Developers CAN re-write this method to meet your needs.
N)r   setr   
issupersetr   )r   r4   scopess      r    validate_requested_scope,AuthorizationServer.validate_requested_scope   sP     T**u-.Ft,,-88@@')) A +5r"   c                     [        US5      (       a  U R                  R                  X45        [        US5      (       a  U R                  R                  X45        gg)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   rJ   r   )r   	grant_cls
extensionss      r    register_grant"AuthorizationServer.register_grant   sS     9<==&&--y.EF9455%%y&=> 6r"   c                     [        U[        5      (       a	  U" U 5      nOXl        U R                  R	                  UR
                  / 5      nUR                  U5        g)zAdd extra endpoint to authorization server. e.g.
RevocationEndpoint::

    authorization_server.register_endpoint(RevocationEndpoint)

:param endpoint_cls: A endpoint class or instance.
N)
isinstancetypeserverr   
setdefaultENDPOINT_NAMErJ   )r   rA   	endpointss      r    register_endpoint%AuthorizationServer.register_endpoint   sI     h%%~H"OOO..x/E/ErJ	"r"   c                 
   U R                    H)  u  p#UR                  U5      (       d  M  [        X#X5      s  $    [        SUR                  R
                   S3UR                  R
                  UR                  R                  S9e)zsFind the authorization grant for current request.

:param request: OAuth2Request instance.
:return: grant instance
zThe response type 'z!' is not supported by the server.)redirect_uri)r   rp   _create_grantr
   payloadresponse_typer   r   r+   rs   rt   s       r    get_authorization_grant+AuthorizationServer.get_authorization_grant   sw     &*%?%?!I55g>>$YGJJ &@ +!'//"?"?!@@abOO)) 55
 	
r"   c                     U R                  U5      n X!l        U R                  U5      nUR                  U5        UR	                  5         U$ ! [
         a!  nUR                  R                  Ul        e SnAff = f)zValidate current HTTP request for authorization page. This page
is designed for resource owner to grant or deny the authorization.
N)r]   r3   r   &validate_no_multiple_request_parametervalidate_consent_requestr   r   state)r   r+   end_usergrantrQ   s        r    get_consent_grant%AuthorizationServer.get_consent_grant   sw     ,,W5	#L009E88A**,   	 "////EK	s   8A 
A8A33A8c                     U R                    H)  u  p#UR                  U5      (       d  M  [        X#X5      s  $    [        UR                  R
                  5      e)zkFind the token grant for current request.

:param request: OAuth2Request instance.
:return: grant instance
)r   rq   r   r	   r   r1   r   s       r    get_token_grant#AuthorizationServer.get_token_grant  sL     &*%7%7!I--g66$YGJJ &8 ((B(BCCr"   c                 *   XR                   ;  a  [        SU S35      eU R                   U   nU H*  nUR                  U5      n U R                  " U" U5      6 s  $    g! [         a     M<  [
         a  nU R                  X%5      s SnAs  $ SnAff = f)zValidate endpoint request and create endpoint response.

:param name: Endpoint name
:param request: HTTP request instance.
:return: Response
zThere is no 'z' endpoint.N)r   r8   create_endpoint_requestrg   r   r   handle_error_response)r   rU   r+   r}   rA   rQ   s         r    create_endpoint_response,AuthorizationServer.create_endpoint_response  s     &tfK@AAOOD)	!H66w?GB++Xg->?? " %  B11'AABs$   A
B,	B5BBBc                 *   [        U[        5      (       d  U R                  U5      nU(       d  [        SSS9   U R	                  U5      n UR                  5       nUR                  XR5      nU R                  " U6 nUR                  SU5        U$ ! [
         a6  nUR                  R                  Ul        U R                  X5      s SnA$ SnAff = f! [         a6  nUR                  R                  Ul        U R                  X5      n SnANSnAff = f)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
z,The 'grant' parameter will become mandatory.z1.8)versionNafter_authorization_response)rx   r   r]   r   r   r
   r   r   r   validate_authorization_requestcreate_authorization_responserg   r   execute_hook)r   r+   
grant_userr   rQ   r   rV   responses           r    r   1AuthorizationServer.create_authorization_response.  s     '=11009GDeTB44W=
	B ??AL66|PD++T2H
 	98D 0 B%oo3311'AAB  	B!////EK11'AH	Bs5   B 0C 
C+C
C
C
D,DDc                 H   U R                  U5      n U R                  U5      n UR	                  5         UR                  5       nU R                  " U6 $ ! [         a  nU R                  X5      s SnA$ SnAff = f! [         a  nU R                  X5      s SnA$ SnAff = f)zYValidate token request and create token response.

:param request: HTTP request instance
N)r]   r   r	   r   validate_token_requestcreate_token_responserg   r   )r   r+   r   rQ   rV   s        r    r   )AuthorizationServer.create_token_responseM  s    
 ,,W5	>((1E	>((*..0D''.. ) 	>--g==	>  	>--g==	>s:   A .A< 
A9A4.A94A9<
B!BB!B!c                 J    U R                   " U" U R                  X5      5      6 $ r   )rg   rR   rP   s      r    r   )AuthorizationServer.handle_error_response_  s#    ##U4+=+=g+M%NOOr"   )r   r   r   r   r   r   r   r   )NNNT)r*   )NN)NNN)!__name__
__module____qualname____firstlineno____doc__r   r'   r,   r:   r=   rB   rG   rL   rR   rX   r   r]   r   ra   rg   rm   ru   r~   r   r   r   r   r   r   r   r   __static_attributes____classcell__)r   s   @r    r   r      s    $$ ""
H#2J=1:1$$ $$k $$*?&#  
 
 *	DB(  <>$P Pr"   r   c                 F    U " X#5      nU(       a  U H  nU" U5        M     U$ r   rO   )rs   rt   r+   rz   r   exts         r    r   r   c  s%    g&ECJ Lr"   N)authlib.common.errorsr   authlib.deprecater   rB   r   errorsr   r   r	   r
   hooksr   r   requestsr   r   utilr   r   r   rO   r"   r    <module>r      s?    3 ' 5 %  - 0   ! # PP( PPf
r"   