Example #1
0
def delete_user_by_account_admin(token, username, email, **kwargs):
    try:
        data = jwt.decode(token, config.FLASK_APP_SECRET_KEY)
        user_admin = repositories.user.find_user_by_id(data['userid'])
        if user_admin.is_admin:
            if (
                user_admin.username != username
                or
                user_admin.email != email
            ):
                user = repositories.user.find_user_by_username_and_email(username=username, email=email)
                if (user):
                    repositories.user.delete_one_by_email_or_username_in_user(user=user)
                    return {
                        'message': 'success'
                    }
                else:
                    raise exceptions.BadRequestException("Not found user")
            else:
                raise  exceptions.BadRequestException("This is your account, delete error")

        else:
            raise exceptions.UnAuthorizedException(message='not authorized')
    except jwt.ExpiredSignature:
        repositories.usertoken.delete_token_by_tokenstring(token)
        raise exceptions.UnAuthorizedException('expired token, auto logout')
    except jwt.exceptions.InvalidTokenError:
        raise exceptions.BadRequestException('Invalid Token')
Example #2
0
 def inner(token):
     try:
         data = jwt.decode(token, config.FLASK_APP_SECRET_KEY)
         user = repositories.user.find_user_by_id(data['userid'])
         if user.is_admin:
             return func(token)
         else:
             raise exceptions.UnAuthorizedException(
                 message='not authorized')
     except jwt.ExpiredSignature:
         repositories.usertoken.delete_token_by_tokenstring(token)
         raise exceptions.UnAuthorizedException(
             'expired token, auto logout')
     except jwt.exceptions.InvalidTokenError:
         raise exceptions.BadRequestException('Invalid Token')
Example #3
0
def check_maintain_login(tokenstring=""):
    try:
        token_data = jwt.decode(tokenstring, config.FLASK_APP_SECRET_KEY)
        return {"message": "still valid"}
    except jwt.ExpiredSignature:
        repositories.usertoken.delete_token_by_tokenstring(tokenstring)
        raise exceptions.UnAuthorizedException('expired token, auto logout')
Example #4
0
def decode(access_token):
    try:
        return jwt.decode(access_token, config.FLASK_APP_SECRET_KEY)
    except jwt.ExpiredSignature:
        r.usertoken.delete.by_token_string(access_token)
        raise e.UnAuthorizedException('expired token, auto logout')
    except jwt.exceptions.InvalidTokenError:
        raise e.BadRequestException('invalid token')
Example #5
0
def eraser_id_is_admin(access_token):
    token_data = token.decode(token=access_token)
    user_id = token_data['user_id']
    user = r.user.find.by_id(user_id=user_id)
    if user.is_admin:
        return user_id
    else:
        raise e.UnAuthorizedException(message='not authorized')
Example #6
0
def do_not_lock_myself(
    locker_id,
    user_id,
):
    if locker_id == user_id:
        raise e.UnAuthorizedException(
            message='This is your account, lock error')
    else:
        return True
Example #7
0
def exist_account(username='', password=''):
    account = r.user.find.by_username(username=username)
    if account:
        if account.check_password(password):
            return account
        else:
            raise exceptions.BadRequestException(message="Password invalid")
    else:
        raise exceptions.UnAuthorizedException(message="Not found user")
Example #8
0
def make_response(access_token="", password="", new_password="", **kwargs):
    token_data = token.decode(access_token=access_token)
    user_id = token_data["user_id"]
    user = check.exist_account(user_id=user_id)
    if check.password(password=password, user=user):
        return pw.update(
            new_password=new_password,
            user=user,
        )
    else:
        raise e.UnAuthorizedException(message="Password invalid")
Example #9
0
def check_token_from_logout_request(tokenstring):
    try:
        jwt.decode(tokenstring, config.FLASK_APP_SECRET_KEY)
        repositories.usertoken.delete_token_by_tokenstring(tokenstring)
        return {
                "message": "logout success",
            }
    except jwt.ExpiredSignature:
        repositories.usertoken.delete_token_by_tokenstring(tokenstring)
        raise exceptions.UnAuthorizedException('expired token, auto logout')
    except jwt.exceptions.InvalidTokenError:
        raise exceptions.BadRequestException('Invalid Token')
Example #10
0
def decode(access_token):
    try:
        token_data = jwt.decode(access_token, config.FLASK_APP_SECRET_KEY)
        return token_data
    except jwt.ExpiredSignature:
        delete_signup_request = r.signup.delete.by_token(access_token)
        if delete_signup_request:
            raise exceptions.UnAuthorizedException('expired token, delete account')
        else:
            raise exceptions.BadRequestException('database error')
    except jwt.exceptions.InvalidTokenError:
        raise exceptions.BadRequestException('Invalid Token')
Example #11
0
def make_response(token, user_id, **kwargs):
    eraser_id = check.eraser_id_is_admin(access_token=token)
    if (check.do_not_delete_your_self(
            eraser_id=eraser_id,
            user_id=user_id,
    )):
        user = check.user_exist(user_id=user_id)
        if user.is_admin:
            raise e.UnAuthorizedException(message='un authorized')
        else:
            r.user.delete.self(user=user)
            return {'msg': 'success'}
Example #12
0
def check_info_from_login_request(username, password, **kwargs):
    user = repositories.user.find_user_by_username(username)
    if user is None:
        raise exceptions.UnAuthorizedException(message="Not found user")
    else:
        if(user.check_password(password)):
            # function add token
            if helpers.verify_look_account_by_user(user):
                user_token = repositories.usertoken.create_token_by_user(user)
                if user_token is None:
                    raise exceptions.UnAuthorizedException(message="Don't insert token")
                else:
                    timestr =  datetime.timestamp(user_token.expired_time)
                    return {
                        'token': user_token.token,
                        'expired_time': timestr,
                        'isAdmin': user.is_admin,
                    }
            else:
                raise exceptions.UnAuthorizedException(message="Account locked")
        else:
            raise exceptions.BadRequestException("Password invalid") 
Example #13
0
def update(new_password, user):
    if check.history_password(user_id=user.id, new_password=new_password):
        r.historypasschange.add.by_user_id_and_password(user_id=user.id,
                                                        password=new_password,
                                                        is_real_pass=False)
        r.historypasschange.delete.more_than_five_data(user_id=user.id)
        r.user.edit.password(
            user=user,
            new_password=new_password,
        )
        return {'message': 'success'}
    else:
        raise e.UnAuthorizedException(
            message="You have retyped the same password 5 times")
Example #14
0
def update_password(newpassword, user):
    if user.check_password(newpassword):
        return False
    else:
        if r.checkhistorypass.check_history_pass_when_change(
                user.id, newpassword):
            r.checkhistorypass.save_history_pass(user.id, newpassword, False)
            r.checkhistorypass.delete_old_password(user.id)
            user.password = newpassword
            models.db.session.add(user)
            models.db.session.commit()
            return True
        else:
            raise exceptions.UnAuthorizedException(
                message="You have retyped the same password 5 times")
Example #15
0
def check_info_and_res(token="", password="", newpassword="", **kwarg):
    try:
        token_data = jwt.decode(token, config.FLASK_APP_SECRET_KEY)
    except jwt.ExpiredSignature:
        repositories.usertoken.delete_token_by_tokenstring(token)
        raise exceptions.UnAuthorizedException('expired token, auto logout')
    except jwt.exceptions.InvalidTokenError:
        raise exceptions.BadRequestException('Invalid Token')

    user_id = token_data["userid"]
    user = repositories.user.find_user_by_id(user_id)
    if user is None:
        raise exceptions.BadRequestException("User not exist!")
    else:
        if (user.check_password(password)):
            if repositories.changepassword.update_password(newpassword, user):
                return {
                    "message": "Change password success",
                }
            else:
                raise exceptions.UnAuthorizedException(
                    message="new password equal password")
        else:
            raise exceptions.UnAuthorizedException(message="Password invalid")
Example #16
0
def make_response(username, password):
    user = check.exist_account(
        username=username,
        password=password,
    )
    if h.verify.lockaccount.by_user(
        user=user
    ):
        user_token = r.usertoken.add.by_user_model(
            user=user
        )
        return {
            'token': user_token.token,
            'isAdmin': user.is_admin,
        }
    else:
        raise e.UnAuthorizedException(message="Account locked")
Example #17
0
def make_response(username, email, **kwargs):
    user = check.exist_account(
        username=username,
        email=email,
    )
    if h.verify.lockaccount.by_user(user=user):
        new_password = password.create_new(user=user)
        content_mail = "Your Password: "******"Reset Password", content_mail, email)
        if check_password:
            return {
                'message':
                'Reset password success. You can check mail: ' + email,
            }
        else:
            raise exceptions.ForbiddenException(message="Send mail error")
    else:
        raise exceptions.UnAuthorizedException(message="Account locked")
Example #18
0
def check_info_form_resetpassword_and_res(username, email, **kwargs):
    user = repositories.user.find_user_by_username_and_email(username, email)
    if user is None:
        raise exceptions.BadRequestException("user not exist!")
    else:
        #update value user in database
        if helpers.verify_look_account_by_user(user):
            newpassword = repositories.resetpassword.change_password(user)
            content_mail = "Your Password: "******"Reset Password", content_mail, email)
            if check_password:
                return {
                    'message': 'Reset password success. You can check mail: ' + email,
                }
            else:
                raise exceptions.ForbiddenException(message="Send mail error")
        else:
            raise exceptions.UnAuthorizedException(message="Account locked")
Example #19
0
def decode(access_token=''):
    try:
        jwt.decode(access_token, config.FLASK_APP_SECRET_KEY)
        if r.usertoken.delete.by_token_string(
            token_string=access_token
        ):
            return {
                "message": "logout success",
            }
        else:
            raise exceptions.BadRequestException('token not exist')
    except jwt.ExpiredSignature:
        r.usertoken.delete.by_token_string(
            token_string=access_token
        )
        raise exceptions.UnAuthorizedException('expired token, auto logout')
    except jwt.exceptions.InvalidTokenError:
        raise exceptions.BadRequestException('Invalid Token')
Example #20
0
def make_response(token):
    users = r.user.find.all()
    if users:
        response = []
        for user in users:
            datetime = str(user.updated_at)
            e = {
                'user_id': user.id,
                'username': user.username,
                'email': user.email,
                'updated_at': datetime,
                'is_admin': user.is_admin,
                'is_active': user.is_active,
            }
            response.append(e)
        return response
    else:
        raise exceptions.UnAuthorizedException('Server error')
Example #21
0
def verify(token_string):
    try:
        token_data = jwt.decode(token_string, config.FLASK_APP_SECRET_KEY)
    except jwt.ExpiredSignature:
        check_del_signup_request = repositories.signup.delete_by_token_in_signup_request(
            token_string)
        if check_del_signup_request:
            raise exceptions.UnAuthorizedException(
                'expired token, delete account')
        else:
            raise exceptions.BadRequestException('database error')
    except jwt.exceptions.InvalidTokenError:
        raise exceptions.BadRequestException('Invalid Token')

    username = token_data["username"]
    user = repositories.signup.find_one_by_email_or_username_in_signup_request(
        email="", username=username)
    if user:
        repositories.signup.delete_one_by_email_or_username_in_signup_request(
            user)
        now = datetime.timestamp(datetime.now())
        expired = datetime.timestamp(user.expired_time)
        if expired - now >= 0:
            #function add info to user and delete Signup_Request
            user_in_tb_user = repositories.signup.save_user_to_user(
                username=user.username,
                email=user.email,
                password=user.password_hash)
            if user_in_tb_user is None:
                raise exceptions.BadRequestException("database error")
            else:
                repositories.checkhistorypass.save_history_pass(
                    user_in_tb_user.id, user_in_tb_user.password_hash, True)
                return {
                    'message': 'success',
                }
    raise exceptions.NotFoundException(message="not found user")
Example #22
0
def password(password, user):
    if user.check_password(password=password):
        return True
    else:
        raise e.UnAuthorizedException(message="Password invalid")