Feature/issue 547 idempotent webhook#646
Open
Tekprecious wants to merge 6 commits into
Open
Conversation
…r AI service callbacks
…ation for AI service callbacks
|
@whitezaddy is attempting to deploy a commit to the Cedarich's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Tekprecious Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
Author
|
Noted |
Author
|
@Cedarich Authorize vercel |
Contributor
|
Fix conflict @Tekprecious |
Author
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request implements issue #547 by introducing a new secure, asynchronous, and idempotent webhook endpoint to receive verification results from the external AI service. This is a critical piece of infrastructure that decouples the main application from the AI processing, improving system resilience and scalability by allowing for reliable, out-of-band communication.
All acceptance criteria have been met:
HmacAuthGuardthat validates theX-Signature-256header.eventIdin the newWebhookEventtable before processing to prevent duplicate updates from repeated deliveries.401for bad signatures,409for duplicates,404for missing sessions) to provide clear feedback to the calling service.Implementation Details
New
WebhooksModule: A dedicated module (WebhooksModule,WebhooksController,WebhooksService) has been created to handle all incoming webhooks. This encapsulates the logic for the newPOST /webhooks/ai-verificationendpoint.HMAC Signature Verification: A new
HmacAuthGuardsecures the endpoint. It computes a SHA256 HMAC of the raw request body using a shared secret (AI_WEBHOOK_SECRET) and compares it in a time-safe manner to the signature provided in theX-Signature-256header.Idempotency Layer: The
WebhooksServicenow performs an idempotency check using theeventIdfrom the payload. It queries a newWebhookEventtable in the database to see if an event has already been processed. If it has, the service immediately returns a409 Conflictstatus to acknowledge receipt without reprocessing.Prisma Schema Update: A new
WebhookEventmodel was added toprisma/schema.prismato store a record of each processed webhook, which is the foundation for the idempotency check.Integration with
SessionService: Upon successful validation, theWebhooksServicefinds the correctidentity_verificationstep within the specified session and callssessionService.submitToStepto update the session's state with the AI's results. The entire operation is wrapped in a database transaction to ensure atomicity.Configuration & Middleware:
AI_WEBHOOK_SECREThas been added to the application's configuration service.main.ts) has been updated to enablerawBodyparsing for JSON requests, which is a prerequisite for the HMAC guard to function correctly.Unit Tests: Comprehensive unit tests have been added for
WebhooksControllerandWebhooksServiceto cover success paths, error handling (duplicate events, missing sessions), and integration with service mocks.Type of Change
Testing
WebhooksControllerandWebhooksService.rawBodyis correctly parsed and available to the guard.webhook_eventstable upon successful processing.Related Issues
Closes #547