<s> """<STR_LIT>""" <EOL> from urlparse import parse_qsl <EOL> from twisted . cred . error import Unauthorized , UnauthorizedLogin <EOL> from twimp . auth . error import AuthProtocolContractError <EOL> def parse_url_args ( argstr ) : <EOL> args = { } <EOL> if argstr : <EOL> args . update ( parse_qsl ( argstr . lstrip ( '<STR_LIT:?>' ) ) ) <EOL> return args <EOL> def parse_description_string ( desc , authmod = None ) : <EOL> base , opts = None , None <EOL> for rpart in desc . split ( '<STR_LIT::>' ) : <EOL> part = rpart . strip ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in part : <EOL> kvs = dict ( v . strip ( ) . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) for v in part . split ( '<STR_LIT:;>' ) ) <EOL> if not authmod or kvs . get ( '<STR_LIT>' ) == authmod : <EOL> base = kvs <EOL> elif part . startswith ( '<STR_LIT:?>' ) : <EOL> opts = parse_url_args ( part ) <EOL> return base , opts <EOL> class Credentials ( object ) : <EOL> invalid = True <EOL> class Authenticator ( object ) : <EOL> authmod = None <EOL> def __init__ ( self , authmod = None ) : <EOL> self . authmod = authmod <EOL> def unhandled_code ( self , code_str , cred , challenge ) : <EOL> raise AuthProtocolContractError ( '<STR_LIT>' , <EOL> ( challenge , ) ) <EOL> def unhandled_reason ( self , reason , cred , challenge ) : <EOL> raise AuthProtocolContractError ( '<STR_LIT>' , <EOL> ( challenge , ) ) <EOL> def initial_args ( self , args , cred , challenge ) : <EOL> if cred . invalid : <EOL> raise Unauthorized ( '<STR_LIT>' % ( cred , ) ) <EOL> return args <EOL> def get_auth_args ( self , cred , challenge ) : <EOL> """<STR_LIT>""" <EOL> base , opts = None , None <EOL> args = { } <EOL> if challenge : <EOL> base , opts = parse_description_string ( challenge , <EOL> authmod = self . authmod ) <EOL> if not base : <EOL> if not challenge : <EOL> return self . initial_args ( args , cred , challenge ) <EOL> raise UnauthorizedLogin ( '<STR_LIT>' % <EOL> ( challenge , ) ) <EOL> else : <EOL> if self . authmod : <EOL> args . update ( authmod = self . authmod ) <EOL> code_str = base . get ( '<STR_LIT:code>' ) <EOL> if code_str : <EOL> code = code_str . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> handler_m = getattr ( self , '<STR_LIT>' % code , None ) <EOL> if handler_m is None : <EOL> return self . unhandled_code ( code_str , cred , challenge ) <EOL> else : <EOL> return handler_m ( args , cred , code_str , challenge ) <EOL> elif opts and opts . has_key ( '<STR_LIT>' ) : <EOL> reason = opts [ '<STR_LIT>' ] <EOL> handler_m = getattr ( self , '<STR_LIT>' % ( reason , ) ) <EOL> if handler_m is None : <EOL> return self . unhandled_reason ( reason , cred , challenge ) <EOL> else : <EOL> return handler_m ( args , cred , opts , challenge ) <EOL> else : <EOL> raise AuthProtocolContractError ( "<STR_LIT>" % <EOL> ( challenge , ) ) </s>
