Skip to content

feat: add webhook verification methods #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,29 @@ client.hris.directory.list_individuals(
)
```

## Webhook Verification

We provide helper methods for verifying that a webhook request came from Finch, and not a malicious third party.

You can use `finch.webhooks.verify_signature(body: string, headers, secret?) -> None` or `finch.webhooks.unwrap(body: string, headers, secret?) -> Payload`,
both of which will raise an error if the signature is invalid.

Note that the "body" parameter must be the raw JSON string sent from the server (do not parse it first).
The `.unwrap()` method can parse this JSON for you into a `Payload` object.

For example, in [FastAPI](https://fastapi.tiangolo.com/):

```py
@app.post('/my-webhook-handler')
async def handler(request: Request):
body = await request.body()
secret = os.environ['FINCH_WEBHOOK_SECRET'] # env var used by default; explicit here.
payload = client.webhooks.unwrap(body, request.headers, secret)
print(payload)

return {'ok': True}
```

## Handling errors

When the library is unable to connect to the API (e.g., due to network connection problems or a timeout), a subclass of `finch.APIConnectionError` is raised.
Expand Down
7 changes: 7 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,10 @@ Methods:

- <code title="post /disconnect">client.account.<a href="./src/finch/resources/account.py">disconnect</a>() -> <a href="./src/finch/types/disconnect_response.py">DisconnectResponse</a></code>
- <code title="get /introspect">client.account.<a href="./src/finch/resources/account.py">introspect</a>() -> <a href="./src/finch/types/introspection.py">Introspection</a></code>

# Webhooks

Methods:

- <code>client.webhooks.<a href="./src/finch/resources/webhooks.py">unwrap</a>(\*args) -> object</code>
- <code>client.webhooks.<a href="./src/finch/resources/webhooks.py">verify_signature</a>(\*args) -> None</code>
20 changes: 20 additions & 0 deletions src/finch/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,20 @@ class Finch(SyncAPIClient):
ats: resources.ATS
providers: resources.Providers
account: resources.Account
webhooks: resources.Webhooks

# client options
access_token: str | None
client_id: str | None
client_secret: str | None
webhook_secret: str | None

def __init__(
self,
*,
client_id: str | None = None,
client_secret: str | None = None,
webhook_secret: str | None = None,
base_url: Optional[str] = None,
access_token: Optional[str] = None,
timeout: Union[float, Timeout, None] = DEFAULT_TIMEOUT,
Expand Down Expand Up @@ -87,6 +90,7 @@ def __init__(
This automatically infers the following arguments from their corresponding environment variables if they are not provided:
- `client_id` from `FINCH_CLIENT_ID`
- `client_secret` from `FINCH_CLIENT_SECRET`
- `webhook_secret` from `FINCH_WEBHOOK_SECRET`
"""
self.access_token = access_token

Expand All @@ -96,6 +100,9 @@ def __init__(
client_secret_envvar = os.environ.get("FINCH_CLIENT_SECRET", None)
self.client_secret = client_secret or client_secret_envvar or None

webhook_secret_envvar = os.environ.get("FINCH_WEBHOOK_SECRET", None)
self.webhook_secret = webhook_secret or webhook_secret_envvar or None

if base_url is None:
base_url = f"https://api.tryfinch.com"

Expand All @@ -116,6 +123,7 @@ def __init__(
self.ats = resources.ATS(self)
self.providers = resources.Providers(self)
self.account = resources.Account(self)
self.webhooks = resources.Webhooks(self)

@property
def qs(self) -> Querystring:
Expand Down Expand Up @@ -151,6 +159,7 @@ def copy(
*,
client_id: str | None = None,
client_secret: str | None = None,
webhook_secret: str | None = None,
access_token: str | None = None,
base_url: str | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -189,6 +198,7 @@ def copy(
return self.__class__(
client_id=client_id or self.client_id,
client_secret=client_secret or self.client_secret,
webhook_secret=webhook_secret or self.webhook_secret,
base_url=base_url or str(self.base_url),
access_token=access_token or self.access_token,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
Expand Down Expand Up @@ -272,17 +282,20 @@ class AsyncFinch(AsyncAPIClient):
ats: resources.AsyncATS
providers: resources.AsyncProviders
account: resources.AsyncAccount
webhooks: resources.AsyncWebhooks

# client options
access_token: str | None
client_id: str | None
client_secret: str | None
webhook_secret: str | None

def __init__(
self,
*,
client_id: str | None = None,
client_secret: str | None = None,
webhook_secret: str | None = None,
base_url: Optional[str] = None,
access_token: Optional[str] = None,
timeout: Union[float, Timeout, None] = DEFAULT_TIMEOUT,
Expand Down Expand Up @@ -310,6 +323,7 @@ def __init__(
This automatically infers the following arguments from their corresponding environment variables if they are not provided:
- `client_id` from `FINCH_CLIENT_ID`
- `client_secret` from `FINCH_CLIENT_SECRET`
- `webhook_secret` from `FINCH_WEBHOOK_SECRET`
"""
self.access_token = access_token

Expand All @@ -319,6 +333,9 @@ def __init__(
client_secret_envvar = os.environ.get("FINCH_CLIENT_SECRET", None)
self.client_secret = client_secret or client_secret_envvar or None

webhook_secret_envvar = os.environ.get("FINCH_WEBHOOK_SECRET", None)
self.webhook_secret = webhook_secret or webhook_secret_envvar or None

if base_url is None:
base_url = f"https://api.tryfinch.com"

Expand All @@ -339,6 +356,7 @@ def __init__(
self.ats = resources.AsyncATS(self)
self.providers = resources.AsyncProviders(self)
self.account = resources.AsyncAccount(self)
self.webhooks = resources.AsyncWebhooks(self)

@property
def qs(self) -> Querystring:
Expand Down Expand Up @@ -374,6 +392,7 @@ def copy(
*,
client_id: str | None = None,
client_secret: str | None = None,
webhook_secret: str | None = None,
access_token: str | None = None,
base_url: str | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -412,6 +431,7 @@ def copy(
return self.__class__(
client_id=client_id or self.client_id,
client_secret=client_secret or self.client_secret,
webhook_secret=webhook_secret or self.webhook_secret,
base_url=base_url or str(self.base_url),
access_token=access_token or self.access_token,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
Expand Down
14 changes: 13 additions & 1 deletion src/finch/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
from .ats import ATS, AsyncATS
from .hris import HRIS, AsyncHRIS
from .account import Account, AsyncAccount
from .webhooks import Webhooks, AsyncWebhooks
from .providers import Providers, AsyncProviders

__all__ = ["HRIS", "AsyncHRIS", "ATS", "AsyncATS", "Providers", "AsyncProviders", "Account", "AsyncAccount"]
__all__ = [
"HRIS",
"AsyncHRIS",
"ATS",
"AsyncATS",
"Providers",
"AsyncProviders",
"Account",
"AsyncAccount",
"Webhooks",
"AsyncWebhooks",
]
Loading