user.py
 1  from pydantic import BaseModel, Field
 2  
 3  from ..utils.docs import example, get_example
 4  
 5  
 6  USERNAME_REGEX = r"^[a-zA-Z0-9]{4,32}$"
 7  PASSWORD_REGEX = r"^((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,})?$"  # noqa: S105
 8  MFA_CODE_REGEX = r"^\d{6}$"
 9  
10  
11  class User(BaseModel):
12      id: str = Field(description="Unique identifier for the user")
13      name: str = Field(description="Unique username")
14      registration: float = Field(description="Timestamp of the user's registration")
15      last_login: float | None = Field(description="Timestamp of the user's last successful login")
16      enabled: bool = Field(description="Whether the user is enabled")
17      admin: bool = Field(description="Whether the user is an administrator")
18      password: bool = Field(description="Whether the user has a password (if not, login is only possible via OAuth)")
19      mfa_enabled: bool = Field(description="Whether the user has enabled MFA")
20  
21      Config = example(
22          id="a13e63b1-9830-4604-8b7f-397d2c29955e",
23          name="user42",
24          registration=1615725447.182818,
25          last_login=1615735459.274742,
26          enabled=True,
27          admin=False,
28          password=True,
29          mfa_enabled=False,
30      )
31  
32  
33  class UsersResponse(BaseModel):
34      total: int = Field(description="Total number of users matching the query")
35      users: list[User] = Field(description="Paginated list of users matching the query")
36  
37      Config = example(total=1, users=[get_example(User)])
38  
39  
40  class CreateUser(BaseModel):
41      name: str = Field(regex=USERNAME_REGEX, description="Unique username")
42      password: str | None = Field(regex=PASSWORD_REGEX, description="Password of the user")
43      oauth_register_token: str | None = Field(description="OAuth registration token returned by `POST /sessions/oauth`")
44      recaptcha_response: str | None = Field(description="Recaptcha response (required if not requested by an admin)")
45      enabled: bool = Field(True, description="Whether the user is enabled")
46      admin: bool = Field(False, description="Whether the user is an administrator")
47  
48  
49  class UpdateUser(BaseModel):
50      name: str | None = Field(regex=USERNAME_REGEX, description="Change the username")
51      password: str | None = Field(
52          regex=PASSWORD_REGEX, description="Change the password (if set to `null`, the password is removed)"
53      )
54      enabled: bool | None = Field(description="Change whether the user is enabled")
55      admin: bool | None = Field(description="Change whether the user is an administrator")