U )._8@sdZddlZddlZddlmZddlmZddlmZddlmZddl m Z dd l m Z dd l m Z dd lmZGd d d e jZGdddeeZGdddeZGdddeZeZGdddeZdS)z flask.sessions ~~~~~~~~~~~~~~ Implements cookie based sessions based on itsdangerous. :copyright: 2010 Pallets :license: BSD-3-Clause N)datetime) BadSignature)URLSafeTimedSerializer) CallbackDict)collections_abc)is_ip) total_seconds)TaggedJSONSerializerc@s6eZdZdZeddZejddZdZdZdZ dS) SessionMixinz3Expands a basic dictionary with session attributes.cCs |ddS)z3This reflects the ``'_permanent'`` key in the dict. _permanentF)getselfrC:\Users\Miouzora\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask/sessions.py permanentszSessionMixin.permanentcCst||d<dS)Nr )bool)rvaluerrrr!sFTN) __name__ __module__ __qualname____doc__propertyrsetternewmodifiedaccessedrrrrr s  r csReZdZdZdZdZd fdd ZfddZd fdd Zdfd 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``. FNcsdd}tt|||dS)NcSsd|_d|_dSNT)rrrrrr on_updateLsz/SecureCookieSession.__init__..on_update)superr__init__)rinitialr  __class__rrr"KszSecureCookieSession.__init__csd|_tt||Sr)rr!r __getitem__)rkeyr$rrr&RszSecureCookieSession.__getitem__csd|_tt|||Sr)rr!rr rr'defaultr$rrr VszSecureCookieSession.getcsd|_tt|||Sr)rr!r setdefaultr(r$rrr*ZszSecureCookieSession.setdefault)N)N)N) rrrrrrr"r&r r* __classcell__rrr$rr5s  rc@s6eZdZdZddZeZZZZZ Z Z [dS) 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. cOs tddS)NzThe session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.) RuntimeError)rargskwargsrrr_faileszNullSession._failN) rrrrr0 __setitem__ __delitem__clearpoppopitemupdater*rrrrr,_sr,c@speZdZdZeZdZddZddZddZ d d Z d d Z d dZ ddZ ddZddZddZddZdS)SessionInterfaceaThe 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() .. versionadded:: 0.8 FcCs|S)aCreates 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_classrapprrrmake_null_sessions z"SessionInterface.make_null_sessioncCs t||jS)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. ) isinstancer8)robjrrris_null_sessionsz SessionInterface.is_null_sessioncCs|jd}|dk r|r|SdS|jd}|s:d|jd<dS|dddd}d|krxtd j|d d|jd<dSt|}|rtd ||d kr|sd|}||jd<|S) aMReturns the domain that should be set for the session cookie. Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise falls back to detecting the domain based on ``SERVER_NAME``. Once detected (or if not set at all), ``SESSION_COOKIE_DOMAIN`` is updated to avoid re-running the logic. ZSESSION_COOKIE_DOMAINNZ SERVER_NAMEF:rr.z"{rv}" is not a valid cookie domain, it must contain a ".". Add an entry to your hosts file, for example "{rv}.localdomain", and use that instead.)rvzThe session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead./)configrsplitlstripwarningswarnformatrget_cookie_path)rr:rAiprrrget_cookie_domains2       z"SessionInterface.get_cookie_domaincCs|jdp|jdS)aReturns 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``. ZSESSION_COOKIE_PATHZAPPLICATION_ROOTrCr9rrrrIsz SessionInterface.get_cookie_pathcCs |jdS)zReturns True if the session cookie should be httponly. This currently just returns the value of the ``SESSION_COOKIE_HTTPONLY`` config var. ZSESSION_COOKIE_HTTPONLYrLr9rrrget_cookie_httponlysz$SessionInterface.get_cookie_httponlycCs |jdS)zReturns True if the cookie should be secure. This currently just returns the value of the ``SESSION_COOKIE_SECURE`` setting. ZSESSION_COOKIE_SECURErLr9rrrget_cookie_securesz"SessionInterface.get_cookie_securecCs |jdS)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. ZSESSION_COOKIE_SAMESITErLr9rrrget_cookie_samesitesz$SessionInterface.get_cookie_samesitecCs|jrt|jSdS)aA 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)rrutcnowpermanent_session_lifetimerr:sessionrrrget_expiration_timesz$SessionInterface.get_expiration_timecCs|jp|jo|jdS)aUsed 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 ZSESSION_REFRESH_EACH_REQUEST)rrrCrRrrrshould_set_cookies z"SessionInterface.should_set_cookiecCs tdS)a%This method has to be implemented and must either return ``None`` in case the loading failed because of a configuration error or an instance of a session object which implements a dictionary like interface + the methods and attributes on :class:`SessionMixin`. NNotImplementedError)rr:requestrrr open_sessionszSessionInterface.open_sessioncCs tdS)zThis is called for actual sessions returned by :meth:`open_session` at the end of the request. This is still called during a request context so if you absolutely need access to the request you can do that. NrV)rr:rSresponserrr save_session'szSessionInterface.save_sessionN)rrrrr,r8Z pickle_basedr;r>rKrIrMrNrOrTrUrYr[rrrrr7ps!  : r7c@sBeZdZdZdZeejZdZ e Z e Z ddZddZdd Zd S) SecureCookieSessionInterfacezuThe default session interface that stores sessions in signed cookies through the :mod:`itsdangerous` module. zcookie-sessionhmaccCs0|js dSt|j|jd}t|j|j|j|dS)N)key_derivation digest_method)salt serializer signer_kwargs)Z secret_keydictr^r_rr`ra)rr:rbrrrget_signing_serializerFsz3SecureCookieSessionInterface.get_signing_serializercCsv||}|dkrdS|j|j}|s0|St|j}z|j||d}||WStk rp|YSXdS)N)max_age) rdcookiesr session_cookie_name session_classr rQloadsr)rr:rXsvalredatarrrrYSs   z)SecureCookieSessionInterface.open_sessionc Cs||}||}|s4|jr0|j|j||ddS|jrF|jd|||sVdS| |}| |}| |}| ||} | |t|} |j|j| | |||||ddS)N)domainpathCookie)expireshttponlyrmrnsecuresamesite)rKrIrZ delete_cookiergrvaryaddrUrMrNrOrTrddumpsrc set_cookie) rr:rSrZrmrnrqrrrsrprkrrrr[as:        z)SecureCookieSessionInterface.save_sessionN)rrrrr` staticmethodhashlibsha1r_r^session_json_serializerrarrhrdrYr[rrrrr\3s  r\)rryrFrZ itsdangerousrrZwerkzeug.datastructuresr_compatrZhelpersrr Zjson.tagr MutableMappingr rr,objectr7r{r\rrrrs"         *A