Format: application/x-python-code
⬇ Download File (10.1 KB)
   1  o
   2  
   3  G'h5�@s�ddlmZddlmZddlmZddlmZddlmZddlmZddlm	Z	ddlm
   4  Z
   5  dd	lmZdd
   6  lm
   7  Z
   8  ddlmZddlmZdd
   9  lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddl!m"Z"ddl!m#Z#ee$e	dfZ%de&de&ddfd d!�Z'	"	"	"		#	"d:d$e(d%e(d&e(d'ee%d(e(d)e(dee
  10  e&e&ffd*d+�Z)	"	"	"		#	"d:d$e(d%e(d&e(d'ee%d(e(d)e(defd,d-�Z*de&de&dee&fd.d/�Z+de
  11  e$dffd0d1�Z,d&e(de
  12  e$ee$ffd2d3�Z-de
  13  e$dffd4d5�Z.d&e(de
  14  e$dffd6d7�Z/	"	#	"d;d'e%d%e(d&e(d(e(d)e(de
  15  e&e&e$ffd8d9�Z0dS)<�)�datetime)�timezone��wraps��split)�Any)�Optional)�Sequence)�Tuple)�Union)�current_app)�g)�request)�
  16  BadRequest)�config)�	CSRFError)�FreshTokenRequired)�InvalidHeaderError)�InvalidQueryParamError)�NoAuthorizationError)�UserLookupError)�custom_verification_for_token)�has_user_lookup)�user_lookup)�verify_token_not_blocklisted)�verify_token_type)�decode_token)�get_unverified_jwt_headersN�
  17  jwt_header�jwt_data�returncCsP|d}t|t�r|std||��dSt�t�tj��}||kr&td||��dS)N�freshzFresh token required)�
  18  isinstance�boolrr�	timestamp�nowr�utc)rr r"r&�r(�XC:\Users\shubh\Videos\agent\nova\Lib\site-packages\flask_jwt_extended/view_decorators.py�_verify_token_is_fresh"s
  19  ��r*FT�optionalr"�refresh�	locations�verify_type�skip_revocation_checkc	Cs�tjtjvrdSzt|||||d�\}}}Wnty1|s �it_it_ddit_	dt_
  20  YdSwt||�t_	|t_|t_|t_
  21  ||fS)aJ
  22      Verify that a valid JWT is present in the request, unless ``optional=True`` in
  23      which case no JWT is also considered valid.
  24  
  25      :param optional:
  26          If ``True``, do not raise an error if no JWT is present in the request.
  27          Defaults to ``False``.
  28  
  29      :param fresh:
  30          If ``True``, require a JWT marked as ``fresh`` in order to be verified.
  31          Defaults to ``False``.
  32  
  33      :param refresh:
  34          If ``True``, requires a refresh JWT to access this endpoint. If ``False``,
  35          requires an access JWT to access this endpoint. Defaults to ``False``
  36  
  37      :param locations:
  38          A location or list of locations to look for the JWT in this request, for
  39          example ``'headers'`` or ``['headers', 'cookies']``. Defaults to ``None``
  40          which indicates that JWTs will be looked for in the locations defined by the
  41          ``JWT_TOKEN_LOCATION`` configuration option.
  42  
  43      :param verify_type:
  44          If ``True``, the token type (access or refresh) will be checked according
  45          to the ``refresh`` argument. If ``False``, type will not be checked and both
  46          access and refresh tokens will be accepted.
  47  
  48      :param skip_revocation_check:
  49          If ``True``, revocation status of the token will be *not* checked. If ``False``,
  50          revocation status of the token will be checked.
  51  
  52      :return:
  53          A tuple containing the jwt_header and the jwt_data if a valid JWT is
  54          present in the request. If ``optional=True`` and no JWT is in the request,
  55          ``None`` will be returned instead. Raise an exception if an invalid JWT
  56          is in the request.
  57      N)r,r.r/�loaded_user)r�methodrZexempt_methods�_decode_jwt_from_requestrrZ_jwt_extended_jwtZ_jwt_extended_jwt_headerZ_jwt_extended_jwt_userZ_jwt_extended_jwt_location�
  58  _load_user)	r+r"r,r-r.r/r r�jwt_locationr(r(r)�verify_jwt_in_request-s0-�
  59  �r5cs������fdd�}|S)a�
  60      A decorator to protect a Flask endpoint with JSON Web Tokens.
  61  
  62      Any route decorated with this will require a valid JWT to be present in the
  63      request (unless optional=True, in which case no JWT is also valid) before the
  64      endpoint can be called.
  65  
  66      :param optional:
  67          If ``True``, allow the decorated endpoint to be accessed if no JWT is present in
  68          the request. Defaults to ``False``.
  69  
  70      :param fresh:
  71          If ``True``, require a JWT marked with ``fresh`` to be able to access this
  72          endpoint. Defaults to ``False``.
  73  
  74      :param refresh:
  75          If ``True``, requires a refresh JWT to access this endpoint. If ``False``,
  76          requires an access JWT to access this endpoint. Defaults to ``False``.
  77  
  78      :param locations:
  79          A location or list of locations to look for the JWT in this request, for
  80          example ``'headers'`` or ``['headers', 'cookies']``. Defaults to ``None``
  81          which indicates that JWTs will be looked for in the locations defined by the
  82          ``JWT_TOKEN_LOCATION`` configuration option.
  83  
  84      :param verify_type:
  85          If ``True``, the token type (access or refresh) will be checked according
  86          to the ``refresh`` argument. If ``False``, type will not be checked and both
  87          access and refresh tokens will be accepted.
  88  
  89      :param skip_revocation_check:
  90          If ``True``, revocation status of the token will be *not* checked. If ``False``,
  91          revocation status of the token will be checked.
  92      cs$t���������fdd��}|S)Ncs&t�������t���|i|��S�N)r5r
  93  Zensure_sync)�args�kwargs)�fnr"r-r+r,r/r.r(r)�	decorator�s�z0jwt_required.<locals>.wrapper.<locals>.decoratorr)r9r:�r"r-r+r,r/r.)r9r)�wrapper�szjwt_required.<locals>.wrapperr()r+r"r,r-r.r/r<r(r;r)�jwt_requiredys+
  94  r=cCsDt�sdS|tj}t||�}|durd�|�}t|||��d|iS)Nz user_lookup returned None for {}r0)rrZidentity_claim_keyr�formatr)rr �identity�user�	error_msgr(r(r)r3�s
  95  
  96  
  97  r3c	stj}tj�tj�|d����d�}|std|�d����ritd|�}�fdd�|D�}t	|�dkrEd	��d
  98  |�d|�d��d
  99  �	}t|��|d��}t	|�dkrad|�d|�d��d
 100  �}t
 101  |��|d}|dfS|��}t	|�dkr�d|�d|�d�}t
 102  |��|d}|dfS)N��,zMissing z Headerz,\s*cs g|]}|��d�kr|�qS)rr)�.0�s��header_typer(r)�
 103  <listcomp>�s z,_decode_jwt_from_headers.<locals>.<listcomp>��	Missing 'z' type in 'z' header. Expected 'z: z <JWT>'r�zBad z header. Expected 'z: <JWT>')r�header_namerGr�headers�get�striprr�lenr)rL�auth_headerZfield_valuesZjwt_headers�msg�parts�
 104  encoded_tokenr(rFr)�_decode_jwt_from_headers�sB
 105  ������	�rUcCs�|rtj}tj}tj}n	tj}tj}tj}tj�	|�}|s$t
 106  d�|���tjrJtj
 107  tjvrJtj�	|d�}|s@tjr@tj�	|d�}|sFtd��||fSd}||fS)NzMissing cookie "{}"zMissing CSRF token)rZrefresh_cookie_nameZrefresh_csrf_header_nameZrefresh_csrf_field_nameZaccess_cookie_nameZaccess_csrf_header_nameZaccess_csrf_field_namer�cookiesrNrr>Zcsrf_protectr1Zcsrf_request_methodsrMZcsrf_check_form�formr)r,Z
 108  cookie_keyZcsrf_header_keyZcsrf_field_keyrTZ
 109  csrf_valuer(r(r)�_decode_jwt_from_cookies�s&
 110  �rXcCsdtj}tj}tj�|�}|std|�d���|�|�s&td|�d|�d���|t	|�d�}|dfS)NrJz' query paramaterz#Invalid value for query parameter 'z%'. Expected the value to start with '�')
 111  rZquery_string_nameZquery_string_value_prefixrr7rNr�
 112  startswithrrP)�
 113  param_name�prefix�valuerTr(r(r)�_decode_jwt_from_query_strings
 114  ��r^cCshtjstd��|r
 115  tj}ntj}ztjotj�|d�}|s t��W|dfSty3td�	|��d�w)Nz/Invalid content-type. Must be application/json.zMissing "{}" key in json data.)
 116  rZis_jsonrrZrefresh_json_keyZjson_key�jsonrNrr>)r,Z	token_keyrTr(r(r)�_decode_jwt_from_jsons$�����r`cs�t|t�r|g}|s
 117  tj}g}|D]A}|dkr#|�|�fdd�f�q|dkr/|�|tf�q|dkr;|�|tf�q|dkrK|�|�fdd�f�qtd|�d	���g}d}|D]0\}}	z|	�\}
 118  }t|
 119  |�}|}t	|
 120  �}
 121  Wnt
 122  y�}z
 123  |�t|��WYd}~qYd}~ww|s�t|�d
 124  kr�djd�
 125  |dd
 126  ��|d
 127  d�
 128  |�d�}t
 129  |��t
 130  |d��|r�t|��|r�t|
 131  |�|s�t|
 132  |�t|
 133  |�||
 134  |fS)NrVc�t��Sr6)rXr(�r,r(r)�<lambda>>�z*_decode_jwt_from_request.<locals>.<lambda>Zquery_stringrMr_crar6)r`r(rbr(r)rcHrdrYz' is not a valid locationrIz5Missing JWT in {start_locs} or {end_locs} ({details})z, �����z; )Z
 135  start_locsZend_locs�detailsr)r#�strrZtoken_location�appendr^rU�RuntimeErrorrrrrPr>�joinrr*rr)r-r"r,r.r/Zget_encoded_token_functions�location�errorsZ
 136  decoded_tokenZget_encoded_token_functionrTZ
 137  csrf_tokenr4r�e�err_msgr(rbr)r2*sf
 138  ���
 139  
 140  ���
 141  
 142  
 143  
 144  
 145  r2)FFFNTF)FTF)1rr�	functoolsr�rer�typingrr	r
 146  rrZflaskr
 147  rrZwerkzeug.exceptionsrZflask_jwt_extended.configrZflask_jwt_extended.exceptionsrrrrrrZ!flask_jwt_extended.internal_utilsrrrrrZflask_jwt_extended.utilsrrrgZLocationType�dictr*r$r5r=r3rUrXr^r`r2r(r(r(r)�<module>s��������
 148  �M�������
 149  �8,�������