Format: application/x-python-code
⬇ Download File (13.9 KB)
   1  o
   2  
   3  G'h�>�@sUddlmZddlmZddlZddlmZddlmZddlmZddl	m
   4  Z
   5  ddlmZdd	l
   6  mZdd
   7  l
   8  mZedd��Zeed
   9  <defdd�Zdefdd�Zdefdd�Zdeefdd�Zdefdd�Z	d=dedeededefdd�Z				d>ded ed!eefd"d#�Z			d?ded!eefd$d%�Zdedefd&d'�Zdedeefd(d)�Zdedefd*d+�Z	d@d,ed-eddfd.d/�Z 		d@d,ed0ed1ee!d2eeddf
  10  d3d4�Z"dAd,ed2eeddfd5d6�Z#dAd,ed2eeddfd7d8�Z$dAd,ed2eeddfd9d:�Z%defd;d<�Z&dS)B�)�Any)�OptionalN)�g)�Response)�
  11  LocalProxy)�config)�get_jwt_manager)�ExpiresDelta)�FreshcCst�S�N��get_current_user�rr�NC:\Users\shubh\Videos\agent\nova\Lib\site-packages\flask_jwt_extended/utils.py�<lambda>sr�current_user�returncC� t�dd�}|durtd��|S)a=
  12      In a protected endpoint, this will return the python dictionary which has
  13      the payload of the JWT that is accessing the endpoint. If no JWT is present
  14      due to ``jwt_required(optional=True)``, an empty dictionary is returned.
  15  
  16      :return:
  17          The payload (claims) of the JWT in the current request
  18      Z_jwt_extended_jwtN�UYou must call `@jwt_required()` or `verify_jwt_in_request()` before using this method�r�get�RuntimeError)Zdecoded_jwtrrr�get_jwt�	�rcCr)a3
  19      In a protected endpoint, this will return the python dictionary which has
  20      the header of the JWT that is accessing the endpoint. If no JWT is present
  21      due to ``jwt_required(optional=True)``, an empty dictionary is returned.
  22  
  23      :return:
  24          The headers of the JWT in the current request
  25      Z_jwt_extended_jwt_headerNrr)Zdecoded_headerrrr�get_jwt_header$rrcCst��tjd�S)a
  26      In a protected endpoint, this will return the identity of the JWT that is
  27      accessing the endpoint. If no JWT is present due to
  28      ``jwt_required(optional=True)``, ``None`` is returned.
  29  
  30      :return:
  31          The identity of the JWT in the current request
  32      N)rrrZidentity_claim_keyrrrr�get_jwt_identity6s	rcCst�dd�S)a�
  33      In a protected endpoint, this will return the "location" at which the JWT
  34      that is accessing the endpoint was found--e.g., "cookies", "query-string",
  35      "headers", or "json". If no JWT is present due to ``jwt_required(optional=True)``,
  36      None is returned.
  37  
  38      :return:
  39          The location of the JWT in the current request; e.g., "cookies",
  40          "query-string", "headers", or "json"
  41      Z_jwt_extended_jwt_locationN)rrrrrr�get_jwt_request_locationBsrcCs*t�t�dd�}|durtd��|dS)a�
  42      In a protected endpoint, this will return the user object for the JWT that
  43      is accessing the endpoint.
  44  
  45      This is only usable if :meth:`~flask_jwt_extended.JWTManager.user_lookup_loader`
  46      is configured. If the user loader callback is not being used, this will
  47      raise an error.
  48  
  49      If no JWT is present due to ``jwt_required(optional=True)``, ``None`` is returned.
  50  
  51      :return:
  52          The current user object for the JWT in the current request
  53      Z_jwt_extended_jwt_userNzHYou must provide a `@jwt.user_lookup_loader` callback to use this methodZloaded_user)rrrr)Z
  54  jwt_user_dictrrrr
  55  Ps�r
  56  F�
  57  encoded_token�
  58  csrf_value�
  59  allow_expiredcCst�}|�|||�S)a�
  60      Returns the decoded token (python dict) from an encoded JWT. This does all
  61      the checks to ensure that the decoded token is valid before returning it.
  62  
  63      This will not fire the user loader callbacks, save the token for access
  64      in protected endpoints, checked if a token is revoked, etc. This is puerly
  65      used to ensure that a JWT is valid.
  66  
  67      :param encoded_token:
  68          The encoded JWT to decode.
  69  
  70      :param csrf_value:
  71          Expected CSRF double submit value (optional).
  72  
  73      :param allow_expired:
  74          If ``True``, do not raise an error if the JWT is expired.  Defaults to ``False``
  75  
  76      :return:
  77          Dictionary containing the payload of the JWT decoded JWT.
  78      )rZ_decode_jwt_from_config)rrr�jwt_managerrrr�decode_tokenhsr!�identity�fresh�
  79  expires_deltacCst�}|j|||||dd�S)aL
  80      Create a new access token.
  81  
  82      :param identity:
  83          The identity of this token. It can be any data that is json serializable.
  84          You can use :meth:`~flask_jwt_extended.JWTManager.user_identity_loader`
  85          to define a callback function to convert any object passed in into a json
  86          serializable format.
  87  
  88      :param fresh:
  89          If this token should be marked as fresh, and can thus access endpoints
  90          protected with ``@jwt_required(fresh=True)``. Defaults to ``False``.
  91  
  92          This value can also be a ``datetime.timedelta``, which indicate
  93          how long this token will be considered fresh.
  94  
  95      :param expires_delta:
  96          A ``datetime.timedelta`` for how long this token should last before it
  97          expires. Set to False to disable expiration. If this is None, it will use
  98          the ``JWT_ACCESS_TOKEN_EXPIRES`` config value (see :ref:`Configuration Options`)
  99  
 100      :param additional_claims:
 101          Optional. A hash of claims to include in the access token.  These claims are
 102          merged into the default claims (exp, iat, etc) and claims returned from the
 103          :meth:`~flask_jwt_extended.JWTManager.additional_claims_loader` callback.
 104          On conflict, these claims take precedence.
 105  
 106      :param headers:
 107          Optional. A hash of headers to include in the access token. These headers
 108          are merged into the default headers (alg, typ) and headers returned from
 109          the :meth:`~flask_jwt_extended.JWTManager.additional_headers_loader`
 110          callback. On conflict, these headers take precedence.
 111  
 112      :return:
 113          An encoded access token
 114      �access�Zclaimsr$r#�headersr"�
 115  token_type�rZ_encode_jwt_from_config)r"r#r$�additional_claims�additional_headersr rrr�create_access_token�s+�r,cCst�}|j||d||dd�S)a"
 116      Create a new refresh token.
 117  
 118      :param identity:
 119          The identity of this token. It can be any data that is json serializable.
 120          You can use :meth:`~flask_jwt_extended.JWTManager.user_identity_loader`
 121          to define a callback function to convert any object passed in into a json
 122          serializable format.
 123  
 124      :param expires_delta:
 125          A ``datetime.timedelta`` for how long this token should last before it expires.
 126          Set to False to disable expiration. If this is None, it will use the
 127          ``JWT_REFRESH_TOKEN_EXPIRES`` config value (see :ref:`Configuration Options`)
 128  
 129      :param additional_claims:
 130          Optional. A hash of claims to include in the refresh token. These claims are
 131          merged into the default claims (exp, iat, etc) and claims returned from the
 132          :meth:`~flask_jwt_extended.JWTManager.additional_claims_loader` callback.
 133          On conflict, these claims take precedence.
 134  
 135      :param headers:
 136          Optional. A hash of headers to include in the refresh token. These headers
 137          are merged into the default headers (alg, typ) and headers returned from the
 138          :meth:`~flask_jwt_extended.JWTManager.additional_headers_loader` callback.
 139          On conflict, these headers take precedence.
 140  
 141      :return:
 142          An encoded refresh token
 143      F�refreshr&r))r"r$r*r+r rrr�create_refresh_token�s#�r.cCs
 144  t�|�S)z�
 145      Returns the Headers of an encoded JWT without verifying the signature of the JWT.
 146  
 147      :param encoded_token:
 148          The encoded JWT to get the Header from.
 149  
 150      :return:
 151          JWT header parameters as python dict()
 152      )�jwtZget_unverified_header�rrrr�get_unverified_jwt_headers�s
 153  
 154  r1cCst|��d�S)z�
 155      Returns the JTI (unique identifier) of an encoded JWT
 156  
 157      :param encoded_token:
 158          The encoded JWT to get the JTI from.
 159  
 160      :return:
 161          The JTI (unique identifier) of a JWT, if it is present.
 162      Zjti)r!rr0rrr�get_jti�s
 163  r2cCst|�}|dS)z�
 164      Returns the CSRF double submit token from an encoded JWT.
 165  
 166      :param encoded_token:
 167          The encoded JWT
 168  
 169      :return:
 170          The CSRF double submit token (string)
 171      Zcsrf)r!)r�tokenrrr�get_csrf_tokens
 172  r4�response�encoded_access_tokenc
 173  C�x|jtj||p	tjtjd|ptjtjtjd�tjr8tj	r:|jtj
 174  t|�|p(tjtjd|p/tjtjtjd�dSdSdS)a�
 175      Modifiy a Flask Response to set a cookie containing the access JWT.
 176      Also sets the corresponding CSRF cookies if ``JWT_CSRF_IN_COOKIES`` is ``True``
 177      (see :ref:`Configuration Options`)
 178  
 179      :param response:
 180          A Flask Response object.
 181  
 182      :param encoded_access_token:
 183          The encoded access token to set in the cookies.
 184  
 185      :param max_age:
 186          The max age of the cookie. If this is None, it will use the
 187          ``JWT_SESSION_COOKIE`` option (see :ref:`Configuration Options`). Otherwise,
 188          it will use this as the cookies ``max-age`` and the JWT_SESSION_COOKIE option
 189          will be ignored. Values should be the number of seconds (as an integer).
 190  
 191      :param domain:
 192          The domain of the cookie. If this is None, it will use the
 193          ``JWT_COOKIE_DOMAIN`` option (see :ref:`Configuration Options`). Otherwise,
 194          it will use this as the cookies ``domain`` and the JWT_COOKIE_DOMAIN option
 195          will be ignored.
 196      T��value�max_age�secure�httponly�domain�path�samesiteFN)
 197  �
 198  set_cookier�access_cookie_name�cookie_max_age�
 199  cookie_secure�
 200  cookie_domain�access_cookie_path�cookie_samesite�csrf_protect�csrf_in_cookies�access_csrf_cookie_namer4�access_csrf_cookie_path)r5r6r:r=rrr�set_access_cookiess,�
 201  ��rK�encoded_refresh_tokenr:r=c
 202  Cr7)a�
 203      Modifiy a Flask Response to set a cookie containing the refresh JWT.
 204      Also sets the corresponding CSRF cookies if ``JWT_CSRF_IN_COOKIES`` is ``True``
 205      (see :ref:`Configuration Options`)
 206  
 207      :param response:
 208          A Flask Response object.
 209  
 210      :param encoded_refresh_token:
 211          The encoded refresh token to set in the cookies.
 212  
 213      :param max_age:
 214          The max age of the cookie. If this is None, it will use the
 215          ``JWT_SESSION_COOKIE`` option (see :ref:`Configuration Options`). Otherwise,
 216          it will use this as the cookies ``max-age`` and the JWT_SESSION_COOKIE option
 217          will be ignored. Values should be the number of seconds (as an integer).
 218  
 219      :param domain:
 220          The domain of the cookie. If this is None, it will use the
 221          ``JWT_COOKIE_DOMAIN`` option (see :ref:`Configuration Options`). Otherwise,
 222          it will use this as the cookies ``domain`` and the JWT_COOKIE_DOMAIN option
 223          will be ignored.
 224      Tr8FN)
 225  r@r�refresh_cookie_namerBrCrD�refresh_cookie_pathrFrGrH�refresh_csrf_cookie_namer4�refresh_csrf_cookie_path)r5rLr:r=rrr�set_refresh_cookiesAs,�
 226  ��rQcCst||�t||�dS)z�
 227      Modifiy a Flask Response to delete the cookies containing access or refresh
 228      JWTs.  Also deletes the corresponding CSRF cookies if applicable.
 229  
 230      :param response:
 231          A Flask Response object
 232      N)�unset_access_cookies�unset_refresh_cookies�r5r=rrr�unset_jwt_cookiesvs
 233  rUc
 234  C�h|jtjddtjd|p
 235  tjtjtjd�tjr0tjr2|jtj	ddtjd|p'tjtj
 236  tjd�dSdSdS)a�
 237      Modifiy a Flask Response to delete the cookie containing an access JWT.
 238      Also deletes the corresponding CSRF cookie if applicable.
 239  
 240      :param response:
 241          A Flask Response object
 242  
 243      :param domain:
 244          The domain of the cookie. If this is None, it will use the
 245          ``JWT_COOKIE_DOMAIN`` option (see :ref:`Configuration Options`). Otherwise,
 246          it will use this as the cookies ``domain`` and the JWT_COOKIE_DOMAIN option
 247          will be ignored.
 248      �rT�r9�expiresr;r<r=r>r?FN)r@rrArCrDrErFrGrHrIrJrTrrrrR��,�
 249  ��rRc
 250  CrV)a�
 251      Modifiy a Flask Response to delete the cookie containing a refresh JWT.
 252      Also deletes the corresponding CSRF cookie if applicable.
 253  
 254      :param response:
 255          A Flask Response object
 256  
 257      :param domain:
 258          The domain of the cookie. If this is None, it will use the
 259          ``JWT_COOKIE_DOMAIN`` option (see :ref:`Configuration Options`). Otherwise,
 260          it will use this as the cookies ``domain`` and the JWT_COOKIE_DOMAIN option
 261          will be ignored.
 262      rWrTrXFN)r@rrMrCrDrNrFrGrHrOrPrTrrrrS�rZrScCs
 263  dt�iS)Nrrrrrr�current_user_context_processor�s
 264  r[)NF)FNNN)NNN)NNr)'�typingrrr/ZflaskrrZwerkzeug.localrZflask_jwt_extended.configrZ!flask_jwt_extended.internal_utilsrZflask_jwt_extended.typingr	r
 265  r�__annotations__�dictrrr�strrr
 266  �boolr!r,r.r1r2r4rK�intrQrUrRrSr[rrrr�<module>s�����
 267  ����
 268  �8��
 269  �.
 270  
 271  ���
 272  �5�����
 273  �5&&