Beispiel #1
0
def reset_password(
        token: str = Body(...),
        new_password: str = Body(...),
        db: Session = Depends(deps.get_db),
) -> Any:
    """
    Reset password
    :return:
    """
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = crud.user.get_by_email(db, email=email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    hashed_password = get_password_hash(new_password)
    user.hashed_password = hashed_password
    db.add(user)
    db.commit()
    return {"msg": "Password updated successfully"}
Beispiel #2
0
async def reset_password(token: str = Body(...), new_password: str = Body(...)):
    """
    Reset password [NOT IMPLEMENTED YET]
    """
    logging.info(">>> " + __name__ + ":" + reset_password.__name__ )
    username = verify_password_reset_token(token)
    if not username:
        raise HTTPException(status_code=400, detail="Invalid token")

    db =  get_database()
    user = await crud.get(db, username)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )

    # NOT IMPLEMENTED YET
    # elif not crud.user.is_active(user):
    #     raise HTTPException(status_code=400, detail="Inactive user")
    # user_in = UserUpdate(name=username, password=new_password)
    # user = crud.user.update(bucket, username=username, user_in=user_in)
    logging.info("FULL NAME: " + user['full_name'])
    hashed_password = get_password_hash(password=new_password)
    collection = get_collection(db, DOCTYPE_USER)
    rs = await collection.update_one(
        {"username": username},
        {'$set': {
            'hashed_password': hashed_password,
            'modified': datetime.utcnow()
            }
        }
    )

    return {"msg": "Password updated successfully"}
async def reset_password(
        token: str = Body(...),
        new_password: str = Body(...),
        db: Session = Depends(deps.get_db),
        redis: aioredis.Redis = Depends(deps.get_redis),
) -> Any:
    """
    Reset password
    """
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = await crud.user_cachedb.get_by_email(db, redis, email=email)
    if user is None:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not user.is_active:
        raise HTTPException(status_code=401, detail="Inactive user")
    user = await crud.user_cachedb.update(db,
                                          redis,
                                          cache_obj=user,
                                          obj_in={"password": new_password})
    return {"msg": "Password updated successfully"}
Beispiel #4
0
def reset_password(
        token: str = Body(...),
        new_password: str = Body(...),
        db: Session = Depends(deps.get_db),
) -> Any:
    """
    Reset password
    """
    if email := verify_password_reset_token(token):
        if user := crud.user.get_by_email(db, email=email):
            if crud.user.is_active(user):
                hashed_password = get_password_hash(new_password)
                user.hashed_password = hashed_password
                db.add(user)
                db.commit()
                return {"msg": "Password updated successfully"}
            raise BadRequestException(detail="Inactive user")
Beispiel #5
0
def route_reset_password(token, new_password):
    name = verify_password_reset_token(token)
    if not name:
        abort(400, "Invalid token")
    bucket = get_default_bucket()
    user = get_user(bucket, name)
    if not user:
        return abort(
            404, f"The user with this username does not exist in the system.")
    elif not check_if_user_is_active(user):
        abort(400, "Inactive user")
    user_in = UserInUpdate(name=name, password=new_password)
    user = update_user(bucket, user_in)
    return {"msg": "Password updated successfully"}
Beispiel #6
0
def reset_password(
        # message_email 前端绑定值
        message_code: str = Body(...),
        message_email: str = Body(...),
        new_password: str = Body(...),
        db: Session = Depends(deps.get_db),
) -> Any:
    """
    Reset password
    """
    user = crud.user.get_by_email(db, email=message_email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="用户不存在与系统中",
        )
    try:
        key = red.get(message_email).decode('ascii')
    except Exception:
        raise HTTPException(
            status_code=404,
            detail="请先点击发送验证码",
        )

    token = json.loads(key).get(message_code)
    if not token:
        raise HTTPException(
            status_code=404,
            detail="短信验证码错误",
        )
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")

    if not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")

    # 是否允许修改管理员找回自己的密码 默认允许
    # if crud.user.is_superuser(user):
    #     raise HTTPException(status_code=400, detail="管理员更改???")

    hashed_password = get_password_hash(new_password)
    user.hashed_password = hashed_password
    db.add(user)
    db.commit()
    return {"msg": "Password updated successfully"}
Beispiel #7
0
def reset_password(token: str, new_password: str):
    """
    Reset password
    """
    username = verify_password_reset_token(token)
    if not username:
        raise HTTPException(status_code=400, detail="Invalid token")
    bucket = get_default_bucket()
    user = crud.user.get(bucket, username=username)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    user_in = UserInUpdate(name=username, password=new_password)
    user = crud.user.update(bucket, username=username, user_in=user_in)
    return {"msg": "Password updated successfully"}
def reset_password(token: str = Body(...), new_password: str = Body(...)):
    """
    Reset password
    """
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = crud.user.get_by_email(email=email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    hashed_password = get_password_hash(new_password)
    user.hashed_password = hashed_password
    user.save()
    return {"msg": "Password updated successfully"}
async def reset_password(
        token: str = Body(...),
        new_password: str = Body(...),
) -> Any:
    """
    Reset password
    """
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = await crud.user.get_by_email(email=email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    await crud.user.update(db_obj=user, obj_in={'password': new_password})
    return {"msg": "Password updated successfully"}
Beispiel #10
0
def reset_password(token: str, db: Session = Depends(deps.get_db),) -> Any:
    """
    Reset password
    """
    email_password_tuple = verify_password_reset_token(token)
    if not email_password_tuple:
        raise HTTPException(status_code=400, detail="Invalid token")
    email, new_password = email_password_tuple
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = services.user.get_user_by_email(email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    hashed_password = get_password_hash(new_password)
    user.password = hashed_password
    services.user.update_user(user.provider_uuid, user.uuid, user)
    return {"msg": "Password updated successfully"}
Beispiel #11
0
async def reset_password(
    token: str = Body(...),
    new_password: str = Body(...),
    db: Database = Depends(get_async_db),
):
    """
    Reset password
    """
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = user_crud.get_by_email(db, email=email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not user_crud.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    hashed_password = get_password_hash(new_password)
    user.hashed_password = hashed_password

    await db.execute(insert(UserORM), **user.__dict__)
    return {"msg": "Password updated successfully"}
Beispiel #12
0
def test_verify_password_reset_token():
    test_email = '*****@*****.**'
    token = generate_password_reset_token(test_email)
    email = verify_password_reset_token(token)
    assert test_email == email
@router.post("/reset-password/", response_model=Msg)
def reset_password(
    token: str = Body(...), new_password: str = Body(...), db: Session = Depends(get_db)
<<<<<<< HEAD
    ):
        """
        Reset password
        """
=======
):
    """
    Reset password
    """
>>>>>>> upstream/master
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = crud.user.get_by_email(db, email=email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    hashed_password = get_password_hash(new_password)
    user.hashed_password = hashed_password
    db.add(user)
    db.commit()
    return {"msg": "Password updated successfully"}