<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import sys <EOL> from StringIO import StringIO <EOL> from paste . httpserver import serve <EOL> from repoze . bfg . authentication import RemoteUserAuthenticationPolicy <EOL> from repoze . bfg . authorization import ACLAuthorizationPolicy <EOL> from repoze . bfg . configuration import Configurator <EOL> from repoze . bfg . security import Allow <EOL> from repoze . bfg . security import Authenticated <EOL> from repoze . bfg . security import DENY_ALL <EOL> from repoze . bfg . security import Everyone <EOL> from repoze . who . api import get_api <EOL> from repoze . who . interfaces import IChallenger <EOL> from repoze . who . middleware import PluggableAuthenticationMiddleware as PAM <EOL> from repoze . who . plugins . basicauth import BasicAuthPlugin <EOL> from repoze . who . plugins . auth_tkt import AuthTktCookiePlugin <EOL> from repoze . who . plugins . redirector import RedirectorPlugin <EOL> from repoze . who . plugins . htpasswd import HTPasswdPlugin <EOL> from repoze . who . classifiers import default_request_classifier <EOL> from repoze . who . classifiers import default_challenge_decider <EOL> from webob import Response <EOL> from webob . exc import HTTPFound <EOL> LINK = '<STR_LIT>' <EOL> ACTIONS = { <EOL> '<STR_LIT:root>' : { '<STR_LIT:url>' : '<STR_LIT:/>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:url>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:url>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:url>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> } <EOL> def _actions ( request ) : <EOL> names = [ '<STR_LIT:root>' ] <EOL> if '<STR_LIT>' in request . environ : <EOL> names . append ( '<STR_LIT>' ) <EOL> names . append ( '<STR_LIT>' ) <EOL> else : <EOL> names . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( [ LINK % ACTIONS [ x ] for x in names ] ) <EOL> PAGE = """<STR_LIT>""" <EOL> def unprotected ( request ) : <EOL> return Response ( PAGE % { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _actions ( request ) , <EOL> } ) <EOL> def protected ( request ) : <EOL> return Response ( PAGE % { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _actions ( request ) , <EOL> } ) <EOL> LOGIN_FORM = """<STR_LIT>""" <EOL> def login ( request ) : <EOL> message = '<STR_LIT>' <EOL> info = { } <EOL> came_from = request . params . get ( '<STR_LIT>' ) <EOL> if came_from is not None : <EOL> info [ '<STR_LIT>' ] = ( <EOL> '<STR_LIT>' % came_from ) <EOL> else : <EOL> info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> who_api = get_api ( request . environ ) <EOL> if '<STR_LIT>' in request . POST : <EOL> creds = { } <EOL> creds [ '<STR_LIT>' ] = request . POST [ '<STR_LIT>' ] <EOL> creds [ '<STR_LIT:password>' ] = request . POST [ '<STR_LIT:password>' ] <EOL> authenticated , headers = who_api . login ( creds ) <EOL> if authenticated : <EOL> return HTTPFound ( location = came_from or '<STR_LIT:/>' , <EOL> headers = headers ) <EOL> else : <EOL> message = '<STR_LIT>' <EOL> else : <EOL> _ , headers = who_api . login ( { } ) <EOL> request . response_headerlist = headers <EOL> if '<STR_LIT>' in request . environ : <EOL> del request . environ [ '<STR_LIT>' ] <EOL> info [ '<STR_LIT:message>' ] = message <EOL> return Response ( LOGIN_FORM % info ) <EOL> def logout ( request ) : <EOL> who_api = get_api ( request . environ ) <EOL> return HTTPFound ( location = '<STR_LIT:/>' , headers = who_api . logout ( ) ) <EOL> class Root ( object ) : <EOL> __acl__ = [ ( Allow , Authenticated , ( '<STR_LIT>' , ) ) , <EOL> ( Allow , Everyone , ( '<STR_LIT>' , ) ) , <EOL> DENY_ALL , <EOL> ] <EOL> def get_root ( * args , ** kw ) : <EOL> return Root ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> authentication_policy = RemoteUserAuthenticationPolicy ( ) <EOL> authorization_policy = ACLAuthorizationPolicy ( ) <EOL> config = Configurator ( <EOL> root_factory = get_root , <EOL> default_permission = '<STR_LIT>' , <EOL> authentication_policy = authentication_policy , <EOL> authorization_policy = authorization_policy , <EOL> ) <EOL> config . begin ( ) <EOL> config . add_view ( unprotected ) <EOL> config . add_view ( protected , '<STR_LIT>' , permission = '<STR_LIT>' ) <EOL> config . add_view ( login , '<STR_LIT>' ) <EOL> config . add_view ( logout , '<STR_LIT>' ) <EOL> config . end ( ) <EOL> app = config . make_wsgi_app ( ) <EOL> io = StringIO ( ) <EOL> for name , password in [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:user>' , '<STR_LIT:user>' ) ] : <EOL> io . write ( '<STR_LIT>' % ( name , password ) ) <EOL> io . seek ( <NUM_LIT:0> ) <EOL> def cleartext_check ( password , hashed ) : <EOL> return password == hashed <EOL> htpasswd = HTPasswdPlugin ( io , cleartext_check ) <EOL> basicauth = BasicAuthPlugin ( '<STR_LIT>' ) <EOL> auth_tkt = AuthTktCookiePlugin ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> redirector = RedirectorPlugin ( login_url = '<STR_LIT>' ) <EOL> redirector . classifications = { IChallenger : [ '<STR_LIT>' ] } <EOL> identifiers = [ ( '<STR_LIT>' , auth_tkt ) , <EOL> ( '<STR_LIT>' , basicauth ) ] <EOL> authenticators = [ ( '<STR_LIT>' , auth_tkt ) , <EOL> ( '<STR_LIT>' , htpasswd ) ] <EOL> challengers = [ ( '<STR_LIT>' , redirector ) , <EOL> ( '<STR_LIT>' , basicauth ) ] <EOL> mdproviders = [ ] <EOL> log_stream = None <EOL> if os . environ . get ( '<STR_LIT>' ) : <EOL> log_stream = sys . stdout <EOL> middleware = PAM ( app , <EOL> identifiers , <EOL> authenticators , <EOL> challengers , <EOL> mdproviders , <EOL> default_request_classifier , <EOL> default_challenge_decider , <EOL> log_stream = log_stream , <EOL> log_level = logging . DEBUG <EOL> ) <EOL> serve ( middleware , host = '<STR_LIT>' ) </s>
