Metadata-Version: 2.3
Name: abs-auth-core
Version: 0.1.2
Summary: Authentication core utilities including JWT token management.
License: MIT
Author: AutoBridgeSystems
Author-email: info@autobridgesystems.com
Requires-Python: >=3.13
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Requires-Dist: fastapi[standard] (>=0.115.12,<0.116.0)
Requires-Dist: passlib (>=1.7.4,<2.0.0)
Requires-Dist: pyjwt (>=2.10.1,<3.0.0)
Description-Content-Type: text/markdown

# FastAPI Auth Core

A robust authentication package for FastAPI applications that provides JWT token management, user authentication, and middleware functionality.

## Features

- JWT token generation and validation
- Password hashing and verification
- User authentication middleware
- Refresh token management
- SQLAlchemy integration

## Installation

```bash
pip install fastapi-auth-core
```

## Dependencies

- Python >= 3.13
- FastAPI >= 0.115.12
- PyJWT >= 2.10.1
- Passlib >= 1.7.4

## Usage

### Basic Setup

```python
from fastapi import FastAPI, Depends
from fastapi_auth_core import AuthFunctions, JWTFunctions
from sqlalchemy.orm import Session

app = FastAPI()

# Initialize JWT functions
jwt_functions = JWTFunctions(
    secret_key="your-secret-key",
    algorithm="HS256",
    expire_minutes=30
)

# Initialize Auth functions with your database session and User model
auth_functions = AuthFunctions(
    db_session=lambda: Session(),
    User=YourUserModel
)
```

### Authentication Middleware

```python
from fastapi_auth_core import auth_middleware

# Create the authentication middleware
auth = auth_middleware(
    db_session=lambda: Session(),
    Users=YourUserModel,
    jwt_secret_key="your-secret-key",
    jwt_algorithm="HS256"
)

# Use the middleware in your routes
@app.get("/protected")
async def protected_route(user = Depends(auth)):
    return {"message": f"Hello, {user.username}!"}
```

### Token Generation

```python
# Generate access and refresh tokens
tokens = jwt_functions.generate_tokens(
    data={"sub": "user123"},
    user_id=1,
    db=lambda: Session(),
    User=YourUserModel
)

# The tokens dictionary contains:
# {
#     "access_token": "...",
#     "refresh_token": "...",
#     "token_type": "bearer"
# }
```

### Password Management

```python
# Hash a password
hashed_password = jwt_functions.get_password_hash("user_password")

# Verify a password
is_valid = jwt_functions.verify_password("user_password", hashed_password)
```

### User Management

```python
# Get user by attribute
user = auth_functions.get_user_by_attribute("email", "user@example.com")

# Update user's refresh token
await auth_functions.update_user_refresh_token(user_id=1, refresh_token="new_token")
```

## Security Features

- Secure password hashing using bcrypt
- JWT token expiration
- Refresh token rotation
- Type-safe database operations
- Exception handling for authentication failures

## Best Practices

1. Always use HTTPS in production
2. Store sensitive configuration (secret keys, etc.) in environment variables
3. Implement proper error handling
4. Use appropriate token expiration times
5. Implement rate limiting for authentication endpoints

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the LICENSE file for details.

