yezz123 / authx

Ready-to-use and customizable Authentications and Oauth2 management for FastAPI ✨
https://authx.yezz.me/
MIT License
796 stars 48 forks source link

Wrong username validation UserInRegister model #237

Closed YogeshUpdhyay closed 2 years ago

YogeshUpdhyay commented 2 years ago

First Check

Example Code

from fastapi import FastAPI
from authx import Authentication, MongoDBBackend
from authx.services.auth import AuthService
from authx.core.jwt import JWTBackend
from authx.backend import UsersRepo
import uvicorn

def app():
    app = FastAPI()

    auth = Authentication(
        debug=True,
        base_url="http://localhost:8000",
        site="http://localhost:8000",
        database_backend=MongoDBBackend(database_name="TravelMonk"),
        callbacks=["http://localhost:8000/callback"],
        access_cookie_name="access_token",
        refresh_cookie_name="refresh_token",
        private_key="private.pem",
        public_key="public.pem",
        access_expiration=3600,
        refresh_expiration=86400,
        smtp_username=None,
        smtp_host=None,
        smtp_password=None,
        smtp_tls=False,
        display_name="travelmonk",
        recaptcha_secret=None,
        social_creds=None,
        social_providers=None,
    )

    app.include_router(auth.auth_router, prefix="/api/users")
    return app

if __name__ == "__main__":
    app = app()
    uvicorn.run(app, host="0.0.0.0", port=8000, debug=True)

Description

Call the endpoint /register With the request body as

{
    "email": "johndoe@email.com",
    "username": "johndoe",
    "password1": "password",
    "password2": "password"
}

It should set access_token and return_token

But it returns

{
    "detail": "Username must be only in latin or cyrillic"
}

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.77.1

Python Version

3.9

Additional Context

The problem possibly is in the check_username method in the file authx/models/user.py

def check_username(v: str) -> str:
    v = v.strip()
    if len(v) < USERNAME_MIN_LENGTH or len(v) > USERNAME_MAX_LENGTH:
        raise ValueError("username length")
    for letter in v:
        if letter not in USERNAME_CHARS:
            raise ValueError("username special characters")
    if v in WRONG_USERNAMES:
        raise ValueError("username wrong")
    if any(letter in ascii_letters for letter in v):
        raise ValueError("username different letters")
    return v

The last if clause should be

if any(letter not in ascii_letters for letter in v):
        raise ValueError("username different letters")
yezz123 commented 2 years ago

Hello @YogeshUpdhyay Thank you for your report, I will check this issue related to it and back to you, Mostly it's coming from the if as you said i will try to let it default and see the issue if its raise again