Skip to main content
userharbor

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

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

Each user has at most one active email verification token and one active password reset token. Storing a new token of either kind removes that user's previous token. Users can have multiple active sessions.

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

Usernames preserve their original casing while lookup and uniqueness use a Unicode case-folded key. The default model stores that key in the unique, indexed username_key column.


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.


Custom user models

A custom user model must provide username_key in addition to username, email, password_hash, and verified:

username: Mapped[str] = mapped_column(String(255), primary_key=True)
username_key: Mapped[str] = mapped_column(String(255), unique=True, index=True)

SQLAlchemyUserStore.create_user() fills username_key with username.casefold(). The unique constraint prevents accounts whose usernames differ only by casing, while username keeps the spelling selected during registration.


License

UserHarbor SQLAlchemy is released under the MIT License.

Metadata

Release files for userharbor-sqlalchemy 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for userharbor-sqlalchemy 0.8.0
File Size Uploaded Provenance Actions
userharbor_sqlalchemy-0.8.0.tar.gz 7.3 kB Without provenance Details for userharbor_sqlalchemy-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for userharbor-sqlalchemy 0.8.0
File Interpreter ABI Platform Actions
userharbor_sqlalchemy-0.8.0-py3-none-any.whl Python 3 none any Details for userharbor_sqlalchemy-0.8.0-py3-none-any.whl

Total release size: 15.7 kB

Release files / userharbor_sqlalchemy-0.8.0.tar.gz

Download URL userharbor_sqlalchemy-0.8.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2af4226ad95233bbd2dc21495adb65d06ee45c2d6e620a07d4bef588037e63a0
BLAKE2b-256 checksum
How to use checksums
ac3650190d90be2401c1003d14ab5b6a96e87403948e08c4bf9d2d5bbc759900
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","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}

Release files / userharbor_sqlalchemy-0.8.0-py3-none-any.whl

Download URL userharbor_sqlalchemy-0.8.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bc3fc5b0650f5ab0bcbc9742b90d02f3c2b31cae5ff3391cfccc822a1c5f435
BLAKE2b-256 checksum
How to use checksums
3541d63cdf1970c8f33b2e888e67964cb7756d93e44173e33c0ffcc0fee2637a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","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}

Security for userharbor-sqlalchemy 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page