def __init__(self):
     '''
     Constructor
     '''
     self.storage = UserDataStoreFactory().get_storage()
     self.signup_validation = UserSignupValidation()
     self.login_validation = LoginValidation()
     self.crypt = Crypt()
class UserService(object):
    '''
    User service
    '''
    

    def __init__(self):
        '''
        Constructor
        '''
        self.storage = UserDataStoreFactory().get_storage()
        self.signup_validation = UserSignupValidation();
        self.login_validation = LoginValidation();
        self.crypt = Crypt()
        
    def register(self, username,password,verify, email=None):
        validation_msgs, isValid = self.signup_validation.validate(username=username, password=password, verify=verify, email=email)
        if not isValid:
            raise UserRegistrationError(validation_msgs)
        else:
            crypt_password = self._crypt_password(username, password)
            user = UserData(username=username,password=crypt_password,email=email)
            saved_user = self.storage.save(user)
            user_id = saved_user.user_id
        return self.crypt.make_cookie(user_id)
    
    def login(self,username,password, cookie):
        self.check_cookie(cookie)
        validation_msgs, login_ok = self.login_validation.validate(username, password)
        if not login_ok:
            raise LoginError(validation_msgs)
        user = self.storage.fetchByUsername(username)
        user_id = user.user_id
        return self.crypt.make_cookie(user_id)

    def welcome(self, cookie):
        self.check_cookie(cookie)
        user_id = cookie.split('|')[0]
        user = self.storage.fetch(int(user_id))
        return user
    
    def check_cookie(self, cookie):
        if not cookie:
            raise InvalidCookieError()
        else: 
            cookie_ok = self.crypt.is_valid_cookie(cookie)
            if not cookie_ok:
                raise InvalidCookieError()
        
    
    def _crypt_password(self, username, password):
        return self.crypt.crypt_password(username, password)
    
    def _hash_user_id(self, user_id):
        return self.crypt.crypt_user_id(user_id)
        
class UserService(object):
    '''
    User service
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.storage = UserDataStoreFactory().get_storage()
        self.signup_validation = UserSignupValidation()
        self.login_validation = LoginValidation()
        self.crypt = Crypt()

    def register(self, username, password, verify, email=None):
        validation_msgs, isValid = self.signup_validation.validate(
            username=username, password=password, verify=verify, email=email)
        if not isValid:
            raise UserRegistrationError(validation_msgs)
        else:
            crypt_password = self._crypt_password(username, password)
            user = UserData(username=username,
                            password=crypt_password,
                            email=email)
            saved_user = self.storage.save(user)
            user_id = saved_user.user_id
        return self.crypt.make_cookie(user_id)

    def login(self, username, password, cookie):
        self.check_cookie(cookie)
        validation_msgs, login_ok = self.login_validation.validate(
            username, password)
        if not login_ok:
            raise LoginError(validation_msgs)
        user = self.storage.fetchByUsername(username)
        user_id = user.user_id
        return self.crypt.make_cookie(user_id)

    def welcome(self, cookie):
        self.check_cookie(cookie)
        user_id = cookie.split('|')[0]
        user = self.storage.fetch(int(user_id))
        return user

    def check_cookie(self, cookie):
        if not cookie:
            raise InvalidCookieError()
        else:
            cookie_ok = self.crypt.is_valid_cookie(cookie)
            if not cookie_ok:
                raise InvalidCookieError()

    def _crypt_password(self, username, password):
        return self.crypt.crypt_password(username, password)

    def _hash_user_id(self, user_id):
        return self.crypt.crypt_user_id(user_id)
Example #4
0
class LoginValidation():
    
    def __init__(self):
        factory = UserDataStoreFactory()
        self.user_datastore = factory.get_storage()
        self.crypt = Crypt()
        pass
                
    def validate(self, username, password):
        validation_messages = {} 
        is_valid = False
        if not self.is_valid_login(username, password):
            is_valid = False
            validation_messages[VERIFICATION_MESSAGES_KEYS[5]] = VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[5]]           
        else:
            is_valid = True
        return validation_messages, is_valid 
   
    def is_valid_login(self, username, password):
        is_valid = False
        user = self.user_datastore.fetchByUsername(username)
        if user:
            user_pwd_hashed = user.password
            if self.crypt.is_password_valid(username, password, user_pwd_hashed):
                is_valid = True
        return is_valid 
 def __init__(self):
     '''
     Constructor
     '''
     self.storage = UserDataStoreFactory().get_storage()
     self.signup_validation = UserSignupValidation();
     self.login_validation = LoginValidation();
     self.crypt = Crypt()
Example #6
0
 def __init__(self):
     factory = UserDataStoreFactory()
     self.user_datastore = factory.get_storage()
     self.crypt = Crypt()
     pass