File manager - Edit - /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/securecookie.pyo
Back
� �[�Rc @ s� d 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 m Z d d l m Z m Z d d l m Z d d l m Z d d l m Z d d l m Z d e f d � � YZ d e f d � � YZ d S( s: werkzeug.contrib.securecookie ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This module implements a cookie that is not alterable from the client because it adds a checksum the server checks for. You can use it as session replacement if all you have is a user id or something to mark a logged in user. Keep in mind that the data is still readable from the client as a normal cookie is. However you don't have to store and flush the sessions you have at the server. Example usage: >>> from werkzeug.contrib.securecookie import SecureCookie >>> x = SecureCookie({"foo": 42, "baz": (1, 2, 3)}, "deadbeef") Dumping into a string so that one can store it in a cookie: >>> value = x.serialize() Loading from that string again: >>> x = SecureCookie.unserialize(value, "deadbeef") >>> x["baz"] (1, 2, 3) If someone modifies the cookie and the checksum is wrong the unserialize method will fail silently and return a new empty `SecureCookie` object. Keep in mind that the values will be visible in the cookie so do not store data in a cookie you don't want the user to see. Application Integration ======================= If you are using the werkzeug request objects you could integrate the secure cookie into your application like this:: from werkzeug.utils import cached_property from werkzeug.wrappers import BaseRequest from werkzeug.contrib.securecookie import SecureCookie # don't use this key but a different one; you could just use # os.urandom(20) to get something random SECRET_KEY = '\xfa\xdd\xb8z\xae\xe0}4\x8b\xea' class Request(BaseRequest): @cached_property def client_session(self): data = self.cookies.get('session_data') if not data: return SecureCookie(secret_key=SECRET_KEY) return SecureCookie.unserialize(data, SECRET_KEY) def application(environ, start_response): request = Request(environ, start_response) # get a response object here response = ... if request.client_session.should_save: session_data = request.client_session.serialize() response.set_cookie('session_data', session_data, httponly=True) return response(environ, start_response) A less verbose integration can be achieved by using shorthand methods:: class Request(BaseRequest): @cached_property def client_session(self): return SecureCookie.load_cookie(self, secret_key=COOKIE_SECRET) def application(environ, start_response): request = Request(environ, start_response) # get a response object here response = ... request.client_session.save_cookie(response) return response(environ, start_response) :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details. :license: BSD, see LICENSE for more details. i����N( t new( t time( t sha1( t iteritemst text_type( t url_quote_plust url_unquote_plus( t _date_to_unix( t ModificationTrackingDict( t safe_str_cmp( t to_nativet UnquoteErrorc B s e Z d Z RS( s6 Internal exception used to signal failures on quoting.( t __name__t __module__t __doc__( ( ( sL /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/securecookie.pyR i s t SecureCookiec B s� e Z d Z e e � Z e Z e Z d d e d � Z d � Z e d � � Z e d � � Z e d � � Z d d � Z e d � � Z e d d d � � Z d d d d d d d e e d � Z RS( s� Represents a secure cookie. You can subclass this class and provide an alternative mac method. The import thing is that the mac method is a function with a similar interface to the hashlib. Required methods are update() and digest(). Example usage: >>> x = SecureCookie({"foo": 42, "baz": (1, 2, 3)}, "deadbeef") >>> x["foo"] 42 >>> x["baz"] (1, 2, 3) >>> x["blafasel"] = 23 >>> x.should_save True :param data: the initial data. Either a dict, list of tuples or `None`. :param secret_key: the secret key. If not set `None` or not specified it has to be set before :meth:`serialize` is called. :param new: The initial value of the `new` flag. c C sG t j | | p d � | d k r1 t | � } n | | _ | | _ d S( N( ( R t __init__t Nonet bytest secret_keyR ( t selft dataR R ( ( sL /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/securecookie.pyR � s c C s/ d | j j t j | � | j r'