Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /Athroniaeth_keyshield /examples /example_scopes.py
| import os | |
| from pathlib import Path | |
| from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession | |
| from keyshield import ApiKeyService | |
| from keyshield.domain.errors import InvalidScopes | |
| from keyshield.hasher.argon2 import Argon2ApiKeyHasher | |
| from keyshield.repositories.sql import SqlAlchemyApiKeyRepository | |
| # Set env var to override default pepper | |
| # Using a strong, unique pepper is crucial for security | |
| # Default pepper is insecure and should not be used in production | |
| pepper = os.getenv("SECRET_PEPPER") | |
| hasher = Argon2ApiKeyHasher(pepper=pepper) | |
| path = Path(__file__).parent / "db.sqlite3" | |
| database_url = os.environ.get("DATABASE_URL", f"sqlite+aiosqlite:///{path}") | |
| async_engine = create_async_engine(database_url, future=True) | |
| async_session_maker = async_sessionmaker( | |
| async_engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| ) | |
| async def main(): | |
| async with async_session_maker() as async_session: | |
| repo = SqlAlchemyApiKeyRepository(async_session) | |
| # Necessary if you don't use your own DeclarativeBase | |
| await repo.ensure_table(async_engine=async_engine) | |
| svc = ApiKeyService(repo=repo, hasher=hasher) | |
| # Create an API key without the required "write" scope | |
| _, bad_api_key = await svc.create(name="no-scope-key", scopes=["read"]) | |
| _, good_api_key = await svc.create(name="with-scope-key", scopes=["write"]) | |
| print(f"Bad API Key (no required scopes): '{bad_api_key}'") | |
| print(f"Good API Key (with required scopes): '{good_api_key}'") | |
| await svc.verify_key(good_api_key, required_scopes=["write"]) | |
| print("Successfully verified good API key with required scopes.") | |
| try: | |
| await svc.verify_key(bad_api_key, required_scopes=["write"]) | |
| except InvalidScopes as e: | |
| print(f"Failed to verify bad API key with required scopes: {e}") | |
| if __name__ == "__main__": | |
| import asyncio | |
| asyncio.run(main()) | |
Xet Storage Details
- Size:
- 1.97 kB
- Xet hash:
- 89a6b82da2f5026ee14550362ad553c74695569e5669176e77032e4ec87c339d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.