<s> '''<STR_LIT>''' <EOL> from hashlib import sha512 <EOL> from os . path import dirname , join as path_join , isdir <EOL> try : <EOL> from os . path import relpath <EOL> except ImportError : <EOL> from common import relpath <EOL> from common import ProtocolError <EOL> from config import USER_PASSWORD , DATA_DIR <EOL> from message import Messager <EOL> from session import get_session , invalidate_session <EOL> from projectconfig import ProjectConfiguration <EOL> class NotAuthorisedError ( ProtocolError ) : <EOL> def __init__ ( self , attempted_action ) : <EOL> self . attempted_action = attempted_action <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . attempted_action <EOL> def json ( self , json_dic ) : <EOL> json_dic [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return json_dic <EOL> class AccessDeniedError ( ProtocolError ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def json ( self , json_dic ) : <EOL> json_dic [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Messager . error ( '<STR_LIT>' ) <EOL> return json_dic <EOL> class InvalidAuthError ( ProtocolError ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def json ( self , json_dic ) : <EOL> json_dic [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return json_dic <EOL> def _is_authenticated ( user , password ) : <EOL> return ( user in USER_PASSWORD and <EOL> password == USER_PASSWORD [ user ] ) <EOL> def _password_hash ( password ) : <EOL> return sha512 ( password ) . hexdigest ( ) <EOL> def login ( user , password ) : <EOL> if not _is_authenticated ( user , password ) : <EOL> raise InvalidAuthError <EOL> get_session ( ) [ '<STR_LIT:user>' ] = user <EOL> Messager . info ( '<STR_LIT>' ) <EOL> return { } <EOL> def logout ( ) : <EOL> try : <EOL> del get_session ( ) [ '<STR_LIT:user>' ] <EOL> except KeyError : <EOL> pass <EOL> Messager . info ( '<STR_LIT>' ) <EOL> return { } <EOL> def whoami ( ) : <EOL> json_dic = { } <EOL> try : <EOL> json_dic [ '<STR_LIT:user>' ] = get_session ( ) . get ( '<STR_LIT:user>' ) <EOL> except KeyError : <EOL> Messager . error ( '<STR_LIT>' , duration = <NUM_LIT:3> ) <EOL> return json_dic <EOL> def allowed_to_read ( real_path ) : <EOL> data_path = path_join ( '<STR_LIT:/>' , relpath ( real_path , DATA_DIR ) ) <EOL> if isdir ( real_path ) : <EOL> data_path = '<STR_LIT>' % ( data_path ) <EOL> real_dir = dirname ( real_path ) <EOL> robotparser = ProjectConfiguration ( real_dir ) . get_access_control ( ) <EOL> if robotparser is None : <EOL> return True <EOL> try : <EOL> user = get_session ( ) . get ( '<STR_LIT:user>' ) <EOL> except KeyError : <EOL> user = None <EOL> if user is None : <EOL> user = '<STR_LIT>' <EOL> return robotparser . can_fetch ( user , data_path ) </s>
