Format: application/x-python-code
⬇ Download File (21.5 KB)
   1  o
   2  
   3  G'hs]�@sddlZddlmZddlmZddlmZddlZddlmZddlmZddlm	Z	ddlm
   4  Z
   5  dd	lmZdd
   6  lmZddlm
   7  Z
   8  ddlmZdd
   9  lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$dd lm%Z%dd!lm&Z&dd"lm'Z'dd#lm(Z(dd$lm)Z)dd%l*m+Z+dd&l*m,Z,dd'l-m.Z.dd(l-m/Z/dd)l0m1Z1Gd*d+�d+e2�Z3dS),�N)�Any)�Callable)�Optional)�Flask)�DecodeError)�ExpiredSignatureError)�InvalidAudienceError)�InvalidIssuerError)�InvalidTokenError)�MissingRequiredClaimError)�config)�"default_additional_claims_callback)�default_blocklist_callback)�default_decode_key_callback)�default_encode_key_callback)�default_expired_token_callback)�default_invalid_token_callback)�default_jwt_headers_callback)�"default_needs_fresh_token_callback)�default_revoked_token_callback)�#default_token_verification_callback)�*default_token_verification_failed_callback)�default_unauthorized_callback)�default_user_identity_callback)�"default_user_lookup_error_callback)�	CSRFError)�FreshTokenRequired)�InvalidHeaderError)�InvalidQueryParamError)�JWTDecodeError)�NoAuthorizationError)�RevokedTokenError)�UserClaimsVerificationError)�UserLookupError)�WrongTokenError)�_decode_jwt)�_encode_jwt)�ExpiresDelta)�Fresh)�current_user_context_processorc@s�eZdZdZ	d8deededdfdd�Zd9dededdfd	d
  10  �Zdeddfdd�Z	e
  11  deddfd
  12  d��Zdedefdd�Z
  13  dedefdd�Zdedefdd�Zdedefdd�Zdedefdd�Zdedefdd�Zdedefdd�Zdedefdd�Zdedefd d!�Zdedefd"d#�Zdedefd$d%�Zdedefd&d'�Zdedefd(d)�Zdedefd*d+�Zdedefd,d-�Z				d:d.ed/ed0ed1eedef
  14  d2d3�Z 	d8d4ed5ede!fd6d7�Z"dS);�
  15  JWTManagera&
  16      An object used to hold JWT settings and callback functions for the
  17      Flask-JWT-Extended extension.
  18  
  19      Instances of :class:`JWTManager` are *not* bound to specific apps, so
  20      you can create one in the main body of your code and then bind it
  21      to your app in a factory function.
  22      NF�app�add_context_processor�returncCsvt|_t|_t|_t|_t|_	t
  23  |_t|_
  24  t|_t|_t|_t|_t|_d|_t|_t|_|dur9|�||�dSdS)a	
  25          Create the JWTManager instance. You can either pass a flask application
  26          in directly here to register this extension with the flask app, or
  27          call init_app after creating this object (in a factory pattern).
  28  
  29          :param app:
  30              The Flask Application object
  31          :param add_context_processor:
  32              Controls if `current_user` is should be added to flasks template
  33              context (and thus be available for use in Jinja templates). Defaults
  34              to ``False``.
  35          N)r�_decode_key_callbackr�_encode_key_callbackr�_expired_token_callbackr�_invalid_token_callbackr�_jwt_additional_header_callbackr�_needs_fresh_token_callbackr�_revoked_token_callbackr�_token_in_blocklist_callbackr�_token_verification_callbackr�_unauthorized_callbackr
  36  �_user_claims_callbackr�_user_identity_callback�_user_lookup_callbackr�_user_lookup_error_callbackr�#_token_verification_failed_callback�init_app��selfr+r,�r@�TC:\Users\shubh\Videos\agent\nova\Lib\site-packages\flask_jwt_extended/jwt_manager.py�__init__;s&��zJWTManager.__init__cCs@t|d�si|_||jd<|r|�t�|�|�|�|�dS)aY
  37          Register this extension with the flask app.
  38  
  39          :param app:
  40              The Flask Application object
  41          :param add_context_processor:
  42              Controls if `current_user` is should be added to flasks template
  43              context (and thus be available for use in Jinja templates). Defaults
  44              to ``False``.
  45          �
  46  extensionszflask-jwt-extendedN)�hasattrrCZcontext_processorr)�"_set_default_configuration_options�_set_error_handler_callbacksr>r@r@rAr=bs
  47  
  48  
  49  
  50  zJWTManager.init_appcsd|�t��fdd��}|�t��fdd��}|�t��fdd��}|�t��fdd��}|�t��fd	d
  51  ��}|�t��fdd��}|�t��fd
  52  d��}|�t��fdd��}	|�t	��fdd��}
  53  |�t
  54  ��fdd��}|�t��fdd��}|�t��fdd��}
  55  |�t
  56  ��fdd��}|�t��fdd��}|�t��fdd��}|�t��fdd ��}dS)!Nc���t|��S�N�r7�str��e�r?r@rA�handle_csrf_errorz�zBJWTManager._set_error_handler_callbacks.<locals>.handle_csrf_errorcrGrH�r1rJrKrMr@rA�handle_decode_error~rOzDJWTManager._set_error_handler_callbacks.<locals>.handle_decode_errorc���|j|j�SrH)r0�
  57  jwt_header�jwt_datarKrMr@rA�handle_expired_error��zEJWTManager._set_error_handler_callbacks.<locals>.handle_expired_errorcrRrH)r3rSrTrKrMr@rA�handle_fresh_token_required�rVzLJWTManager._set_error_handler_callbacks.<locals>.handle_fresh_token_requiredcrGrHrPrKrMr@rA�#handle_missing_required_claim_error�rOzTJWTManager._set_error_handler_callbacks.<locals>.handle_missing_required_claim_errorcrGrHrPrKrMr@rA�handle_invalid_audience_error�rOzNJWTManager._set_error_handler_callbacks.<locals>.handle_invalid_audience_errorcrGrHrPrKrMr@rA�handle_invalid_issuer_error�rOzLJWTManager._set_error_handler_callbacks.<locals>.handle_invalid_issuer_errorcrGrHrPrKrMr@rA�handle_invalid_header_error�rOzLJWTManager._set_error_handler_callbacks.<locals>.handle_invalid_header_errorcrGrHrPrKrMr@rA�handle_invalid_token_error�rOzKJWTManager._set_error_handler_callbacks.<locals>.handle_invalid_token_errorcrGrHrPrKrMr@rA�handle_jwt_decode_error�rOzHJWTManager._set_error_handler_callbacks.<locals>.handle_jwt_decode_errorcrGrHrIrKrMr@rA�handle_auth_error�rOzBJWTManager._set_error_handler_callbacks.<locals>.handle_auth_errorcrGrHrPrKrMr@rA� handle_invalid_query_param_error�rOzQJWTManager._set_error_handler_callbacks.<locals>.handle_invalid_query_param_errorcrRrH)r4rSrTrKrMr@rA�handle_revoked_token_error�rVzKJWTManager._set_error_handler_callbacks.<locals>.handle_revoked_token_errorcrRrH)r<rSrTrKrMr@rA� handle_failed_token_verification�rVzQJWTManager._set_error_handler_callbacks.<locals>.handle_failed_token_verificationcrRrH)r;rSrTrKrMr@rA�handler_user_lookup_error�rVzJJWTManager._set_error_handler_callbacks.<locals>.handler_user_lookup_errorcrGrHrPrKrMr@rA�handle_wrong_token_error�rOzIJWTManager._set_error_handler_callbacks.<locals>.handle_wrong_token_error)Zerrorhandlerrrrrrrr	rr
  58  rr rr!r"r#r$)r?r+rNrQrUrWrXrYrZr[r\r]r^r_r`rarbrcr@rMrArFys@z'JWTManager._set_error_handler_callbackscCsd|j�dtjdd��|j�dd�|j�dd�|j�dd	�|j�d
  59  d�|j�dd�|j�d
  60  d�|j�dd�|j�dd�|j�dd�|j�dd�|j�dd�|j�dd�|j�dd�|j�dgd��|j�dd�|j�dd�|j�dd�|j�dd�|j�d d�|j�d!d�|j�d"d#�|j�d$d%�|j�d&d'�|j�d(d)�|j�d*d+�|j�d,d�|j�d-d�|j�d.d/�|j�d0d1�|j�d2d3�|j�d4d�|j�d5d6�|j�d7d�|j�d8d�|j�d9d�|j�d:d;�|j�d<tjd=d>��|j�d?d�|j�d@d�|j�dAdB�|j�dCd�dS)DNZJWT_ACCESS_TOKEN_EXPIRES�)�minutesZJWT_ACCESS_COOKIE_NAMEZaccess_token_cookieZJWT_ACCESS_COOKIE_PATH�/ZJWT_ACCESS_CSRF_COOKIE_NAMEZcsrf_access_tokenZJWT_ACCESS_CSRF_COOKIE_PATHZJWT_ACCESS_CSRF_FIELD_NAMEZ
  61  csrf_tokenZJWT_ACCESS_CSRF_HEADER_NAMEzX-CSRF-TOKENZ
  62  JWT_ALGORITHMZHS256ZJWT_COOKIE_CSRF_PROTECTTZJWT_COOKIE_DOMAINZJWT_COOKIE_SAMESITEZJWT_COOKIE_SECUREFZJWT_CSRF_CHECK_FORMZJWT_CSRF_IN_COOKIESZJWT_CSRF_METHODS)�POST�PUT�PATCH�DELETEZJWT_DECODE_ALGORITHMSZJWT_DECODE_AUDIENCEZJWT_DECODE_ISSUERZJWT_DECODE_LEEWAYrZJWT_ENCODE_AUDIENCEZJWT_ENCODE_ISSUERZJWT_ERROR_MESSAGE_KEY�msgZJWT_HEADER_NAME�
  63  AuthorizationZJWT_HEADER_TYPEZBearerZJWT_IDENTITY_CLAIM�subZJWT_JSON_KEYZaccess_tokenZJWT_PRIVATE_KEYZJWT_PUBLIC_KEYZJWT_QUERY_STRING_NAME�jwtZJWT_QUERY_STRING_VALUE_PREFIX�ZJWT_REFRESH_COOKIE_NAMEZrefresh_token_cookieZJWT_REFRESH_COOKIE_PATHZJWT_REFRESH_CSRF_COOKIE_NAMEZcsrf_refresh_tokenZJWT_REFRESH_CSRF_COOKIE_PATHZJWT_REFRESH_CSRF_FIELD_NAMEZJWT_REFRESH_CSRF_HEADER_NAMEZJWT_REFRESH_JSON_KEYZ
  64  refresh_tokenZJWT_REFRESH_TOKEN_EXPIRES�)�daysZJWT_SECRET_KEYZJWT_SESSION_COOKIEZJWT_TOKEN_LOCATION)�headersZJWT_ENCODE_NBF)r�
  65  setdefault�datetime�	timedelta)r+r@r@rArE�sX�z-JWTManager._set_default_configuration_options�callbackcC�
  66  ||_|S)aF
  67          This decorator sets the callback function used to add additional claims
  68          when creating a JWT. The claims returned by this function will be merged
  69          with any claims passed in via the ``additional_claims`` argument to
  70          :func:`~flask_jwt_extended.create_access_token` or
  71          :func:`~flask_jwt_extended.create_refresh_token`.
  72  
  73          The decorated function must take **one** argument.
  74  
  75          The argument is the identity that was used when creating a JWT.
  76  
  77          The decorated function must return a dictionary of claims to add to the JWT.
  78          )r8�r?rvr@r@rA�additional_claims_loader��z#JWTManager.additional_claims_loadercCrw)aK
  79          This decorator sets the callback function used to add additional headers
  80          when creating a JWT. The headers returned by this function will be merged
  81          with any headers passed in via the ``additional_headers`` argument to
  82          :func:`~flask_jwt_extended.create_access_token` or
  83          :func:`~flask_jwt_extended.create_refresh_token`.
  84  
  85          The decorated function must take **one** argument.
  86  
  87          The argument is the identity that was used when creating a JWT.
  88  
  89          The decorated function must return a dictionary of headers to add to the JWT.
  90          )r2rxr@r@rA�additional_headers_loader�rzz$JWTManager.additional_headers_loadercCrw)a�
  91          This decorator sets the callback function for dynamically setting the JWT
  92          decode key based on the **UNVERIFIED** contents of the token. Think
  93          carefully before using this functionality, in most cases you probably
  94          don't need it.
  95  
  96          The decorated function must take **two** arguments.
  97  
  98          The first argument is a dictionary containing the header data of the
  99          unverified JWT.
 100  
 101          The second argument is a dictionary containing the payload data of the
 102          unverified JWT.
 103  
 104          The decorated function must return a *string* that is used to decode and
 105          verify the token.
 106          )r.rxr@r@rA�decode_key_loaderszJWTManager.decode_key_loadercCrw)a�
 107          This decorator sets the callback function for dynamically setting the JWT
 108          encode key based on the tokens identity. Think carefully before using this
 109          functionality, in most cases you probably don't need it.
 110  
 111          The decorated function must take **one** argument.
 112  
 113          The argument is the identity used to create this JWT.
 114  
 115          The decorated function must return a *string* which is the secrete key used to
 116          encode the JWT.
 117          )r/rxr@r@rA�encode_key_loader �
 118  zJWTManager.encode_key_loadercCrw)a�
 119          This decorator sets the callback function for returning a custom
 120          response when an expired JWT is encountered.
 121  
 122          The decorated function must take **two** arguments.
 123  
 124          The first argument is a dictionary containing the header data of the JWT.
 125  
 126          The second argument is a dictionary containing the payload data of the JWT.
 127  
 128          The decorated function must return a Flask Response.
 129          )r0rxr@r@rA�expired_token_loader0r~zJWTManager.expired_token_loadercCrw)a�
 130          This decorator sets the callback function for returning a custom
 131          response when an invalid JWT is encountered.
 132  
 133          This decorator sets the callback function that will be used if an
 134          invalid JWT attempts to access a protected endpoint.
 135  
 136          The decorated function must take **one** argument.
 137  
 138          The argument is a string which contains the reason why a token is invalid.
 139  
 140          The decorated function must return a Flask Response.
 141          )r1rxr@r@rA�invalid_token_loader@rzzJWTManager.invalid_token_loadercCrw)a�
 142          This decorator sets the callback function for returning a custom
 143          response when a valid and non-fresh token is used on an endpoint
 144          that is marked as ``fresh=True``.
 145  
 146          The decorated function must take **two** arguments.
 147  
 148          The first argument is a dictionary containing the header data of the JWT.
 149  
 150          The second argument is a dictionary containing the payload data of the JWT.
 151  
 152          The decorated function must return a Flask Response.
 153          )r3rxr@r@rA�needs_fresh_token_loaderQrzz#JWTManager.needs_fresh_token_loadercCrw)a�
 154          This decorator sets the callback function for returning a custom
 155          response when a revoked token is encountered.
 156  
 157          The decorated function must take **two** arguments.
 158  
 159          The first argument is a dictionary containing the header data of the JWT.
 160  
 161          The second argument is a dictionary containing the payload data of the JWT.
 162  
 163          The decorated function must return a Flask Response.
 164          )r4rxr@r@rA�revoked_token_loaderbr~zJWTManager.revoked_token_loadercCrw)a�
 165          This decorator sets the callback function used to check if a JWT has
 166          been revoked.
 167  
 168          The decorated function must take **two** arguments.
 169  
 170          The first argument is a dictionary containing the header data of the JWT.
 171  
 172          The second argument is a dictionary containing the payload data of the JWT.
 173  
 174          The decorated function must be return ``True`` if the token has been
 175          revoked, ``False`` otherwise.
 176          )r5rxr@r@rA�token_in_blocklist_loaderrrzz$JWTManager.token_in_blocklist_loadercCrw)a�
 177          This decorator sets the callback function used to return a custom
 178          response when the claims verification check fails.
 179  
 180          The decorated function must take **two** arguments.
 181  
 182          The first argument is a dictionary containing the header data of the JWT.
 183  
 184          The second argument is a dictionary containing the payload data of the JWT.
 185  
 186          The decorated function must return a Flask Response.
 187          )r<rxr@r@rA� token_verification_failed_loader�r~z+JWTManager.token_verification_failed_loadercCrw)a�
 188          This decorator sets the callback function used for custom verification
 189          of a valid JWT.
 190  
 191          The decorated function must take **two** arguments.
 192  
 193          The first argument is a dictionary containing the header data of the JWT.
 194  
 195          The second argument is a dictionary containing the payload data of the JWT.
 196  
 197          The decorated function must return ``True`` if the token is valid, or
 198          ``False`` otherwise.
 199          )r6rxr@r@rA�token_verification_loader�rzz$JWTManager.token_verification_loadercCrw)aF
 200          This decorator sets the callback function used to return a custom
 201          response when no JWT is present.
 202  
 203          The decorated function must take **one** argument.
 204  
 205          The argument is a string that explains why the JWT could not be found.
 206  
 207          The decorated function must return a Flask Response.
 208          )r7rxr@r@rA�unauthorized_loader�szJWTManager.unauthorized_loadercCrw)a�
 209          This decorator sets the callback function used to convert an identity to
 210          a JSON serializable format when creating JWTs. This is useful for
 211          using objects (such as SQLAlchemy instances) as the identity when
 212          creating your tokens.
 213  
 214          The decorated function must take **one** argument.
 215  
 216          The argument is the identity that was used when creating a JWT.
 217  
 218          The decorated function must return JSON serializable data.
 219          )r9rxr@r@rA�user_identity_loader�r~zJWTManager.user_identity_loadercCrw)a�
 220          This decorator sets the callback function used to convert a JWT into
 221          a python object that can be used in a protected endpoint. This is useful
 222          for automatically loading a SQLAlchemy instance based on the contents
 223          of the JWT.
 224  
 225          The object returned from this function can be accessed via
 226          :attr:`~flask_jwt_extended.current_user` or
 227          :meth:`~flask_jwt_extended.get_current_user`
 228  
 229          The decorated function must take **two** arguments.
 230  
 231          The first argument is a dictionary containing the header data of the JWT.
 232  
 233          The second argument is a dictionary containing the payload data of the JWT.
 234  
 235          The decorated function can return any python object, which can then be
 236          accessed in a protected endpoint. If an object cannot be loaded, for
 237          example if a user has been deleted from your database, ``None`` must be
 238          returned to indicate that an error occurred loading the user.
 239          )r:rxr@r@rA�user_lookup_loader�szJWTManager.user_lookup_loadercCrw)a�
 240          This decorator sets the callback function used to return a custom
 241          response when loading a user via
 242          :meth:`~flask_jwt_extended.JWTManager.user_lookup_loader` fails.
 243  
 244          The decorated function must take **two** arguments.
 245  
 246          The first argument is a dictionary containing the header data of the JWT.
 247  
 248          The second argument is a dictionary containing the payload data of the JWT.
 249  
 250          The decorated function must return a Flask Response.
 251          )r;rxr@r@rA�user_lookup_error_loader�rzz#JWTManager.user_lookup_error_loader�identity�
 252  token_type�fresh�
 253  expires_deltac	Cs�|�|�}|dur|�|�|�|�}|dur|�|�|dur+|dkr(tj}ntj}ttjtj|tj	||||�
 254  |�tjtjtj
 255  |�|�|tjd�S)N�access)�	algorithm�audience�claim_overridesZcsrfr�r��header_overridesr��identity_claim_key�issuer�json_encoder�secretr�Znbf)r2�updater8rZaccess_expiresZrefresh_expiresr&r�Zencode_audienceZcsrf_protectr9r�Z
 256  encode_issuerr�r/Z
 257  encode_nbf)	r?r�r�Zclaimsr�r�rrr�r�r@r@rA�_encode_jwt_from_config�s4
 258  	
 259  
 260  
 261  �z"JWTManager._encode_jwt_from_config�
 262  encoded_token�
 263  allow_expiredc	
 264  Cs�tj|tjddid�}t�|�}|�||�}tjtj||tjtjtj	|tjdud�	}zt
 265  di|�d|i��WStyR}z||_t
 266  di|�ddi��|_
 267  �d}~ww)NZverify_signatureF)�
 268  algorithms�options)	r�r��
 269  csrf_valuer�r�r��leewayr�Z
 270  verify_audr�Tr@)rn�decoderZdecode_algorithmsZget_unverified_headerr.Zdecode_audiencer�Z
 271  decode_issuerr�r%rrSrT)	r?r�r�r�Zunverified_claimsZunverified_headersr��kwargsrLr@r@rA�_decode_jwt_from_configs2�
 272  ���z"JWTManager._decode_jwt_from_config)NF)F)NFNN)#�__name__�
 273  __module__�__qualname__�__doc__rr�boolrBr=rF�staticmethodrErryr{r|r}rr�r�r�r�r�r�r�r�r�r�rrJr(r'r��dictr�r@r@r@rAr*1sh
 274  ���
 275  �'A.�����
 276  �)����r*)4rt�typingrrrrnZflaskrrrrr	r
 277  rZflask_jwt_extended.configrZ$flask_jwt_extended.default_callbacksr
 278  rrrrrrrrrrrrrZflask_jwt_extended.exceptionsrrrrrr r!r"r#r$Zflask_jwt_extended.tokensr%r&Zflask_jwt_extended.typingr'r(Zflask_jwt_extended.utilsr)�objectr*r@r@r@rA�<module>sV