o
    q>eY7                     @  s   d dl mZ d dlZd dl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jrId	dlmZ d	dlmZmZ G dd deZG dd deeZG dd deZG dd dZe ZG dd deZdS )    )annotationsN)MutableMapping)datetime)timezone)BadSignature)URLSafeTimedSerializer)CallbackDict   )TaggedJSONSerializer)Flask)RequestResponsec                   @  s:   e Zd ZdZedddZejdddZd	Zd
Zd
Z	dS )SessionMixinz3Expands a basic dictionary with session attributes.returnboolc                 C  s   |  ddS )z3This reflects the ``'_permanent'`` key in the dict.
_permanentF)getself r   O/var/www/bmteknikk.ddns.net/venv/lib/python3.10/site-packages/flask/sessions.py	permanent   s   zSessionMixin.permanentvalueNonec                 C  s   t || d< d S )Nr   )r   )r   r   r   r   r   r      s   FTN)r   r   )r   r   r   r   )
__name__
__module____qualname____doc__propertyr   setternewmodifiedaccessedr   r   r   r   r      s    r   c                      sZ   e Zd ZdZdZdZdd fdd	Zd fddZdd fddZdd fddZ	  Z
S )SecureCookieSessiona	  Base class for sessions based on signed cookies.

    This session backend will set the :attr:`modified` and
    :attr:`accessed` attributes. It cannot reliably track whether a
    session is new (vs. empty), so :attr:`new` remains hard coded to
    ``False``.
    FNinitialt.Anyr   r   c                   s   ddd}t  || d S )Nr   r   c                 S  s   d| _ d| _d S NT)r!   r"   r   r   r   r   	on_updateG   s   
z/SecureCookieSession.__init__.<locals>.on_update)r   r   )super__init__)r   r$   r'   	__class__r   r   r)   F   s   
zSecureCookieSession.__init__keystrc                   s   d| _ t |S r&   )r"   r(   __getitem__)r   r,   r*   r   r   r.   M   s   zSecureCookieSession.__getitem__defaultc                      d| _ t ||S r&   )r"   r(   r   r   r,   r/   r*   r   r   r   Q      zSecureCookieSession.getc                   r0   r&   )r"   r(   
setdefaultr1   r*   r   r   r3   U   r2   zSecureCookieSession.setdefault)N)r$   r%   r   r   )r,   r-   r   r%   )r,   r-   r/   r%   r   r%   )r   r   r   r   r!   r"   r)   r.   r   r3   __classcell__r   r   r*   r   r#   0   s    r#   c                   @  s8   e Zd ZdZd
ddZe Z Z Z Z Z	 Z
Z[d	S )NullSessionzClass used to generate nicer error messages if sessions are not
    available.  Will still allow read-only access to the empty session
    but fail on setting.
    argsr%   kwargsr   
t.NoReturnc                 O  s   t d)NzThe session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.)RuntimeError)r   r6   r7   r   r   r   _fail`   s   zNullSession._failN)r6   r%   r7   r%   r   r8   )r   r   r   r   r:   __setitem____delitem__clearpoppopitemupdater3   r   r   r   r   r5   Z   s
    
r5   c                   @  s   e Zd ZdZeZdZd.ddZd/ddZd0ddZ	d1ddZ
d0ddZd2ddZd2ddZd0ddZd3dd Zd4d!d"Zd5d&d'Zd6d+d,Zd-S )7SessionInterfacea4  The basic interface you have to implement in order to replace the
    default session interface which uses werkzeug's securecookie
    implementation.  The only methods you have to implement are
    :meth:`open_session` and :meth:`save_session`, the others have
    useful defaults which you don't need to change.

    The session object returned by the :meth:`open_session` method has to
    provide a dictionary like interface plus the properties and methods
    from the :class:`SessionMixin`.  We recommend just subclassing a dict
    and adding that mixin::

        class Session(dict, SessionMixin):
            pass

    If :meth:`open_session` returns ``None`` Flask will call into
    :meth:`make_null_session` to create a session that acts as replacement
    if the session support cannot work because some requirement is not
    fulfilled.  The default :class:`NullSession` class that is created
    will complain that the secret key was not set.

    To replace the session interface on an application all you have to do
    is to assign :attr:`flask.Flask.session_interface`::

        app = Flask(__name__)
        app.session_interface = MySessionInterface()

    Multiple requests with the same session may be sent and handled
    concurrently. When implementing a new session interface, consider
    whether reads or writes to the backing store must be synchronized.
    There is no guarantee on the order in which the session for each
    request is opened or saved, it will occur in the order that requests
    begin and end processing.

    .. versionadded:: 0.8
    Fappr   r   r5   c                 C  s   |   S )a  Creates a null session which acts as a replacement object if the
        real session support could not be loaded due to a configuration
        error.  This mainly aids the user experience because the job of the
        null session is to still support lookup without complaining but
        modifications are answered with a helpful error message of what
        failed.

        This creates an instance of :attr:`null_session_class` by default.
        )null_session_classr   rB   r   r   r   make_null_session   s   
z"SessionInterface.make_null_sessionobjobjectr   c                 C  s   t || jS )zChecks if a given object is a null session.  Null sessions are
        not asked to be saved.

        This checks if the object is an instance of :attr:`null_session_class`
        by default.
        )
isinstancerC   )r   rF   r   r   r   is_null_session   s   z SessionInterface.is_null_sessionr-   c                 C  
   |j d S )zJThe name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``.SESSION_COOKIE_NAMEconfigrD   r   r   r   get_cookie_name   s   
z SessionInterface.get_cookie_name
str | Nonec                 C  s   |j d }|r	|S dS )a  The value of the ``Domain`` parameter on the session cookie. If not set,
        browsers will only send the cookie to the exact domain it was set from.
        Otherwise, they will send it to any subdomain of the given value as well.

        Uses the :data:`SESSION_COOKIE_DOMAIN` config.

        .. versionchanged:: 2.3
            Not set by default, does not fall back to ``SERVER_NAME``.
        SESSION_COOKIE_DOMAINNrL   )r   rB   rvr   r   r   get_cookie_domain   s   

z"SessionInterface.get_cookie_domainc                 C  s   |j d p	|j d S )a  Returns the path for which the cookie should be valid.  The
        default implementation uses the value from the ``SESSION_COOKIE_PATH``
        config var if it's set, and falls back to ``APPLICATION_ROOT`` or
        uses ``/`` if it's ``None``.
        SESSION_COOKIE_PATHAPPLICATION_ROOTrL   rD   r   r   r   get_cookie_path   s   z SessionInterface.get_cookie_pathc                 C  rJ   )zReturns True if the session cookie should be httponly.  This
        currently just returns the value of the ``SESSION_COOKIE_HTTPONLY``
        config var.
        SESSION_COOKIE_HTTPONLYrL   rD   r   r   r   get_cookie_httponly      
z$SessionInterface.get_cookie_httponlyc                 C  rJ   )zReturns True if the cookie should be secure.  This currently
        just returns the value of the ``SESSION_COOKIE_SECURE`` setting.
        SESSION_COOKIE_SECURErL   rD   r   r   r   get_cookie_secure   s   
z"SessionInterface.get_cookie_securec                 C  rJ   )zReturn ``'Strict'`` or ``'Lax'`` if the cookie should use the
        ``SameSite`` attribute. This currently just returns the value of
        the :data:`SESSION_COOKIE_SAMESITE` setting.
        SESSION_COOKIE_SAMESITErL   rD   r   r   r   get_cookie_samesite   rX   z$SessionInterface.get_cookie_samesitesessionr   datetime | Nonec                 C  s   |j rttj|j S dS )a  A helper method that returns an expiration date for the session
        or ``None`` if the session is linked to the browser session.  The
        default implementation returns now + the permanent session
        lifetime configured on the application.
        N)r   r   nowr   utcpermanent_session_lifetimer   rB   r]   r   r   r   get_expiration_time   s   z$SessionInterface.get_expiration_timec                 C  s   |j p
|jo
|jd S )a  Used by session backends to determine if a ``Set-Cookie`` header
        should be set for this session cookie for this response. If the session
        has been modified, the cookie is set. If the session is permanent and
        the ``SESSION_REFRESH_EACH_REQUEST`` config is true, the cookie is
        always set.

        This check is usually skipped if the session was deleted.

        .. versionadded:: 0.11
        SESSION_REFRESH_EACH_REQUEST)r!   r   rM   rb   r   r   r   should_set_cookie   s   z"SessionInterface.should_set_cookierequestr   SessionMixin | Nonec                 C     t  )a  This is called at the beginning of each request, after
        pushing the request context, before matching the URL.

        This must return an object which implements a dictionary-like
        interface as well as the :class:`SessionMixin` interface.

        This will return ``None`` to indicate that loading failed in
        some way that is not immediately an error. The request
        context will fall back to using :meth:`make_null_session`
        in this case.
        NotImplementedError)r   rB   rf   r   r   r   open_session   s   zSessionInterface.open_sessionresponser   r   c                 C  rh   )zThis is called at the end of each request, after generating
        a response, before removing the request context. It is skipped
        if :meth:`is_null_session` returns ``True``.
        ri   )r   rB   r]   rl   r   r   r   save_session  s   zSessionInterface.save_sessionN)rB   r   r   r5   )rF   rG   r   r   )rB   r   r   r-   )rB   r   r   rO   )rB   r   r   r   )rB   r   r]   r   r   r^   )rB   r   r]   r   r   r   )rB   r   rf   r   r   rg   rB   r   r]   r   rl   r   r   r   )r   r   r   r   r5   rC   pickle_basedrE   rI   rN   rR   rU   rW   rZ   r\   rc   re   rk   rm   r   r   r   r   rA   k   s     (


	








rA   c                   @  sH   e Zd ZdZdZeejZdZ	e
ZeZddd	ZdddZdddZdS )SecureCookieSessionInterfacezuThe default session interface that stores sessions in signed cookies
    through the :mod:`itsdangerous` module.
    zcookie-sessionhmacrB   r   r   URLSafeTimedSerializer | Nonec                 C  s0   |j sd S t| j| jd}t|j | j| j|dS )N)key_derivationdigest_method)salt
serializersigner_kwargs)
secret_keydictrs   rt   r   ru   rv   )r   rB   rw   r   r   r   get_signing_serializer'  s   z3SecureCookieSessionInterface.get_signing_serializerrf   r   SecureCookieSession | Nonec                 C  sx   |  |}|d u rd S |j| |}|s|  S t|j }z|j||d}| |W S  t	y;   |   Y S w )N)max_age)
rz   cookiesr   rN   session_classintra   total_secondsloadsr   )r   rB   rf   svalr|   datar   r   r   rk   4  s   
z)SecureCookieSessionInterface.open_sessionr]   r   rl   r   r   c              
   C  s   |  |}| |}| |}| |}| |}| |}	|jr'|jd |s?|j	r=|j
||||||	d |jd d S | ||sGd S | ||}
| |t|}|j|||
|	||||d |jd d S )NCookie)domainpathsecuresamesitehttponly)expiresr   r   r   r   r   )rN   rR   rU   rZ   r\   rW   r"   varyaddr!   delete_cookiere   rc   rz   dumpsry   
set_cookie)r   rB   r]   rl   namer   r   r   r   r   r   r   r   r   r   rm   B  sF   






z)SecureCookieSessionInterface.save_sessionN)rB   r   r   rr   )rB   r   rf   r   r   r{   rn   )r   r   r   r   ru   staticmethodhashlibsha1rt   rs   session_json_serializerrv   r#   r~   rz   rk   rm   r   r   r   r   rp     s    


rp   )
__future__r   r   typingtcollections.abcr   r   r   itsdangerousr   r   werkzeug.datastructuresr   json.tagr
   TYPE_CHECKINGrB   r   wrappersr   r   r   r#   r5   rA   r   rp   r   r   r   r   <module>   s(    * '