<s> from __future__ import print_function , unicode_literals <EOL> import time <EOL> import traceback <EOL> import voodoo . log as log <EOL> from weblab . core . login . web import EXTERNAL_MANAGERS <EOL> from weblab . core . wl import weblab_api <EOL> import weblab . core . login . exc as LoginErrors <EOL> from weblab . core . exc import DbUserNotFoundError <EOL> from weblab . data import ValidDatabaseSessionId <EOL> LOGIN_FAILED_DELAY = <NUM_LIT:2> <EOL> NOT_LINKABLE_USERS = '<STR_LIT>' <EOL> DEFAULT_GROUPS = '<STR_LIT>' <EOL> CREATING_EXTERNAL_USERS = '<STR_LIT>' <EOL> LINKING_EXTERNAL_USERS = '<STR_LIT>' <EOL> class LoginManager ( object ) : <EOL> def __init__ ( self , db , core_server ) : <EOL> self . _db = db <EOL> self . _core_server = core_server <EOL> def login ( self , username , password ) : <EOL> """<STR_LIT>""" <EOL> if not password : <EOL> self . _process_invalid ( ) <EOL> db_session_id = self . _validate_simple_authn ( username , password ) <EOL> return self . _reserve_session ( db_session_id ) <EOL> def _process_invalid ( self ) : <EOL> time . sleep ( LOGIN_FAILED_DELAY ) <EOL> raise LoginErrors . InvalidCredentialsError ( "<STR_LIT>" ) <EOL> def _validate_simple_authn ( self , username , credentials ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> login , role_name , user_auths = self . _db . retrieve_role_and_user_auths ( username ) <EOL> except DbUserNotFoundError : <EOL> return self . _process_invalid ( ) <EOL> errors = False <EOL> for user_auth in user_auths : <EOL> if user_auth . is_simple_authn ( ) : <EOL> try : <EOL> authenticated = user_auth . authenticate ( login , credentials ) <EOL> except : <EOL> log . log ( LoginManager , log . level . Warning , "<STR_LIT>" % ( login , user_auth ) ) <EOL> log . log_exc ( LoginManager , log . level . Warning ) <EOL> errors = True <EOL> traceback . print_exc ( ) <EOL> continue <EOL> if authenticated : <EOL> log . log ( LoginManager , log . level . Debug , "<STR_LIT>" % ( login , user_auth ) ) <EOL> return ValidDatabaseSessionId ( login , role_name ) <EOL> else : <EOL> log . log ( LoginManager , log . level . Warning , "<STR_LIT>" % ( login , user_auth ) ) <EOL> if errors : <EOL> raise LoginErrors . LoginError ( "<STR_LIT>" ) <EOL> return self . _process_invalid ( ) <EOL> def _reserve_session ( self , db_session_id ) : <EOL> session_id , server_route = self . _core_server . _reserve_session ( db_session_id ) <EOL> if hasattr ( session_id , '<STR_LIT:id>' ) : <EOL> weblab_api . ctx . session_id = session_id . id <EOL> else : <EOL> weblab_api . ctx . session_id = session_id <EOL> return session_id <EOL> def extensible_login ( self , system , credentials ) : <EOL> """<STR_LIT>""" <EOL> external_user_id , _ = self . _validate_web_protocol_authn ( system , credentials ) <EOL> try : <EOL> db_session_id = self . _db . check_external_credentials ( external_user_id , system ) <EOL> except DbUserNotFoundError : <EOL> raise LoginErrors . InvalidCredentialsError ( "<STR_LIT>" % ( system , external_user_id ) ) <EOL> return self . _reserve_session ( db_session_id ) <EOL> def _validate_web_protocol_authn ( self , system , credentials ) : <EOL> system = system . upper ( ) <EOL> if not system in EXTERNAL_MANAGERS : <EOL> raise LoginErrors . LoginError ( "<STR_LIT>" ) <EOL> external_user_id = EXTERNAL_MANAGERS [ system ] . get_user_id ( credentials ) <EOL> if external_user_id == "<STR_LIT>" : <EOL> raise LoginErrors . InvalidCredentialsError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> external_user = EXTERNAL_MANAGERS [ system ] . get_user ( credentials ) <EOL> return external_user_id , external_user <EOL> def grant_external_credentials ( self , username , password , system , credentials ) : <EOL> """<STR_LIT>""" <EOL> if not self . _cfg_manager . get_value ( LINKING_EXTERNAL_USERS , True ) : <EOL> raise LoginErrors . LoginError ( "<STR_LIT>" ) <EOL> not_linkable_users = self . _cfg_manager . get_value ( NOT_LINKABLE_USERS , [ ] ) <EOL> for not_linkable_user in not_linkable_users : <EOL> if username == not_linkable_user : <EOL> raise LoginErrors . LoginError ( "<STR_LIT>" ) <EOL> local_db_session_id = self . _validate_simple_authn ( username , password ) <EOL> external_user_id , _ = self . _validate_web_protocol_authn ( system , credentials ) <EOL> self . _db . grant_external_credentials ( username , external_user_id , system ) <EOL> return self . _reserve_session ( local_db_session_id ) <EOL> def create_external_user ( self , system , credentials ) : <EOL> """<STR_LIT>""" <EOL> if not self . _cfg_manager . get_value ( CREATING_EXTERNAL_USERS , True ) : <EOL> raise LoginErrors . LoginError ( "<STR_LIT>" ) <EOL> external_user_id , external_user = self . _validate_web_protocol_authn ( system , credentials ) <EOL> if external_user is None : <EOL> raise LoginErrors . LoginError ( "<STR_LIT>" ) <EOL> group_names = self . _cfg_manager . get_value ( DEFAULT_GROUPS , [ ] ) <EOL> self . _db . create_external_user ( external_user , external_user_id , system , group_names ) <EOL> return self . extensible_login ( system , credentials ) </s>
