<s> """<STR_LIT>""" <EOL> from bottle import request , response , template , local , redirect , default_app , get , post <EOL> from codalab . lib import crypt_util , spec_util <EOL> from codalab . lib . server_util import redirect_with_query <EOL> from codalab . lib . spec_util import NAME_REGEX <EOL> from codalab . objects . user import User <EOL> from codalab . common import UsageError <EOL> from codalab . server . authenticated_plugin import AuthenticatedPlugin , UserVerifiedPlugin <EOL> from codalab . server . cookie import LoginCookie <EOL> def send_verification_key ( username , email , key ) : <EOL> hostname = request . get_header ( '<STR_LIT>' ) or request . get_header ( '<STR_LIT>' ) <EOL> local . emailer . send_email ( <EOL> subject = "<STR_LIT>" , <EOL> body = template ( '<STR_LIT>' , user = username , current_site = hostname , key = key ) , <EOL> recipient = email , <EOL> ) <EOL> @ get ( '<STR_LIT>' , name = '<STR_LIT>' , skip = UserVerifiedPlugin ) <EOL> def do_logout ( ) : <EOL> LoginCookie . clear ( ) <EOL> redirect_uri = request . query . get ( '<STR_LIT>' ) <EOL> return redirect ( redirect_uri ) <EOL> @ post ( '<STR_LIT>' ) <EOL> def do_login ( ) : <EOL> success_uri = request . forms . get ( '<STR_LIT>' ) <EOL> error_uri = request . forms . get ( '<STR_LIT>' ) <EOL> username = request . forms . get ( '<STR_LIT:username>' ) <EOL> password = request . forms . get ( '<STR_LIT:password>' ) <EOL> user = local . model . get_user ( username = username ) <EOL> if not ( user and user . check_password ( password ) ) : <EOL> return redirect_with_query ( error_uri , { <EOL> "<STR_LIT:error>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : success_uri , <EOL> } ) <EOL> local . model . update_user_last_login ( user . user_id ) <EOL> cookie = LoginCookie ( user . user_id , max_age = <NUM_LIT:30> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> cookie . save ( ) <EOL> if success_uri : <EOL> return redirect ( success_uri ) <EOL> else : <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> @ post ( '<STR_LIT>' ) <EOL> def do_signup ( ) : <EOL> if request . user : <EOL> return redirect ( default_app ( ) . get_url ( '<STR_LIT:success>' , message = "<STR_LIT>" ) ) <EOL> success_uri = request . forms . get ( '<STR_LIT>' ) <EOL> error_uri = request . forms . get ( '<STR_LIT>' ) <EOL> username = request . forms . get ( '<STR_LIT:username>' ) <EOL> password = request . forms . get ( '<STR_LIT:password>' ) <EOL> email = request . forms . get ( '<STR_LIT:email>' ) <EOL> errors = [ ] <EOL> if request . forms . get ( '<STR_LIT>' ) != password : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if not spec_util . NAME_REGEX . match ( username ) : <EOL> errors . append ( "<STR_LIT>" ) <EOL> try : <EOL> User . validate_password ( password ) <EOL> except UsageError as e : <EOL> errors . append ( e . message ) <EOL> if not spec_util . BASIC_EMAIL_REGEX . match ( email ) : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if local . model . user_exists ( username , email ) : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if not NAME_REGEX . match ( username ) : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if errors : <EOL> return redirect_with_query ( error_uri , { <EOL> '<STR_LIT:error>' : '<STR_LIT:U+0020>' . join ( errors ) , <EOL> '<STR_LIT>' : success_uri , <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT:username>' : username , <EOL> } ) <EOL> _ , verification_key = local . model . add_user ( username , email , password ) <EOL> send_verification_key ( username , email , verification_key ) <EOL> return redirect_with_query ( success_uri , { <EOL> '<STR_LIT:email>' : email <EOL> } ) <EOL> @ get ( '<STR_LIT>' , skip = UserVerifiedPlugin ) <EOL> def do_verify ( key ) : <EOL> if local . model . verify_user ( key ) : <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> return redirect ( '<STR_LIT>' ) <EOL> @ get ( '<STR_LIT>' , name = '<STR_LIT>' , skip = UserVerifiedPlugin ) <EOL> def resend_key ( ) : <EOL> if request . user . is_verified : <EOL> return redirect ( '<STR_LIT>' ) <EOL> key = local . model . get_verification_key ( request . user . user_id ) <EOL> send_verification_key ( request . user . user_name , request . user . email , key ) <EOL> return redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT:email>' : request . user . email , <EOL> } ) <EOL> @ get ( '<STR_LIT>' , apply = AuthenticatedPlugin ( ) , skip = UserVerifiedPlugin ) <EOL> def whoami ( ) : <EOL> info = request . user . to_dict ( ) <EOL> del info [ '<STR_LIT:password>' ] <EOL> return info <EOL> @ get ( '<STR_LIT>' , skip = UserVerifiedPlugin ) <EOL> def css ( ) : <EOL> response . content_type = '<STR_LIT>' <EOL> if request . user is None : <EOL> return template ( '<STR_LIT>' ) <EOL> else : <EOL> return template ( '<STR_LIT>' , username = request . user . user_name ) <EOL> @ post ( '<STR_LIT>' ) <EOL> def request_reset ( ) : <EOL> """<STR_LIT>""" <EOL> email = request . forms . get ( '<STR_LIT:email>' ) <EOL> user = local . model . get_user ( username = email ) <EOL> if user is None : <EOL> return redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT:error>' : "<STR_LIT>" % email <EOL> } ) <EOL> reset_code = local . model . new_user_reset_code ( user . user_id ) <EOL> hostname = request . get_header ( '<STR_LIT>' ) or request . get_header ( '<STR_LIT>' ) <EOL> user_name = user . first_name or user . user_name <EOL> local . emailer . send_email ( <EOL> subject = "<STR_LIT>" , <EOL> body = template ( '<STR_LIT>' , user = user_name , current_site = hostname , code = reset_code ) , <EOL> recipient = email , <EOL> ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> @ get ( '<STR_LIT>' ) <EOL> def verify_reset_code ( code ) : <EOL> """<STR_LIT>""" <EOL> if local . model . get_reset_code_user_id ( code , delete = False ) is not None : <EOL> redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:code>' : code , <EOL> } ) <EOL> else : <EOL> redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : False , <EOL> } ) <EOL> @ post ( '<STR_LIT>' ) <EOL> def reset_password ( ) : <EOL> """<STR_LIT>""" <EOL> code = request . forms . get ( '<STR_LIT:code>' ) <EOL> password = request . forms . get ( '<STR_LIT:password>' ) <EOL> confirm_password = request . forms . get ( '<STR_LIT>' ) <EOL> if confirm_password != password : <EOL> return redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT:error>' : "<STR_LIT>" <EOL> } ) <EOL> try : <EOL> User . validate_password ( password ) <EOL> except UsageError as e : <EOL> return redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT:error>' : e . message <EOL> } ) <EOL> user_id = local . model . get_reset_code_user_id ( code , delete = True ) <EOL> if user_id is None : <EOL> return redirect_with_query ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : False , <EOL> } ) <EOL> user_info = local . model . get_user_info ( user_id ) <EOL> user_info [ '<STR_LIT:password>' ] = User . encode_password ( password , crypt_util . get_random_string ( ) ) , <EOL> local . model . update_user_info ( user_info ) <EOL> return redirect ( '<STR_LIT>' ) </s>
