Metadata-Version: 2.1
Name: fygaro-webhook
Version: 1.0.0
Summary: Webhook signature verification for Fygaro
Author-email: Fygaro <support@fygaro.com>
License: MIT License
        
        Copyright (c) 2025 Fygaro
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Keywords: fygaro,webhook,signature
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE

# fygaro-webhook

> **Webhook signature verification for Fygaro — pure Python stdlib, zero runtime deps**

This helper validates the `Fygaro-Signature` header of incoming webhooks.
It supports secret rotation (multiple active secrets) and is ready for future
hash algorithms.

---

## Installation

```bash
pip install fygaro-webhook
```

Requires Python ≥ 3.8.

---

## Quick start

```python
from fygaro.webhook import FygaroWebhookValidator

# Load your current and (optionally) previous secrets
validator = FygaroWebhookValidator(
    secrets=[
        b"my-primary-secret",     # bytes or str → utf-8 encoded
    ],
    # max_age_seconds=300     # optional, default = 5 min
)

# In your view / handler
if not validator.verify_signature(
    signature_header=request.headers["Fygaro-Signature"],
    body=request.body,          # raw bytes exactly as sent
):
    raise ValueError("Invalid signature")

# ...process JSON, return 200...
```

---

## API reference (detailed)

### `class FygaroWebhookValidator`

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `secrets` | `Sequence[str \| bytes]` | ✔ | —  | One or more active webhook secrets. Provide **all currently valid** secrets during a rotation window. Each secret can be a UTF-8 `str` or raw `bytes`. |
| `max_age_seconds` | `int` | ✖ | `300` | Maximum allowable clock skew (in seconds) between the timestamp in the header and the server time. A low value mitigates replay attacks. |

```python
validator = FygaroWebhookValidator(
    secrets=["primary"],        # Add multiple for rotation: secrets=["primary", "previous"]
)
```

---

#### `validator.verify_signature(signature_header: str, body: bytes) -> bool`

| Argument | Type | Description |
|----------|------|-------------|
| `signature_header` | `str` | The exact value of the incoming **Fygaro-Signature** HTTP header. |
| `body` | `bytes` | The unmodified request body (raw bytes). **Do not** `.decode()` or re-serialize. |

Return value:

* `True` — signature is valid **and** timestamp is within `max_age_seconds`.
* `False` — signature mismatch, stale timestamp, or malformed header.

```python
is_valid = validator.verify_signature(sig_header, raw_body)
```

---

## License

MIT © Fygaro — support: [support@fygaro.com](mailto:support@fygaro.com)
