<s> import base64 <EOL> from conary import errors <EOL> from conary . lib import util <EOL> class PermissionDenied ( errors . WebError ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def getAuth ( req ) : <EOL> if hasattr ( req , '<STR_LIT>' ) : <EOL> headers_in = req . headers_in <EOL> remote_ip = req . connection . remote_ip <EOL> else : <EOL> headers_in = req . headers <EOL> remote_ip = req . remote_addr <EOL> if not '<STR_LIT>' in headers_in : <EOL> authToken = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> info = headers_in [ '<STR_LIT>' ] . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if len ( info ) != <NUM_LIT:2> or info [ <NUM_LIT:0> ] != "<STR_LIT>" : <EOL> return None <EOL> try : <EOL> authString = base64 . decodestring ( info [ <NUM_LIT:1> ] ) <EOL> except : <EOL> return None <EOL> authToken = authString . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> if len ( authToken ) != <NUM_LIT:2> : <EOL> authToken . append ( util . ProtectedString ( '<STR_LIT>' ) ) <EOL> else : <EOL> authToken [ <NUM_LIT:1> ] = util . ProtectedString ( authToken [ <NUM_LIT:1> ] ) <EOL> try : <EOL> entitlementList = parseEntitlement ( <EOL> headers_in . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> return None <EOL> authToken . append ( entitlementList ) <EOL> authToken . append ( remote_ip ) <EOL> return authToken <EOL> class Authorization : <EOL> def __init__ ( self , passwordOK = False , isInternal = False , userId = - <NUM_LIT:1> ) : <EOL> self . passwordOK = passwordOK <EOL> self . isInternal = isInternal <EOL> self . userId = userId <EOL> def requiresAuth ( func ) : <EOL> def wrapper ( self , ** kwargs ) : <EOL> if not kwargs [ '<STR_LIT>' ] . passwordOK : <EOL> raise PermissionDenied <EOL> else : <EOL> return func ( self , ** kwargs ) <EOL> return wrapper <EOL> def internalOnly ( func ) : <EOL> def wrapper ( self , ** kwargs ) : <EOL> if not kwargs [ '<STR_LIT>' ] . isInternal : <EOL> raise PermissionDenied <EOL> else : <EOL> return func ( self , ** kwargs ) <EOL> return wrapper <EOL> def externalOnly ( func ) : <EOL> def wrapper ( self , ** kwargs ) : <EOL> if self . cfg . externalAccess : <EOL> return func ( self , ** kwargs ) <EOL> else : <EOL> raise PermissionDenied <EOL> return wrapper <EOL> def parseEntitlement ( entHeader ) : <EOL> entitlementList = [ ] <EOL> allEntitlements = entHeader . split ( ) <EOL> for i in range ( <NUM_LIT:0> , len ( allEntitlements ) , <NUM_LIT:2> ) : <EOL> ent = [ allEntitlements [ i ] ] <EOL> ent . append ( base64 . decodestring ( allEntitlements [ i + <NUM_LIT:1> ] ) ) <EOL> if ent [ <NUM_LIT:0> ] == '<STR_LIT:*>' : <EOL> ent [ <NUM_LIT:0> ] = None <EOL> entitlementList . append ( tuple ( ent ) ) <EOL> return entitlementList </s>
