o
    G'h5  ã                   @   sš  d dl m Z  d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl!m#Z# ee$e	df Z%de&de&ddfd d!„Z'	"	"	"		#	"d:d$e(d%e(d&e(d'ee% d(e(d)e(dee
e&e&f  fd*d+„Z)	"	"	"		#	"d:d$e(d%e(d&e(d'ee% d(e(d)e(defd,d-„Z*de&de&dee& fd.d/„Z+de
e$df fd0d1„Z,d&e(de
e$ee$ f fd2d3„Z-de
e$df fd4d5„Z.d&e(de
e$df fd6d7„Z/	"	#	"d;d'e%d%e(d&e(d(e(d)e(de
e&e&e$f fd8d9„Z0dS )<é    )Údatetime)Útimezone©Úwraps©Úsplit)ÚAny)ÚOptional)ÚSequence)ÚTuple)ÚUnion)Úcurrent_app)Úg)Úrequest)Ú
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Ú
jwt_headerÚjwt_dataÚreturnc                 C   sP   |d }t |tƒr|std| |ƒ‚d S t t tj¡¡}||k r&td| |ƒ‚d S )NÚfreshzFresh token required)Ú
isinstanceÚboolr   r   Ú	timestampÚnowr   Úutc)r   r    r"   r&   © r(   úXC:\Users\shubh\Videos\agent\nova\Lib\site-packages\flask_jwt_extended/view_decorators.pyÚ_verify_token_is_fresh"   s   
ÿÿr*   FTÚoptionalr"   ÚrefreshÚ	locationsÚverify_typeÚskip_revocation_checkc           	      C   sŠ   t jtjv rdS zt|||||d�\}}}W n ty1   | s ‚ i t_i t_ddit_	dt_
Y dS w t||ƒt_	|t_|t_|t_
||fS )aJ  
    Verify that a valid JWT is present in the request, unless ``optional=True`` in
    which case no JWT is also considered valid.

    :param optional:
        If ``True``, do not raise an error if no JWT is present in the request.
        Defaults to ``False``.

    :param fresh:
        If ``True``, require a JWT marked as ``fresh`` in order to be verified.
        Defaults to ``False``.

    :param refresh:
        If ``True``, requires a refresh JWT to access this endpoint. If ``False``,
        requires an access JWT to access this endpoint. Defaults to ``False``

    :param locations:
        A location or list of locations to look for the JWT in this request, for
        example ``'headers'`` or ``['headers', 'cookies']``. Defaults to ``None``
        which indicates that JWTs will be looked for in the locations defined by the
        ``JWT_TOKEN_LOCATION`` configuration option.

    :param verify_type:
        If ``True``, the token type (access or refresh) will be checked according
        to the ``refresh`` argument. If ``False``, type will not be checked and both
        access and refresh tokens will be accepted.

    :param skip_revocation_check:
        If ``True``, revocation status of the token will be *not* checked. If ``False``,
        revocation status of the token will be checked.

    :return:
        A tuple containing the jwt_header and the jwt_data if a valid JWT is
        present in the request. If ``optional=True`` and no JWT is in the request,
        ``None`` will be returned instead. Raise an exception if an invalid JWT
        is in the request.
    N)r,   r.   r/   Úloaded_user)r   Úmethodr   Zexempt_methodsÚ_decode_jwt_from_requestr   r   Z_jwt_extended_jwtZ_jwt_extended_jwt_headerZ_jwt_extended_jwt_userZ_jwt_extended_jwt_locationÚ
_load_user)	r+   r"   r,   r-   r.   r/   r    r   Újwt_locationr(   r(   r)   Úverify_jwt_in_request-   s0   -û
ùr5   c                    s   ‡ ‡‡‡‡‡fdd„}|S )aÓ  
    A decorator to protect a Flask endpoint with JSON Web Tokens.

    Any route decorated with this will require a valid JWT to be present in the
    request (unless optional=True, in which case no JWT is also valid) before the
    endpoint can be called.

    :param optional:
        If ``True``, allow the decorated endpoint to be accessed if no JWT is present in
        the request. Defaults to ``False``.

    :param fresh:
        If ``True``, require a JWT marked with ``fresh`` to be able to access this
        endpoint. Defaults to ``False``.

    :param refresh:
        If ``True``, requires a refresh JWT to access this endpoint. If ``False``,
        requires an access JWT to access this endpoint. Defaults to ``False``.

    :param locations:
        A location or list of locations to look for the JWT in this request, for
        example ``'headers'`` or ``['headers', 'cookies']``. Defaults to ``None``
        which indicates that JWTs will be looked for in the locations defined by the
        ``JWT_TOKEN_LOCATION`` configuration option.

    :param verify_type:
        If ``True``, the token type (access or refresh) will be checked according
        to the ``refresh`` argument. If ``False``, type will not be checked and both
        access and refresh tokens will be accepted.

    :param skip_revocation_check:
        If ``True``, revocation status of the token will be *not* checked. If ``False``,
        revocation status of the token will be checked.
    c                    s$   t ˆ ƒ‡ ‡‡‡‡‡‡fdd„ƒ}|S )Nc                     s&   t ˆˆˆˆˆˆƒ t ˆ ¡| i |¤ŽS ©N)r5   r   Zensure_sync)ÚargsÚkwargs)Úfnr"   r-   r+   r,   r/   r.   r(   r)   Ú	decorator¥   s   ÿz0jwt_required.<locals>.wrapper.<locals>.decoratorr   )r9   r:   ©r"   r-   r+   r,   r/   r.   )r9   r)   Úwrapper¤   s   zjwt_required.<locals>.wrapperr(   )r+   r"   r,   r-   r.   r/   r<   r(   r;   r)   Újwt_requiredy   s   +
r=   c                 C   sD   t ƒ sd S |tj }t| |ƒ}|d u rd |¡}t|| |ƒ‚d|iS )Nz user_lookup returned None for {}r0   )r   r   Zidentity_claim_keyr   Úformatr   )r   r    ÚidentityÚuserÚ	error_msgr(   r(   r)   r3   ±   s   


r3   c               	      s  t j} t j‰ tj | d¡ ¡  d¡}|std| › d�ƒ‚ˆ ritd|ƒ}‡ fdd„|D ƒ}t	|ƒdkrEd	ˆ › d
| › d| › dˆ › d�	}t|ƒ‚|d  ¡ }t	|ƒdkrad| › d| › dˆ › d�}t
|ƒ‚|d }|d fS | ¡ }t	|ƒdkr€d| › d| › d�}t
|ƒ‚|d }|d fS )NÚ ú,zMissing z Headerz,\s*c                    s    g | ]}|  ¡ d  ˆ kr|‘qS )r   r   )Ú.0Ús©Úheader_typer(   r)   Ú
<listcomp>Í   s     z,_decode_jwt_from_headers.<locals>.<listcomp>é   ú	Missing 'z' type in 'z' header. Expected 'z: z <JWT>'r   é   zBad z header. Expected 'z: <JWT>')r   Úheader_namerG   r   ÚheadersÚgetÚstripr   r   Úlenr   )rL   Úauth_headerZfield_valuesZjwt_headersÚmsgÚpartsÚencoded_tokenr(   rF   r)   Ú_decode_jwt_from_headers½   sB   
ÿÿÿÿÿÿ	ùrU   c                 C   s    | rt j}t j}t j}n	t j}t j}t j}tj 	|¡}|s$t
d |¡ƒ‚t jrJtjt jv rJtj 	|d ¡}|s@t jr@tj 	|d ¡}|sFtdƒ‚||fS d }||fS )NzMissing cookie "{}"zMissing CSRF token)r   Zrefresh_cookie_nameZrefresh_csrf_header_nameZrefresh_csrf_field_nameZaccess_cookie_nameZaccess_csrf_header_nameZaccess_csrf_field_namer   ÚcookiesrN   r   r>   Zcsrf_protectr1   Zcsrf_request_methodsrM   Zcsrf_check_formÚformr   )r,   Z
cookie_keyZcsrf_header_keyZcsrf_field_keyrT   Z
csrf_valuer(   r(   r)   Ú_decode_jwt_from_cookiesé   s&   
þrX   c                  C   sd   t j} t j}tj | ¡}|std| › d�ƒ‚| |¡s&td| › d|› d�ƒ‚|t	|ƒd … }|d fS )NrJ   z' query paramaterz#Invalid value for query parameter 'z%'. Expected the value to start with 'ú')
r   Zquery_string_nameZquery_string_value_prefixr   r7   rN   r   Ú
startswithr   rP   )Ú
param_nameÚprefixÚvaluerT   r(   r(   r)   Ú_decode_jwt_from_query_string  s   
ÿÿr^   c                 C   sh   t jstdƒ‚| rtj}ntj}zt jot j |d ¡}|s tƒ ‚W |d fS  ty3   td 	|¡ƒd ‚w )Nz/Invalid content-type. Must be application/json.zMissing "{}" key in json data.)
r   Zis_jsonr   r   Zrefresh_json_keyZjson_keyÚjsonrN   r   r>   )r,   Z	token_keyrT   r(   r(   r)   Ú_decode_jwt_from_json  s$   ÿûÿþÿr`   c                    sž  t | tƒr| g} | stj} g }| D ]A}|dkr#| |‡ fdd„f¡ q|dkr/| |tf¡ q|dkr;| |tf¡ q|dkrK| |‡ fdd„f¡ qtd|› d	�ƒ‚g }d }|D ]0\}}	z|	ƒ \}
}t|
|ƒ}|}t	|
ƒ}W  n t
y‰ } z| t|ƒ¡ W Y d }~qYd }~ww |s°t| ƒd
krªdjd | d d… ¡| d d |¡d�}t
|ƒ‚t
|d ƒ‚|r·t|ˆ ƒ |r¾t||ƒ |sÅt||ƒ t||ƒ |||fS )NrV   c                      ó   t ˆ ƒS r6   )rX   r(   ©r,   r(   r)   Ú<lambda>>  ó    z*_decode_jwt_from_request.<locals>.<lambda>Zquery_stringrM   r_   c                      ra   r6   )r`   r(   rb   r(   r)   rc   H  rd   rY   z' is not a valid locationrI   z5Missing JWT in {start_locs} or {end_locs} ({details})z, éÿÿÿÿz; )Z
start_locsZend_locsÚdetailsr   )r#   Ústrr   Ztoken_locationÚappendr^   rU   ÚRuntimeErrorr   r   r   rP   r>   Újoinr   r*   r   r   )r-   r"   r,   r.   r/   Zget_encoded_token_functionsÚlocationÚerrorsZdecoded_tokenZget_encoded_token_functionrT   Z
csrf_tokenr4   r   ÚeÚerr_msgr(   rb   r)   r2   *  sf   
ÿÿÿ

€ÿý




r2   )FFFNTF)FTF)1r   r   Ú	functoolsr   Úrer   Útypingr   r	   r
   r   r   Zflaskr   r   r   Zwerkzeug.exceptionsr   Zflask_jwt_extended.configr   Zflask_jwt_extended.exceptionsr   r   r   r   r   r   Z!flask_jwt_extended.internal_utilsr   r   r   r   r   Zflask_jwt_extended.utilsr   r   rg   ZLocationTypeÚdictr*   r$   r5   r=   r3   rU   rX   r^   r`   r2   r(   r(   r(   r)   Ú<module>   s¸    úÿþýüûú
ùMúÿþýüûú
ù8,ûÿþýüûú