<s> from __future__ import print_function <EOL> import json <EOL> import getpass <EOL> from . models import User <EOL> from giotto . exceptions import NotAuthorized <EOL> from giotto . control import Redirection <EOL> from giotto import get_config <EOL> from giotto . middleware import GiottoOutputMiddleware , GiottoInputMiddleware <EOL> class AuthenticationMiddleware ( GiottoInputMiddleware ) : <EOL> """<STR_LIT>""" <EOL> def http ( self , request ) : <EOL> user = None <EOL> session_key = request . cookies . get ( '<STR_LIT>' , None ) <EOL> if not session_key and request . POST : <EOL> session_key = request . POST . get ( '<STR_LIT>' , None ) <EOL> if session_key : <EOL> username = get_config ( '<STR_LIT>' ) . get ( session_key ) <EOL> try : <EOL> user = User . objects . get ( username = username ) <EOL> except User . DoesNotExist : <EOL> user = None <EOL> setattr ( request , '<STR_LIT:user>' , user ) <EOL> return request <EOL> def cmd ( self , request ) : <EOL> user = None <EOL> session_key = request . enviornment . get ( '<STR_LIT>' , None ) <EOL> if session_key : <EOL> user = get_config ( '<STR_LIT>' ) . get ( session_key ) <EOL> if not user : <EOL> print ( "<STR_LIT>" ) <EOL> username = raw_input ( ) <EOL> password = getpass . getpass ( ) <EOL> user = User . objects . get_user_by_password ( username , password ) <EOL> setattr ( request , '<STR_LIT:user>' , user ) <EOL> return request <EOL> class AuthenticatedOrDie ( GiottoInputMiddleware ) : <EOL> """<STR_LIT>""" <EOL> def http ( self , request ) : <EOL> if not request . user : <EOL> raise NotAuthorized ( '<STR_LIT>' ) <EOL> return request <EOL> def cmd ( self , request ) : <EOL> user = getattr ( request , '<STR_LIT:user>' , None ) <EOL> if not user : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> return request <EOL> class NotAuthenticatedOrDie ( GiottoInputMiddleware ) : <EOL> """<STR_LIT>""" <EOL> def http ( self , request ) : <EOL> if request . user : <EOL> raise NotAuthorized ( '<STR_LIT>' ) <EOL> return request <EOL> def cmd ( self , request ) : <EOL> user = getattr ( request , '<STR_LIT:user>' , None ) <EOL> if user : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> return request <EOL> def AuthenticatedOrRedirect ( invocation ) : <EOL> """<STR_LIT>""" <EOL> class AuthenticatedOrRedirect ( GiottoInputMiddleware ) : <EOL> def http ( self , request ) : <EOL> if request . user : <EOL> return request <EOL> return Redirection ( invocation ) <EOL> def cmd ( self , request ) : <EOL> if request . user : <EOL> return request <EOL> return Redirection ( invocation ) <EOL> return AuthenticatedOrRedirect <EOL> def NotAuthenticatedOrRedirect ( invocation ) : <EOL> """<STR_LIT>""" <EOL> class NotAuthenticatedOrRedirect ( GiottoInputMiddleware ) : <EOL> def http ( self , request ) : <EOL> if not request . user : <EOL> return request <EOL> return Redirection ( invocation ) <EOL> def cmd ( self , request ) : <EOL> if not request . user : <EOL> return request <EOL> return Redirection ( invocation ) <EOL> return NotAuthenticatedOrRedirect <EOL> class LogoutMiddleware ( GiottoOutputMiddleware ) : <EOL> """<STR_LIT>""" <EOL> def http ( self , request , response ) : <EOL> if request . method == '<STR_LIT:POST>' and '<STR_LIT>' in request . POST : <EOL> key = request . POST [ '<STR_LIT>' ] <EOL> session_key = get_config ( '<STR_LIT>' ) . set ( key , None , <NUM_LIT:1> ) <EOL> response . delete_cookie ( '<STR_LIT>' ) <EOL> return response </s>
