<s> import api . user <EOL> import bcrypt <EOL> from api . exceptions import * <EOL> from api . schemas import check , verify_to_schema <EOL> from flask import session <EOL> from voluptuous import Schema , Required , Length <EOL> UserLoginSchema = Schema ( { <EOL> Required ( '<STR_LIT:username>' ) : check ( <EOL> ( [ str , Length ( min = <NUM_LIT:3> , max = <NUM_LIT:50> ) ] , "<STR_LIT>" ) , <EOL> ) , <EOL> Required ( '<STR_LIT:password>' ) : check ( <EOL> ( [ str , Length ( min = <NUM_LIT:3> , max = <NUM_LIT:50> ) ] , "<STR_LIT>" ) <EOL> ) <EOL> } ) <EOL> def confirm_password ( attempt , actual ) : <EOL> return bcrypt . hashpw ( attempt , actual ) == actual <EOL> def login ( username , password ) : <EOL> verify_to_schema ( UserLoginSchema , { "<STR_LIT:username>" : username , "<STR_LIT:password>" : password } ) <EOL> user = api . user . get_user ( username_lower = username . lower ( ) ) <EOL> if user is None : <EOL> raise WebException ( "<STR_LIT>" ) <EOL> if user . get ( "<STR_LIT>" , False ) : <EOL> raise WebException ( "<STR_LIT>" ) <EOL> if confirm_password ( password , user [ "<STR_LIT:password>" ] ) : <EOL> if user [ "<STR_LIT>" ] is not None : <EOL> session [ "<STR_LIT>" ] = user [ "<STR_LIT>" ] <EOL> if user [ "<STR_LIT:type>" ] == <NUM_LIT:0> : <EOL> session [ "<STR_LIT>" ] = True <EOL> session . permanent = True <EOL> else : <EOL> raise WebException ( "<STR_LIT>" ) <EOL> else : <EOL> raise WebException ( "<STR_LIT>" ) <EOL> def logout ( ) : <EOL> session . permanent = False <EOL> if "<STR_LIT>" in session : session . pop ( "<STR_LIT>" ) <EOL> session [ "<STR_LIT>" ] = False <EOL> if "<STR_LIT>" in session : del session [ "<STR_LIT>" ] <EOL> session . clear ( ) <EOL> def is_logged_in ( ) : <EOL> logged_in = "<STR_LIT>" in session <EOL> if logged_in and not api . user . get_user ( uid = session [ "<STR_LIT>" ] ) : <EOL> logout ( ) <EOL> return False <EOL> return logged_in <EOL> def get_uid ( ) : <EOL> if is_logged_in ( ) : <EOL> return session [ '<STR_LIT>' ] <EOL> else : <EOL> return None </s>
