<s> import logging <EOL> from paste . request import parse_dict_querystring , parse_formvars <EOL> from paste . httpexceptions import HTTPUnauthorized <EOL> from paste . httpheaders import CONTENT_LENGTH , CONTENT_TYPE <EOL> from repoze . what . middleware import setup_auth <EOL> from repoze . who . plugins . auth_tkt import AuthTktCookiePlugin <EOL> from repoze . who . interfaces import IChallenger , IIdentifier <EOL> import sys <EOL> from zope . interface import implements <EOL> from . adapters import GroupAdapter , PermissionAdapter , Authenticator , MDPlugin <EOL> class BasicAuth ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( IChallenger , IIdentifier ) <EOL> def __init__ ( self , login_url = "<STR_LIT>" , logout_url = "<STR_LIT>" ) : <EOL> self . _login_url = login_url <EOL> self . _logout_url = logout_url <EOL> def identify ( self , environ ) : <EOL> path_info = environ [ '<STR_LIT>' ] <EOL> query = parse_dict_querystring ( environ ) <EOL> if path_info == self . _logout_url : <EOL> environ [ '<STR_LIT>' ] = HTTPUnauthorized ( ) <EOL> return None <EOL> elif path_info == self . _login_url : <EOL> form = parse_formvars ( environ ) <EOL> form . update ( query ) <EOL> try : <EOL> credentials = { <EOL> '<STR_LIT>' : form [ '<STR_LIT>' ] , <EOL> '<STR_LIT:password>' : form [ '<STR_LIT:password>' ] <EOL> } <EOL> except KeyError : <EOL> credentials = None <EOL> def auth_resp ( environ , start_response ) : <EOL> import json <EOL> resp = { <EOL> "<STR_LIT:success>" : True <EOL> } <EOL> resp_str = json . dumps ( resp ) <EOL> content_length = CONTENT_LENGTH . tuples ( str ( len ( resp_str ) ) ) <EOL> content_type = CONTENT_TYPE . tuples ( '<STR_LIT:application/json>' ) <EOL> headers = content_length + content_type <EOL> start_response ( '<STR_LIT>' , headers ) <EOL> return [ resp_str ] <EOL> environ [ '<STR_LIT>' ] = auth_resp <EOL> return credentials <EOL> def challenge ( self , environ , status , app_headers , forget_headers ) : <EOL> cookies = [ ( h , v ) for ( h , v ) in app_headers if h . lower ( ) == '<STR_LIT>' ] <EOL> if not forget_headers : <EOL> return HTTPUnauthorized ( ) <EOL> def auth_form ( environ , start_response ) : <EOL> towrite = "<STR_LIT>" <EOL> content_length = CONTENT_LENGTH . tuples ( str ( len ( towrite ) ) ) <EOL> content_type = CONTENT_TYPE . tuples ( '<STR_LIT>' ) <EOL> headers = content_length + content_type + forget_headers <EOL> start_response ( '<STR_LIT>' , headers ) <EOL> return [ towrite ] <EOL> return auth_form <EOL> def remember ( self , environ , identity ) : <EOL> return environ [ '<STR_LIT>' ] [ '<STR_LIT>' ] . remember ( environ , identity ) <EOL> def forget ( self , environ , identity ) : <EOL> return environ [ '<STR_LIT>' ] [ '<STR_LIT>' ] . forget ( environ , identity ) <EOL> def AuthBasicMiddleware ( app , conf , user_class ) : <EOL> groups = GroupAdapter ( user_class ) <EOL> groups = { '<STR_LIT>' : groups } <EOL> permissions = { '<STR_LIT>' : PermissionAdapter ( conf [ "<STR_LIT>" ] ) } <EOL> basicauth = BasicAuth ( ) <EOL> cookie = AuthTktCookiePlugin ( conf [ '<STR_LIT>' ] ) <EOL> who_args = { } <EOL> who_args [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , Authenticator ( user_class ) ) ] <EOL> who_args [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , basicauth ) ] <EOL> who_args [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , basicauth ) , ( '<STR_LIT>' , cookie ) ] <EOL> who_args [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , MDPlugin ( user_class ) ) ] <EOL> who_args [ '<STR_LIT>' ] = sys . stdout <EOL> who_args [ '<STR_LIT>' ] = logging . DEBUG <EOL> return setup_auth ( app , groups , permissions , ** who_args ) </s>
