Skip to main content
userharbor

GitHub License Tests Codecov PyPI - Python Version PyPI - Version Code style: black Linting: Ruff uv Pytest Zensical

Project status: UserHarbor SQLAlchemy is currently in an early stage of development. The API may change frequently. The library is not ready for production use yet.

userharbor-sqlalchemy provides a SQLAlchemy-based UserStore implementation for userharbor.

It stores:

  • users,
  • email verification tokens,
  • sessions,
  • password reset tokens,
  • password hashes,
  • roles,
  • permissions,
  • user-to-role assignments,
  • role-to-permission assignments.

The package only handles persistence. It does not send emails, expose HTTP endpoints, or implement application-specific authentication flows.


Installation

pip install userharbor-sqlalchemy

This package depends on userharbor and SQLAlchemy 2.x.


Example usage

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from userharbor import UserHarbor
from userharbor_sqlalchemy import SQLAlchemyUserStore


class EmailSender:
    def send_verification(
        self,
        username: str,
        email: str,
        verification_token: str,
    ) -> None:
        print(f"Send verification token to {email}: {verification_token}")

    def send_password_reset(
        self,
        username: str,
        email: str,
        reset_token: str,
    ) -> None:
        print(f"Send password reset token to {email}: {reset_token}")

    def send_email_verified(self, username: str, email: str) -> None:
        print(f"Email verified for {email}")

    def send_password_changed(self, username: str, email: str) -> None:
        print(f"Password changed for {email}")

    def send_account_deleted(self, username: str, email: str) -> None:
        print(f"Account deleted for {email}")


engine = create_engine("sqlite:///users.db")
SessionLocal = sessionmaker(bind=engine)

store = SQLAlchemyUserStore(SessionLocal)
store.metadata.create_all(engine)
email_sender = EmailSender()

harbor = UserHarbor(
    secret_key="your-secret-key",
    store=store,
    email_sender=email_sender,
)

harbor.register(
    username="jane",
    email="jane@example.com",
    password="StrongPassword123!",
)

harbor.verify_email("verification-token-from-email")

session_token = harbor.login(
    username="jane",
    password="StrongPassword123!",
)

current_user = harbor.get_current_user(session_token)
print(current_user.username)

harbor.roles.create("admin")
harbor.permissions.create("users.delete")
harbor.roles.grant_permission("admin", "users.delete")
harbor.grant_role("jane", "admin")

if harbor.has_permission(session_token, "users.delete"):
    print("User can delete users")

harbor.logout(session_token)

For real applications, replace EmailSender with an implementation that sends verification and password reset messages through your email provider.


Transactions

SQLAlchemyUserStore.transaction() provides a transaction context used by UserHarbor for multi-step operations such as email verification, password reset, password change, account deletion, and role or permission assignment. Successful blocks are committed; exceptions roll back all changes from the block.


License

UserHarbor SQLAlchemy is released under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

userharbor_sqlalchemy-0.4.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

userharbor_sqlalchemy-0.4.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file userharbor_sqlalchemy-0.4.0.tar.gz.

File metadata

  • Download URL: userharbor_sqlalchemy-0.4.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for userharbor_sqlalchemy-0.4.0.tar.gz
Algorithm Hash digest
SHA256 80f5bf9daaa575657b34deae7050ee6f0570123ad452ea2c464da136ce5d8de6
MD5 c8717e18d2f21c6ad08ac43829cb9134
BLAKE2b-256 12ccc0bc6a07e2f5f83968440ec38be710856a70574bbdcb42047ca57df6a813

See more details on using hashes here.

File details

Details for the file userharbor_sqlalchemy-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: userharbor_sqlalchemy-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for userharbor_sqlalchemy-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2fe48982f1c59f916f65e2e43c403814c9370d1a6cceff95d31a85cc481d9de2
MD5 090940442898061be54f5713675c70fd
BLAKE2b-256 99837f13e2f5d0410e9eacf56bfbf45a1d7efe812367702fc50df00a8f31cf20

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

This release

0.4.0 This release

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

0.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page