<s> '''<STR_LIT>''' <EOL> from biicode . common . exception import AuthenticationException , ForbiddenException <EOL> from biicode . common . api . biiapi import BiiAPI <EOL> from biicode . common . utils . bii_logging import logger <EOL> from uuid import getnode as get_mac <EOL> import hashlib <EOL> def input_credentials_if_unauthorized ( func ) : <EOL> """<STR_LIT>""" <EOL> LOGIN_RETRIES = <NUM_LIT:3> <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> self . set_custom_headers ( self . user ) <EOL> return func ( self , * args , ** kwargs ) <EOL> except ForbiddenException as e : <EOL> logger . debug ( "<STR_LIT>" % str ( e ) ) <EOL> if self . user is None or self . rest_client . token is None : <EOL> self . user_io . out . info ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if self . user is None : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> return retry_with_new_token ( self , * args , ** kwargs ) <EOL> else : <EOL> raise e <EOL> except AuthenticationException : <EOL> self . _store_login ( ( self . user , None ) ) <EOL> self . rest_client . token = None <EOL> self . set_custom_headers ( self . user ) <EOL> return wrapper ( self , * args , ** kwargs ) <EOL> def retry_with_new_token ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for _ in range ( LOGIN_RETRIES ) : <EOL> user , password = self . user_io . request_login ( self . user ) <EOL> token = None <EOL> try : <EOL> token = self . authenticate ( user , password ) <EOL> except AuthenticationException : <EOL> if self . user is None : <EOL> self . user_io . out . error ( '<STR_LIT>' ) <EOL> else : <EOL> self . user_io . out . error ( '<STR_LIT>' % self . user ) <EOL> self . user_io . out . info ( '<STR_LIT>' ) <EOL> if token : <EOL> self . rest_client . token = token <EOL> self . user = user <EOL> self . _store_login ( ( user , token ) ) <EOL> self . set_custom_headers ( user ) <EOL> return func ( self , * args , ** kwargs ) <EOL> raise AuthenticationException ( "<STR_LIT>" ) <EOL> return wrapper <EOL> class BiiApiAuthManager ( BiiAPI ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rest_client , user_io , localdb ) : <EOL> self . user_io = user_io <EOL> self . rest_client = rest_client <EOL> self . localdb = localdb <EOL> self . user , self . rest_client . token = localdb . get_login ( ) <EOL> def _store_login ( self , login ) : <EOL> try : <EOL> self . localdb . set_login ( login ) <EOL> except Exception as e : <EOL> self . user_io . out . error ( '<STR_LIT>' ) <EOL> self . user_io . out . debug ( str ( e ) + '<STR_LIT:\n>' ) <EOL> @ staticmethod <EOL> def get_mac_digest ( ) : <EOL> sha1 = hashlib . sha1 ( ) <EOL> sha1 . update ( str ( get_mac ( ) ) ) <EOL> return str ( sha1 . hexdigest ( ) ) <EOL> def set_custom_headers ( self , username ) : <EOL> self . rest_client . custom_headers [ '<STR_LIT>' ] = self . get_mac_digest ( ) <EOL> self . rest_client . custom_headers [ '<STR_LIT>' ] = username <EOL> @ input_credentials_if_unauthorized <EOL> def get_dep_table ( self , block_version ) : <EOL> return self . rest_client . get_dep_table ( block_version ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_published_resources ( self , references ) : <EOL> return self . rest_client . get_published_resources ( references ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_cells_snapshot ( self , block_version ) : <EOL> return self . rest_client . get_cells_snapshot ( block_version ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_renames ( self , brl_block , t1 , t2 ) : <EOL> return self . rest_client . get_renames ( brl_block , t1 , t2 ) <EOL> @ input_credentials_if_unauthorized <EOL> def publish ( self , publish_request ) : <EOL> return self . rest_client . publish ( publish_request ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_version_delta_info ( self , block_version ) : <EOL> return self . rest_client . get_version_delta_info ( block_version ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_version_by_tag ( self , brl_block , version_tag ) : <EOL> return self . rest_client . get_version_by_tag ( brl_block , version_tag ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_block_info ( self , brl_block ) : <EOL> return self . rest_client . get_block_info ( brl_block ) <EOL> @ input_credentials_if_unauthorized <EOL> def find ( self , finder_request , response ) : <EOL> return self . rest_client . find ( finder_request , response ) <EOL> @ input_credentials_if_unauthorized <EOL> def read_hive ( self , brl_hive ) : <EOL> return self . rest_client . read_hive ( brl_hive ) <EOL> @ input_credentials_if_unauthorized <EOL> def upload ( self , brl_hive , upload_pack ) : <EOL> return self . rest_client . upload ( brl_hive , upload_pack ) <EOL> @ input_credentials_if_unauthorized <EOL> def get_server_info ( self ) : <EOL> return self . rest_client . get_server_info ( ) <EOL> @ input_credentials_if_unauthorized <EOL> def require_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . rest_client . require_auth ( ) <EOL> def authenticate ( self , user , password ) : <EOL> """<STR_LIT>""" <EOL> return self . rest_client . authenticate ( user , password ) </s>
