Authorization module

Authentication module for managing user access.

Route

app.features.auth.route.delete_user_route(body)

Delete user account from the system — tags:

  • features/auth

security:
  • BearerAuth: []

requestBody:

required: true content:

application/json:
schema:

type: object required:

  • user_name

  • password

properties:
user_name:

type: string description: The user’s name example: “Test User”

password:

type: string description: Password example: “123456”

responses:
200:

description: User account successfully deleted content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: object nullable: true example: null

error:

type: string nullable: true example: null

400:

description: Validation error of incoming credentials (Pydantic) content:

application/json:
schema:

type: object properties:

validation_error:

type: object properties:

body_params:

type: array items:

type: object properties:

input:

type: string example: “”

loc:

type: array items:

type: string

example: [“password”]

msg:

type: string example: “Field required”

type:

type: string example: “missing”

401:

description: Unauthorized (missing or invalid JWT) content:

application/json:
schema:

type: object properties:

message:

type: string example: “Missing Authorization Header”

409:

description: Deletion error (invalid password or user not found) content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: object nullable: true example: null

error:

type: string example: “Invalid password or user does not exist”

Parameters:

body (AuthRequest)

app.features.auth.route.get_data_route()

Get account data — tags:

  • features/auth

security:
  • BearerAuth: []

responses:
200:

description: Successful retrieval of user data content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: object required: [user_name] properties:

user_name:

type: string description: The user’s name example: “admin”

error:

type: string nullable: true example: null

401:

description: Unauthorized (missing or invalid JWT) content:

application/json:
schema:

type: object properties:

message:

type: string example: “Missing Authorization Header”

409:

description: Conflict (logical error, for example, user not found) content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: object nullable: true example: null

error:

type: string example: “User not found”

app.features.auth.route.login_route(body)

Log in to the system and receive a JWT token — tags:

  • features/auth

requestBody:

required: true content:

application/json:
schema:

type: object required:

  • user_name

  • password

properties:
user_name:

type: string description: The user’s name example: “Test User”

password:

type: string description: Password example: “123456”

responses:
200:

description: Successful authorization content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: string description: JWT access token (Access Token) example: “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…”

error:

type: string nullable: true example: null

400:

description: Validation error (required fields are missing) content:

application/json:
schema:

type: object properties:

validation_error:

type: object properties:

body_params:

type: array items:

type: object properties:

input:

type: string example: “Test User”

loc:

type: array items:

type: string

example: [“password”]

msg:

type: string example: “Field required”

type:

type: string example: “missing”

409:

description: Authorization error (invalid username or password) content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: object nullable: true example: null

error:

type: string example: “Invalid username or password”

Parameters:

body (AuthRequest)

app.features.auth.route.signup_route(body)

Register in the system and receive a JWT token — tags:

  • features/auth

requestBody:

required: true content:

application/json:
schema:

type: object required:

  • user_name

  • password

properties:
user_name:

type: string description: The user’s name example: “Test User”

password:

type: string description: Password example: “123456”

responses:
200:

description: Successful registration and user creation content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: string description: JWT access token (Access Token) example: “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…”

error:

type: string nullable: true example: null

400:

description: Validation error (required fields are missing or empty strings) content:

application/json:
schema:

type: object properties:

validation_error:

type: object properties:

body_params:

type: array items:

type: object properties:

input:

type: string example: “”

loc:

type: array items:

type: string

example: [“user_name”]

msg:

type: string example: “String should have at least 1 character”

type:

type: string example: “string_too_short”

409:

description: Registration error (user already exists / database error) content:

application/json:
schema:

type: object required: [data, error] properties:

data:

type: object nullable: true example: null

error:

type: string example: “Username already exists”

Parameters:

body (AuthRequest)

Service

class app.features.auth.service.AuthService

Bases: object

Service for user authentication.

delete(user_name, password, user_id)

Deletes a user from the system.

Parameters:
  • user_name (str) – The name of the user.

  • password (str) – The user’s password.

  • user_id (int) – The unique ID of the user.

Returns:

The data transfer object containing deletion results.

Return type:

BaseDTO

get_user_data(user_id)

Retrieves user data.

Parameters:

user_id (int) – The unique ID of the user.

Returns:

The data transfer object containing the user data.

Return type:

BaseDTO

login(user_name, password)

Authenticates users and performs system login.

Parameters:
  • user_name (str) – The name of the user.

  • password (str) – The user’s password.

Returns:

The data transfer object containing authentication results.

Return type:

BaseDTO

signup(user_name, password)

Registers users in the system.

Parameters:
  • user_name (str) – The name of the user.

  • password (str) – The user’s password.

Returns:

The data transfer object containing registration results.

Return type:

BaseDTO

Repository

class app.features.auth.repository.AuthRepository

Bases: object

Repository for interacting with user database tables.

add_user(_user_name, _password_hash, _is_admin)

Adds a new user.

Parameters:
  • _user_name (str) – The user’s name.

  • _password_hash (str) – The password hash string.

  • _is_admin (bool) – Indicates whether the user is a server administrator.

Raises:

Exception – If the record creation fails.

count_users()

Counts the number of users.

Returns:

The total count of users.

Return type:

int

delete_user(user_name)

Deletes a user.

Parameters:

user_name (str) – The name of the user.

Raises:

Exception – If the user deletion fails.

get_first()

Returns the first user created in the database (ordered by ID).

Returns:

The first user object, or None if the table is empty.

Return type:

Optional[User]

get_user(user_name)

Returns a user from the database by name.

Parameters:

user_name (str) – The name of the user.

Returns:

The found user object.

Return type:

User

get_user_by_id(user_id)

Returns a user from the database by id.

Parameters:

user_id (int) – The id of the user.

Returns:

The found user object.

Return type:

User

set_admin(user_name)

Grants server administrator privileges to a user in the database.

Parameters:

user_name (str) – The name of the user.

Requests

class app.features.auth.requests.AuthRequest

Bases: BaseModel

Represents an authorization request payload.

Fields

Field

Type

Required

Default

Constraints

password

str

Yes

min_length=1

user_name

str

Yes

password: str
user_name: str

Responses

app.features.auth.responses.DeleteResponse

Delete user response

Fields

Field

Type

Required

Default

data

str | None

No

None

error

str | None

No

None

app.features.auth.responses.JWTResponse

Giving JWT response

Fields

Field

Type

Required

Default

data

str | None

No

None

error

str | None

No

None

class app.features.auth.responses.UserData

Bases: BaseModel

User’s data

Fields

Field

Type

Required

Default

user_name

str

Yes

user_name: str

The unique name of the user.

app.features.auth.responses.UserDataResponse

User data response

Fields

Field

Type

Required

Default

data

UserData | None

No

None

error

str | None

No

None

Consts

class app.features.auth.consts.ResultCodes

Operation execution result codes.

OK: str = 'OK'

Successful result.

USER_EXISTS_ALREADY: str = 'User already exists'

The username or email is taken during registration.

PASSWORD_INCORRECT: str = 'Incorrect password'

The provided password does not match the stored hash.

UNEXPECTED_ERROR: str = 'Unexpected error'

Various errors worth checking in logs.

USER_NOT_FOUND: str = 'User not found'

The requested user ID does not exist in the database.

DELETION_FORBIDDEN: str = 'No permission to delete'

Removal prohibited due to insufficient user privileges.

DTO

class app.features.auth.dto.UserDTO

Data Transfer Object (DTO) for user information.

user_name: str

Unique name (login) of the user in the system.