inveniosoftware / invenio-accounts

Invenio module for managing user accounts.
https://invenio-accounts.readthedocs.io
MIT License
6 stars 67 forks source link

User registration: usernames used for validation and storage differ #466

Open daglueck opened 10 months ago

daglueck commented 10 months ago

Package version (if known): v3.5.0

Describe the bug

Upon user registration, invenio_userprofiles.ProfileForm validates the username and whether the user already exists in the database. For this, it uses the user name basically as provided by the registration form (field.data). However, usernames seem to be lowercased upon storage into the DB. This creates a divergence between what is being checked and what is actually stored into the database.

For example, let's assume there is a user with username myuser in the DB. If someone now tries to create a new user MyUser, validation will pass through, but the DB model will turn the user name into myuser which will result in:

sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "uq_accounts_user_username"
DETAIL:  Key (username)=(myuser) already exists.

Steps to Reproduce

  1. On an InvenioRDM site (not logged into any user account), open the registration form ( /signup ).
  2. Create a user with certain credentials.
  3. Log out of the user account.
  4. Again in the registration form (/signup), create a new user where the user name is similar to the one used in 2., but where at least one of the characters is different in that it is upper or lower case. Also use a different email address than the one used in 2., otherwise the error won't surface due to email address validation failing (email address already being used).
  5. Submit the data.
  6. On the client side, an "Internal server error" or something alike will occur. On the server side, there is a sqlalchemy.exc.IntegrityError (as mentioned above).

Expected behavior

Username validation should operate with the exact same string as the one being used for final username ingestion. That is, either should the username be lowercased upon validation, or it should be stored as is (as provided by the user) into the DB.

Screenshots (if applicable)

Additional context