auth

auth

class ochra.manager.lab.auth.auth.SessionToken(**kwargs)

Bases: Base

SessionToken model for managing user sessions.

id

The primary key of the session token.

Type:

int

user_id

The ID of the user associated with the session.

Type:

str

token

The unique session token.

Type:

str

created_at

The timestamp when the session was created.

Type:

str

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

classmethod create_session_token(user_id, db)

Create a new session token for a user.

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

  • db (Session) – The database session.

Returns:

The generated session token.

Return type:

str

created_at
classmethod get_session(db, token)

Retrieve a session by its token.

Parameters:
  • db (Session) – The database session.

  • token (str) – The session token.

Returns:

The session object if found, None otherwise.

Return type:

Optional[SessionToken]

classmethod get_user_from_session(token, db)

Get the user associated with a session token.

Parameters:
  • token (str) – The session token.

  • db (Session) – The database session.

Returns:

The user object if found, None otherwise.

Return type:

Optional[User]

id
token
user
user_id
class ochra.manager.lab.auth.auth.User(**kwargs)

Bases: Base

User model for authentication.

username

The username of the user.

Type:

str

email

The email of the user.

Type:

str

password

The hashed password of the user.

Type:

str

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

classmethod authenticate_user(username, password, db)

Authenticate a user by username and password.

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

  • password (str) – The password of the user.

  • db (Session) – The database session.

Returns:

The authenticated user object if successful, None otherwise.

Return type:

Optional[User]

email
classmethod fetch_user(db, username)

Fetch a user by username.

Parameters:
  • db (Session) – The database session.

  • username (str) – The username of the user to fetch.

Returns:

The user object if found, None otherwise.

Return type:

Optional[User]

password
username
static verify_password(plain_password, hashed_password)

Verify a plain password against a hashed password.

Parameters:
  • plain_password (str) – The plain password to verify.

  • hashed_password (str) – The hashed password to compare against.

Returns:

True if the passwords match, False otherwise.

Return type:

bool

ochra.manager.lab.auth.auth.get_db()

Yields a database session and ensures it is closed after use.

Return type:

Session

ochra.manager.lab.auth.auth.init_user_db()

Initialize the user database. Creates tables if they do not exist.

Return type:

None