[Python] FastAPI does not allow double quotation as a special character in password [duplicate]

Discussão em 'Python' iniciado por Stack, Janeiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    This is the route to change password in my application:

    @auth_router.patch('/change-password', response_model=UserSchema)
    async def change_password(change_password_schema: ChangePasswordSchema, db: Annotated[AsyncSession, Depends(get_db)],
    user: Annotated[User, Depends(current_user)]):
    return await auth.change_password(change_password_schema=change_password_schema, db=db, user=user)


    And this is the method with change password logic:

    async def change_password(change_password_schema: ChangePasswordSchema,
    db: Annotated[AsyncSession, Depends(get_db)],
    user: User):
    password_match = verify_password(change_password_schema.current_password, user.password)
    if not password_match:
    raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
    detail="Current password doesn't match.")

    user.password = get_password_hash(change_password_schema.new_password)
    if user.first_login:
    user.first_login = False
    db.add(user)
    await db.commit()
    await db.refresh(user)
    return user


    This is the ChangePassworSchema:

    class ChangePasswordSchema(BaseModel):
    current_password: str
    new_password: str

    @model_validator(mode='after')
    def validate_password(self):
    if self.new_password == self.current_password:
    raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
    detail="New password can't be same as old password")
    try:
    validate_password(self.new_password)
    except ValueError as e:
    raise HTTPException(
    status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(e))
    return self


    Here the problem is when the person uses the password with the sign double quotation (") in the new password then the program fails. I want to let the users use double quotation as a special character in the password. How can I do this in FastAPI? I am new to programming am I missing something very fundamental?

    When I request the endpoint with the following value:

    {
    "current_password": "Na$6Ae.5",
    "new_password": "Ml39|0"UHa.K"
    }


    it shows an error like this:

    {
    "detail": [
    {
    "type": "json_invalid",
    "loc": [
    "body",
    62
    ],
    "msg": "JSON decode error",
    "input": {},
    "ctx": {
    "error": "Expecting ',' delimiter"
    }
    }
    ]
    }

    Continue reading...

Compartilhe esta Página