<s> '''<STR_LIT>''' <EOL> import smisk . core <EOL> import smisk . mvc . http as http <EOL> from smisk . core import Application as App <EOL> from smisk . mvc . decorators import leaf_filter , LeafFilter <EOL> from smisk . mvc . helpers import redirect_to <EOL> from smisk . mvc . model import sql <EOL> from time import time <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import md5 <EOL> __all__ = [ '<STR_LIT>' ] <EOL> @ leaf_filter <EOL> def confirm ( leaf , * va , ** params ) : <EOL> '''<STR_LIT>''' <EOL> req = App . current . request <EOL> params [ '<STR_LIT>' ] = False <EOL> try : <EOL> if params [ '<STR_LIT>' ] == req . session [ '<STR_LIT>' ] : <EOL> params [ '<STR_LIT>' ] = True <EOL> except ( KeyError , TypeError ) : <EOL> pass <EOL> try : del params [ '<STR_LIT>' ] <EOL> except : pass <EOL> rsp = leaf ( * va , ** params ) <EOL> if not params [ '<STR_LIT>' ] : <EOL> if not isinstance ( req . session , dict ) : <EOL> req . session = { } <EOL> confirm_token = smisk . core . uid ( ) <EOL> req . session [ '<STR_LIT>' ] = confirm_token <EOL> if not isinstance ( rsp , dict ) : <EOL> rsp = { } <EOL> rsp [ '<STR_LIT>' ] = confirm_token <EOL> else : <EOL> try : del req . session [ '<STR_LIT>' ] <EOL> except : pass <EOL> try : del rsp [ '<STR_LIT>' ] <EOL> except : pass <EOL> return rsp <EOL> class AuthFilter ( LeafFilter ) : <EOL> authorized_param = '<STR_LIT>' <EOL> create_leaf = None <EOL> def __init__ ( self , create_leaf = None , authorized_param = None ) : <EOL> if create_leaf : <EOL> self . create_leaf = create_leaf <EOL> if authorized_param : <EOL> self . authorized_param = authorized_param <EOL> @ property <EOL> def authorized ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ property <EOL> def have_valid_create_leaf ( self ) : <EOL> return self . create_leaf and ( isinstance ( self . create_leaf , basestring ) or control . uri_for ( self . create_leaf ) is not None ) <EOL> def will_authorize ( self , * va , ** kw ) : <EOL> pass <EOL> def did_authorize ( self , user , rsp , exc ) : <EOL> pass <EOL> def did_fail ( self ) : <EOL> if not self . have_valid_create_leaf : <EOL> raise http . Unauthorized ( ) <EOL> redirect_to ( self . create_leaf ) <EOL> def create ( self , leaf ) : <EOL> return self . filter_proxy ( leaf , self . _create ) <EOL> def _create ( self , leaf , * va , ** kw ) : <EOL> exc = None <EOL> rsp = None <EOL> self . will_authorize ( va , kw ) <EOL> try : <EOL> rsp = leaf ( * va , ** kw ) <EOL> except http . HTTPExc , e : <EOL> exc = e <EOL> if rsp and isinstance ( rsp , dict ) and self . authorized_param in rsp and rsp [ self . authorized_param ] : <EOL> self . did_authorize ( rsp [ self . authorized_param ] , rsp , exc ) <EOL> if exc : <EOL> raise exc <EOL> return rsp <EOL> def require ( self , leaf ) : <EOL> return self . filter_proxy ( leaf , self . _require ) <EOL> __call__ = require <EOL> def _require ( self , leaf , * va , ** kw ) : <EOL> if not self . authorized : <EOL> self . did_fail ( ) <EOL> return leaf ( * va , ** kw ) <EOL> def destroy ( self , leaf ) : <EOL> return self . filter_proxy ( leaf , self . _destroy ) <EOL> def _destroy ( self , leaf , * va , ** kw ) : <EOL> App . current . request . session = None <EOL> return leaf ( * va , ** kw ) <EOL> class SessionAuthFilter ( AuthFilter ) : <EOL> referrer_param = '<STR_LIT>' <EOL> @ property <EOL> def session ( self ) : <EOL> if not isinstance ( App . current . request . session , dict ) : <EOL> App . current . request . session = { } <EOL> return App . current . request . session <EOL> @ property <EOL> def authorized ( self ) : <EOL> if isinstance ( App . current . request . session , dict ) : <EOL> return App . current . request . session . get ( self . authorized_param ) <EOL> def will_authorize ( self , va , kw ) : <EOL> if self . referrer_param in kw : <EOL> self . session [ self . referrer_param ] = kw [ self . referrer_param ] <EOL> del kw [ self . referrer_param ] <EOL> def did_authorize ( self , user , rsp , exc ) : <EOL> self . session [ self . authorized_param ] = user <EOL> if self . referrer_param in self . session : <EOL> referrer = self . session [ self . referrer_param ] <EOL> del self . session [ self . referrer_param ] <EOL> redirect_to ( referrer ) <EOL> def did_fail ( self ) : <EOL> if not self . have_valid_create_leaf : <EOL> raise http . Unauthorized ( ) <EOL> redirect_to ( self . create_leaf , ** { self . referrer_param : App . current . request . url } ) <EOL> class DigestAuthFilter ( LeafFilter ) : <EOL> '''<STR_LIT>''' <EOL> required = [ '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> users = { } <EOL> def __init__ ( self , realm , users = None , require_authentication = True ) : <EOL> self . realm = realm <EOL> if users is not None : <EOL> self . users = users <EOL> self . require_authentication = require_authentication <EOL> self . leaf = None <EOL> def respond_unauthorized ( self , send401 = True , * va , ** kw ) : <EOL> if not send401 : <EOL> kw [ '<STR_LIT>' ] = None <EOL> return self . leaf ( * va , ** kw ) <EOL> App . current . response . headers . append ( <EOL> '<STR_LIT>' <EOL> % ( self . realm , self . create_nonce ( ) ) <EOL> ) <EOL> raise http . Unauthorized ( ) <EOL> def respond_authorized ( self , user , * va , ** kw ) : <EOL> kw [ '<STR_LIT>' ] = user <EOL> return self . leaf ( * va , ** kw ) <EOL> def get_authorized ( self , username ) : <EOL> return username <EOL> def create_nonce ( self ) : <EOL> return md5 ( '<STR_LIT>' % ( time ( ) , self . realm ) ) . hexdigest ( ) <EOL> def H ( self , data ) : <EOL> return md5 ( data ) . hexdigest ( ) <EOL> def KD ( self , secret , data ) : <EOL> return self . H ( secret + '<STR_LIT::>' + data ) <EOL> def filter ( self , * va , ** kw ) : <EOL> if '<STR_LIT>' not in App . current . request . env : <EOL> return self . respond_unauthorized ( self . require_authentication , * va , ** kw ) <EOL> if not App . current . request . env [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> raise http . BadRequest ( '<STR_LIT>' ) <EOL> params = { } <EOL> required = len ( self . required ) <EOL> for k , v in [ i . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) for i in App . current . request . env [ '<STR_LIT>' ] [ <NUM_LIT:7> : ] . strip ( ) . split ( '<STR_LIT:U+002C>' ) ] : <EOL> k = k . strip ( ) <EOL> params [ k ] = v . strip ( ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> if k in self . required : <EOL> required -= <NUM_LIT:1> <EOL> if required > <NUM_LIT:0> : <EOL> raise http . BadRequest ( '<STR_LIT>' ) <EOL> if params [ '<STR_LIT:username>' ] not in self . users : <EOL> return self . respond_unauthorized ( True , * va , ** kw ) <EOL> A1 = '<STR_LIT>' % ( params [ '<STR_LIT:username>' ] , self . realm , self . users [ params [ '<STR_LIT:username>' ] ] ) <EOL> A2 = App . current . request . method + '<STR_LIT::>' + App . current . request . url . uri <EOL> expected_response = None <EOL> if '<STR_LIT>' in params : <EOL> if not '<STR_LIT>' in params or not '<STR_LIT>' in params : <EOL> raise http . BadRequest ( '<STR_LIT>' ) <EOL> if params [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> raise http . BadRequest ( '<STR_LIT>' + params [ '<STR_LIT>' ] ) <EOL> expected_response = self . KD ( self . H ( A1 ) , '<STR_LIT>' % ( <EOL> params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] , self . H ( A2 ) ) ) <EOL> else : <EOL> expected_response = self . KD ( self . H ( A1 ) , params [ '<STR_LIT>' ] + '<STR_LIT::>' + self . H ( A2 ) ) <EOL> if params [ '<STR_LIT>' ] != self . realm : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return self . respond_unauthorized ( True , * va , ** kw ) <EOL> if params [ '<STR_LIT>' ] != expected_response : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return self . respond_unauthorized ( True , * va , ** kw ) <EOL> return self . respond_authorized ( params [ '<STR_LIT:username>' ] , * va , ** kw ) <EOL> class sortable_entities ( LeafFilter ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , entity , parameter , sortdefault , orderdefault = '<STR_LIT>' , kwprefix = '<STR_LIT>' ) : <EOL> self . entity = entity <EOL> self . parameter = parameter <EOL> self . sortdefault = sortdefault <EOL> self . orderdefault = orderdefault <EOL> self . kwprefix = kwprefix <EOL> def filter ( self , leaf , * va , ** kw ) : <EOL> rsp = leaf ( * va , ** kw ) <EOL> if self . parameter in rsp : <EOL> q = rsp [ self . parameter ] <EOL> else : <EOL> q = self . entity . query <EOL> if not q : <EOL> return rsp <EOL> sort = kw . get ( self . kwprefix + '<STR_LIT>' , self . sortdefault ) <EOL> order = kw . get ( self . kwprefix + '<STR_LIT>' , self . orderdefault ) <EOL> if sort : <EOL> sort_key = getattr ( self . entity , sort ) <EOL> if order == '<STR_LIT>' : <EOL> q = q . order_by ( sql . desc ( sort_key ) ) <EOL> else : <EOL> q = q . order_by ( sort_key ) <EOL> rsp . update ( { <EOL> self . parameter : q . all ( ) , <EOL> self . kwprefix + '<STR_LIT>' : sort , <EOL> self . kwprefix + '<STR_LIT>' : order , <EOL> self . kwprefix + '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) [ int ( order == '<STR_LIT>' ) ] <EOL> } ) <EOL> return rsp </s>
