<s> """<STR_LIT>""" <EOL> __authors__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import logging <EOL> import validictory <EOL> from copy import deepcopy <EOL> from tornado import gen , options , web <EOL> from viewfinder . backend . base import handler , util <EOL> from viewfinder . backend . base . exceptions import PermissionError <EOL> from viewfinder . backend . db . device import Device <EOL> from viewfinder . backend . db . identity import Identity <EOL> from viewfinder . backend . db . operation import Operation <EOL> from viewfinder . backend . db . user import User <EOL> from viewfinder . backend . resources . message . error_messages import ALREADY_LINKED , ALREADY_REGISTERED , NO_USER_ACCOUNT <EOL> from viewfinder . backend . resources . message . error_messages import MERGE_REQUIRES_LOGIN , LOGIN_REQUIRES_REGISTER <EOL> from viewfinder . backend . www import base , json_schema , www_util <EOL> options . define ( '<STR_LIT>' , False , <EOL> help = '<STR_LIT>' ) <EOL> _NEED_INVITATION_MESSAGE = '<STR_LIT>' <EOL> _ACCOUNT_ALREADY_LINKED_MESSAGE = '<STR_LIT>' <EOL> _CANNOT_SET_DEVICE_FOR_USER_MESSAGE = '<STR_LIT>' <EOL> _FREEZE_NEW_ACCOUNTS_MESSAGE = '<STR_LIT>' + '<STR_LIT>' <EOL> _CANNOT_LINK_TO_PROSPECTIVE = '<STR_LIT>' + '<STR_LIT>' <EOL> class AuthHandler ( base . BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> _AUTH_ATTRIBUTE_MAP = { '<STR_LIT:email>' : '<STR_LIT:email>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT:name>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> """<STR_LIT>""" <EOL> @ gen . coroutine <EOL> def _StartJSONRequest ( self , action , request , schema , migrators = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . api_name = action <EOL> self . _action = action <EOL> self . _request_message = yield gen . Task ( base . BaseHandler . _CreateRequestMessage , <EOL> self . _client , <EOL> self . _LoadJSONRequest ( ) , <EOL> schema , <EOL> migrators = migrators ) <EOL> except Exception as e : <EOL> logging . warning ( '<STR_LIT>' % <EOL> ( type ( e ) . __name__ , e . message , util . FormatLogArgument ( request . body ) ) ) <EOL> raise web . HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> @ gen . engine <EOL> def _FinishJSONRequest ( self , op , response_dict , schema ) : <EOL> """<STR_LIT>""" <EOL> if op is not None : <EOL> scratch_response_dict = deepcopy ( response_dict ) <EOL> scratch_response_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : op . operation_id , <EOL> '<STR_LIT>' : op . timestamp } <EOL> else : <EOL> scratch_response_dict = response_dict <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> response_message = yield gen . Task ( base . BaseHandler . _CreateResponseMessage , <EOL> self . _client , scratch_response_dict , schema , <EOL> self . _request_message . original_version ) <EOL> self . write ( response_message . dict ) <EOL> self . finish ( ) <EOL> def _StartInteractiveRequest ( self , action ) : <EOL> """<STR_LIT>""" <EOL> self . _action = action <EOL> def _FinishInteractiveRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> next = self . get_argument ( '<STR_LIT>' , None ) <EOL> if next is not None : <EOL> self . redirect ( next ) <EOL> else : <EOL> self . redirect ( '<STR_LIT>' ) <EOL> @ gen . coroutine <EOL> def _PrepareAuthUser ( self , user_dict , ident_dict , device_dict ) : <EOL> """<STR_LIT>""" <EOL> ident_dict [ '<STR_LIT>' ] = user_dict <EOL> identity = yield gen . Task ( Identity . Query , self . _client , ident_dict [ '<STR_LIT:key>' ] , None , must_exist = False ) <EOL> current_user = self . get_current_user ( ) <EOL> if self . _action in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if identity is not None and identity . user_id is not None : <EOL> user = yield gen . Task ( User . Query , self . _client , identity . user_id , None , must_exist = False ) <EOL> else : <EOL> user = None <EOL> if user is None : <EOL> raise PermissionError ( NO_USER_ACCOUNT , account = Identity . GetDescription ( ident_dict [ '<STR_LIT:key>' ] ) ) <EOL> if not user . IsRegistered ( ) : <EOL> raise PermissionError ( LOGIN_REQUIRES_REGISTER ) <EOL> user_dict [ '<STR_LIT>' ] = identity . user_id <EOL> elif self . _action == '<STR_LIT>' : <EOL> if identity is not None and identity . user_id is not None : <EOL> user = yield gen . Task ( User . Query , self . _client , identity . user_id , None , must_exist = False ) <EOL> if user is None or user . IsRegistered ( ) : <EOL> raise PermissionError ( ALREADY_REGISTERED , account = Identity . GetDescription ( identity . key ) ) <EOL> user_dict [ '<STR_LIT>' ] = user . user_id <EOL> else : <EOL> user_id , webapp_dev_id = yield User . AllocateUserAndWebDeviceIds ( self . _client ) <EOL> user_dict [ '<STR_LIT>' ] = user_id <EOL> request = { '<STR_LIT>' : { '<STR_LIT>' : True } , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : webapp_dev_id , <EOL> '<STR_LIT>' : ident_dict [ '<STR_LIT:key>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> yield gen . Task ( Operation . CreateAndExecute , <EOL> self . _client , <EOL> user_id , <EOL> webapp_dev_id , <EOL> '<STR_LIT>' , <EOL> request ) <EOL> user = yield gen . Task ( User . Query , self . _client , user_id , None ) <EOL> identity = yield gen . Task ( Identity . Query , self . _client , ident_dict [ '<STR_LIT:key>' ] , None ) <EOL> if options . options . freeze_new_accounts : <EOL> raise web . HTTPError ( <NUM_LIT> , _FREEZE_NEW_ACCOUNTS_MESSAGE ) <EOL> else : <EOL> assert self . _action == '<STR_LIT>' , self . _action <EOL> if current_user is None : <EOL> raise PermissionError ( MERGE_REQUIRES_LOGIN ) <EOL> if not current_user . IsRegistered ( ) : <EOL> raise web . HTTPError ( <NUM_LIT> , _CANNOT_LINK_TO_PROSPECTIVE ) <EOL> if identity is not None and identity . user_id is not None and current_user . user_id != identity . user_id : <EOL> raise PermissionError ( ALREADY_LINKED , account = Identity . GetDescription ( ident_dict [ '<STR_LIT:key>' ] ) ) <EOL> if identity is None : <EOL> identity = Identity . CreateFromKeywords ( key = ident_dict [ '<STR_LIT:key>' ] ) <EOL> yield gen . Task ( identity . Update , self . _client ) <EOL> user = current_user <EOL> user_dict [ '<STR_LIT>' ] = current_user . user_id <EOL> assert user , user_dict <EOL> assert identity , ident_dict <EOL> if device_dict is not None : <EOL> if '<STR_LIT>' in device_dict : <EOL> if '<STR_LIT>' in user_dict : <EOL> device = yield gen . Task ( Device . Query , self . _client , user_dict [ '<STR_LIT>' ] , device_dict [ '<STR_LIT>' ] , <EOL> None , must_exist = False ) <EOL> if device is None : <EOL> raise web . HTTPError ( <NUM_LIT> , '<STR_LIT>' % <EOL> ( user_dict [ '<STR_LIT>' ] , device_dict [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> logging . warning ( '<STR_LIT>' % device_dict ) <EOL> raise web . HTTPError ( <NUM_LIT> , _CANNOT_SET_DEVICE_FOR_USER_MESSAGE ) <EOL> raise gen . Return ( user ) <EOL> @ gen . engine <EOL> def _AuthUser ( self , user_dict , ident_dict , device_dict , confirmed = False ) : <EOL> """<STR_LIT>""" <EOL> before_user = yield gen . Task ( self . _PrepareAuthUser , user_dict , ident_dict , device_dict ) <EOL> scratch_user_dict = { '<STR_LIT>' : user_dict [ '<STR_LIT>' ] } <EOL> for k , v in user_dict . items ( ) : <EOL> user_key = AuthHandler . _AUTH_ATTRIBUTE_MAP . get ( k , None ) <EOL> if user_key is not None : <EOL> if getattr ( before_user , user_key ) is None : <EOL> scratch_user_dict [ user_key ] = v <EOL> if user_key == '<STR_LIT:email>' and ident_dict [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if getattr ( before_user , '<STR_LIT>' ) is None : <EOL> scratch_user_dict [ '<STR_LIT>' ] = v <EOL> if device_dict is not None and '<STR_LIT>' not in device_dict : <EOL> device_dict [ '<STR_LIT>' ] = yield gen . Task ( Device . _allocator . NextId , self . _client ) <EOL> request = { '<STR_LIT>' : { '<STR_LIT>' : True } , <EOL> '<STR_LIT>' : scratch_user_dict , <EOL> '<STR_LIT>' : ident_dict , <EOL> '<STR_LIT>' : device_dict } <EOL> op = yield gen . Task ( Operation . CreateAndExecute , <EOL> self . _client , <EOL> user_dict [ '<STR_LIT>' ] , <EOL> device_dict [ '<STR_LIT>' ] if device_dict is not None else before_user . webapp_dev_id , <EOL> '<STR_LIT>' , <EOL> request ) <EOL> if self . _action == '<STR_LIT>' : <EOL> request = { '<STR_LIT:key>' : ident_dict [ '<STR_LIT:key>' ] , <EOL> '<STR_LIT>' : user_dict [ '<STR_LIT>' ] } <EOL> if self . _IsInteractiveRequest ( ) or self . _request_message . dict [ '<STR_LIT>' ] . get ( '<STR_LIT>' , False ) : <EOL> request [ '<STR_LIT>' ] = { '<STR_LIT>' : True } <EOL> op = yield gen . Task ( Operation . CreateAndExecute , <EOL> self . _client , <EOL> user_dict [ '<STR_LIT>' ] , <EOL> device_dict [ '<STR_LIT>' ] if device_dict is not None else before_user . webapp_dev_id , <EOL> '<STR_LIT>' , <EOL> request ) <EOL> after_user = yield gen . Task ( User . Query , self . _client , user_dict [ '<STR_LIT>' ] , None ) <EOL> confirm_time = util . GetCurrentTimestamp ( ) if confirmed else None <EOL> device_id = after_user . webapp_dev_id if device_dict is None else device_dict [ '<STR_LIT>' ] <EOL> user_cookie_dict = self . CreateUserCookieDict ( after_user . user_id , <EOL> device_id , <EOL> after_user . name , <EOL> confirm_time = confirm_time ) <EOL> if self . _IsInteractiveRequest ( ) : <EOL> self . SetUserCookie ( user_cookie_dict ) <EOL> self . _FinishInteractiveRequest ( ) <EOL> else : <EOL> response_dict = { '<STR_LIT>' : user_dict [ '<STR_LIT>' ] } <EOL> if device_dict is not None : <EOL> response_dict [ '<STR_LIT>' ] = device_dict [ '<STR_LIT>' ] <EOL> use_session_cookie = self . _request_message . dict . get ( '<STR_LIT>' , None ) <EOL> util . SetIfNotNone ( user_cookie_dict , '<STR_LIT>' , use_session_cookie ) <EOL> self . SetUserCookie ( user_cookie_dict ) <EOL> self . _FinishJSONRequest ( op , response_dict , json_schema . AUTH_RESPONSE ) <EOL> class AuthFormHandler ( base . BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> @ handler . asynchronous ( datastore = True ) <EOL> @ gen . engine <EOL> def get ( self ) : <EOL> current_user = self . get_current_user ( ) <EOL> if self . get_argument ( '<STR_LIT>' , None ) is not None : <EOL> self . ClearUserCookie ( ) <EOL> current_user = None <EOL> if not current_user : <EOL> self . render ( '<STR_LIT>' ) <EOL> elif not current_user . IsRegistered ( ) : <EOL> signup_ident = yield gen . Task ( current_user . QueryPrimaryIdentity , self . _client ) <EOL> is_confirmed = base . ViewfinderContext . current ( ) . IsConfirmedUser ( ) <EOL> merge_cookie = self . get_cookie ( base . _USER_COOKIE_NAME ) if is_confirmed else None <EOL> self . render ( '<STR_LIT>' , <EOL> signup_ident = signup_ident . key , <EOL> merge_cookie = merge_cookie ) <EOL> else : <EOL> self . redirect ( '<STR_LIT>' ) <EOL> class LogoutHandler ( base . BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> self . ClearUserCookie ( ) <EOL> self . redirect ( '<STR_LIT:/>' ) </s>
