o 3a@sddlZddlZddlmZddlmZmZddlmZddl m Z ddl m Z m Z ddlmZmZGdd d ejZGd d d ejZGd d d ZGdddeZdS)N)settings) BaseStorageMessage)signing) SimpleCookie)constant_time_compare salted_hmac)SafeData mark_safecs$eZdZdZdZfddZZS)MessageEncoderzI Compactly serialize instances of the ``Message`` class as JSON. __json_messagecsRt|tr#t|jtr dnd}|j||j|jg}|jr!||j|St |S)Nr) isinstancermessager message_keylevel extra_tagsappendsuperdefault)selfobj is_safedatar __class__H/usr/lib/python3/dist-packages/django/contrib/messages/storage/cookie.pyrs   zMessageEncoder.default)__name__ __module__ __qualname____doc__rr __classcell__rrrrr sr cs(eZdZdZddZfddZZS)MessageDecoderzE Decode JSON that includes serialized ``Message`` instances. cszt|tr+|r+|dtjkr"|drt|d|d<t|ddSfdd|DSt|tr;fdd|DS|S) Nrr csg|]}|qSrprocess_messages).0itemrrr (sz3MessageDecoder.process_messages..csi|] \}}||qSrr%)r'keyvaluer)rr *sz3MessageDecoder.process_messages..)rlistr rr rdictitemsrrrr)rr&"s  zMessageDecoder.process_messagesc stj|fi|}||S)N)rdecoder&)rskwargsdecodedrrrr2.s zMessageDecoder.decode)rrrr r&r2r!rrrrr"s r"c@seZdZddZddZdS)MessageSerializercCstj|dtddS)N),:) separatorsclslatin-1)jsondumpsr encoder1rrrr=4szMessageSerializer.dumpscCstj|dtdS)Nr;r:)r<loadsr2r")rdatarrrr@;szMessageSerializer.loadsN)rrrr=r@rrrrr63s r6csleZdZdZdZdZdZdZfddZdd Z d d Z dd dZ ddZ dddZ ddZddZZS) CookieStoragez% Store messages in a cookie. messagesi__messagesnotfinished__django.contrib.messagescs&tj|i|tj|jd|_dS)N)salt)r__init__rget_cookie_signerkey_saltsigner)rargsr4rrrrGKszCookieStorage.__init__cOsF|jj|j}||}|o|d|jk }|r|s|||fS)a  Retrieve a list of messages from the messages cookie. If the not_finished sentinel value is found at the end of the message list, remove it and return a result indicating that not all messages were retrieved by this storage. )requestCOOKIESget cookie_name_decode not_finishedpop)rrKr4rArC all_retrievedrrr_getOs  zCookieStorage._getcCsJ|r|j|j|tjtjp dtjpdtjddS|j|jtjtjddS)zz Either set the cookie with the encoded data if there is any data to store, or delete the cookie. N)domainsecurehttponlysamesite)rVrY) set_cookierPrSESSION_COOKIE_DOMAINSESSION_COOKIE_SECURESESSION_COOKIE_HTTPONLYSESSION_COOKIE_SAMESITE delete_cookie)r encoded_dataresponserrr_update_cookie^s  zCookieStorage._update_cookieTc sg}||}|jrCtfdd}|rC|||jkrC|r'||dn|d||j||jg|d}|rC|||jks||||S)aT Store the messages to a cookie and return a list of any messages which could not be stored. If the encoded data is larger than ``max_cookie_size``, remove messages until the data fits (these are the messages which are returned), and add the not_finished sentinel value to indicate as much. cst|dS)Nr )len value_encode)valcookierr stored_lengthsz+CookieStorage._store..stored_lengthr) encode_empty)_encodemax_cookie_sizerrrSinsertrRrb) rrCra remove_oldestrKr4unstored_messagesr`rhrrfr_storers   zCookieStorage._storecCsd}t||S)z # RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid. Create an HMAC/SHA1 hash based on the value and the project setting's SECRET_KEY, modified to make it unique for the present purpose. rE)r hexdigest)rr,rIrrr _legacy_hashs zCookieStorage._legacy_hashFcCs|s|r |jj|tddSdS)a Return an encoded version of the messages list which can be stored as plain text. Since the data will be retrieved from the client-side, the encoded data also contains a hash to ensure that the data was not tampered with. T) serializercompressN)rJ sign_objectr6)rrCrirrrrjszCookieStorage._encodec Cs|sdSz |jj|tdWStjy||}Yntjtj fy.|j |}Ynw|rDztj |t dWStj yCYnwd|_ dS)z Safely decode an encoded text stream back into a list of messages. If the encoded text stream contained an invalid hash or was in an invalid format, return None. N)rrr?T)rJ unsign_objectr6r BadSignature_legacy_decodebinasciiErrorr<JSONDecodeErrorunsignr@r"used)rrAr5rrrrQs"zCookieStorage._decodecCs8|dd}t|dkr|\}}t|||r|SdS)N$r r$)splitrcrrq)rrAbitshash_r,rrrrws  zCookieStorage._legacy_decode)T)F)rrrr rPrkrRrIrGrUrbrorqrjrQrwr!rrrrrB?s    "rB)rxr< django.confr$django.contrib.messages.storage.baserr django.corer django.httprdjango.utils.cryptorrdjango.utils.safestringr r JSONEncoderr JSONDecoderr"r6rBrrrrs