
    lKj!                     R    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ  G d de      Z	y)    N)jwt)import_any_keygenerate_token)BearerTokenGeneratorc                        e Zd ZdZ	 	 	 d fd	Zd Zd Zdeee   z  fdZ	dedz  fdZ
dedz  fd	Zdee   dz  fd
ZdefdZd Z xZS )JWTBearerTokenGeneratora  A JWT formatted access token generator.

    :param issuer: The issuer identifier. Will appear in the JWT ``iss`` claim.

    :param \\*\\*kwargs: Other parameters are inherited from
        :class:`~authlib.oauth2.rfc6750.token.BearerTokenGenerator`.

    This token generator can be registered into the authorization server::

        class MyJWTBearerTokenGenerator(JWTBearerTokenGenerator):
            def get_jwks(self): ...

            def get_extra_claims(self, client, grant_type, user, scope): ...


        authorization_server.register_token_generator(
            "default",
            MyJWTBearerTokenGenerator(
                issuer="https://authorization-server.example.org"
            ),
        )
    Nc                 X    t         |   | j                  ||       || _        || _        y )N)super__init__access_token_generatorissueralg)selfr   r   refresh_token_generatorexpires_generator	__class__s        m/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/authlib/oauth2/rfc9068/token.pyr   z JWTBearerTokenGenerator.__init__"   s1     	'')@BS	
     c                     t               )zReturn the JWKs that will be used to sign the JWT access token.
        Developers MUST re-implement this method::

            def get_jwks(self):
                return load_jwks("jwks.json")
        )NotImplementedError)r   s    r   get_jwksz JWTBearerTokenGenerator.get_jwks/   s     "##r   c                     i S )aY  Return extra claims to add in the JWT access token. Developers MAY
        re-implement this method to add identity claims like the ones in
        :ref:`specs/oidc` ID Token, or any other arbitrary claims::

            def get_extra_claims(self, client, grant_type, user, scope):
                return generate_user_info(user, scope)
         r   client
grant_typeuserscopes        r   get_extra_claimsz(JWTBearerTokenGenerator.get_extra_claims8   s	     	r   returnc                 "    |j                         S )aj  Return the audience for the token. By default this simply returns
        the client ID. Developers MAY re-implement this method to add extra
        audiences::

            def get_audiences(self, client, user, scope):
                return [
                    client.get_client_id(),
                    resource_server.get_id(),
                ]
        )get_client_id)r   r   r   r   s       r   get_audiencesz%JWTBearerTokenGenerator.get_audiencesB   s     ##%%r   c                      y)a  Authentication Context Class Reference.
        Returns a user-defined case sensitive string indicating the class of
        authentication the used performed. Token audience may refuse to give access to
        some resources if some ACR criteria are not met.
        :ref:`specs/oidc` defines one special value: ``0`` means that the user
        authentication did not respect `ISO29115`_ level 1, and will be refused monetary
        operations. Developers MAY re-implement this method::

            def get_acr(self, user):
                if user.insecure_session():
                    return "0"
                return "urn:mace:incommon:iap:silver"

        .. _ISO29115: https://www.iso.org/standard/45138.html
        Nr   r   r   s     r   get_acrzJWTBearerTokenGenerator.get_acrO   s      r   c                      y)a}  User authentication time.
        Time when the End-User authentication occurred. Its value is a JSON number
        representing the number of seconds from 1970-01-01T0:0:0Z as measured in UTC
        until the date/time. Developers MAY re-implement this method::

            def get_auth_time(self, user):
                return datetime.timestamp(user.get_auth_time())
        Nr   r&   s     r   get_auth_timez%JWTBearerTokenGenerator.get_auth_timea        r   c                      y)a{  Authentication Methods References.
        Defined by :ref:`specs/oidc` as an option list of user-defined case-sensitive
        strings indication which authentication methods have been used to authenticate
        the user. Developers MAY re-implement this method::

            def get_amr(self, user):
                return ["2FA"] if user.has_2fa_enabled() else []
        Nr   r&   s     r   get_amrzJWTBearerTokenGenerator.get_amrl   r*   r   c                     t        d      S )zJWT ID.
        Create an unique identifier for the token. Developers MAY re-implement
        this method::

            def get_jti(self, client, grant_type, user scope):
                return generate_random_string(16)
           r   r   s        r   get_jtizJWTBearerTokenGenerator.get_jtiw   s     b!!r   c           
         t        t        j                               }|| j                  ||      z   }| j                  ||j	                         || j                  ||||      |d}|r|j                         |d<   n|j	                         |d<   	 | j                  |||      |d<   | j                  |      x}r||d<   | j                  |      x}	r|	|d<   | j                  |      x}
r|
|d<   |j                  | j                  ||||             | j                  dd}t        | j                               }t!        j"                  |||	      }|S )
N)issexp	client_idiatjtir   subaud	auth_timeacramrzat+jwt)r   typ)key)inttime_get_expires_inr   r#   r/   get_user_idr$   r)   r'   r,   updater    r   r   r   r   encode)r   r   r   r   r   now
expires_in
token_datar8   r9   r:   headerr<   access_tokens                 r   r   z.JWTBearerTokenGenerator.access_token_generator   so   $))+4//
CC
 ;;--/<<
D%@

  $ 0 0 2Ju !' 4 4 6Ju  !% 2 264 GJu **40090&/J{#
 ,,t$$3$ #Ju
 ,,t$$3$ #Ju 	$//
D%PQ (3T]]_-zz

 r   )RS256NN)__name__
__module____qualname____doc__r   r   r    strlistr$   r'   r=   r)   r,   r/   r   __classcell__)r   s   @r   r	   r	   
   s}    4  $$&C$s)O &sTz $	S4Z 		tCy4/ 	"# "Xr   r	   )
r>   joserfcr   authlib._joserfc_helpersr   authlib.common.securityr   authlib.oauth2.rfc6750.tokenr   r	   r   r   r   <module>rT      s#      3 2 =O2 Or   