-
Notifications
You must be signed in to change notification settings - Fork 532
feat(api-integration): add Nango webhook handling #3716
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
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7e33a34
feat(api-integration): add webhook handling for Nango auth events
devin-ai-integration[bot] 12641a1
refactor: make nango_webhook_secret non-optional
devin-ai-integration[bot] a20ea6f
refactor(nango): add verify_webhook_signature and use it from api-int…
devin-ai-integration[bot] c6d71da
chore: cleanup after moving webhook verification
devin-ai-integration[bot] a95456f
merge: resolve conflicts with main (nango webhook and modules)
devin-ai-integration[bot] 3a92e6d
fix(api-integration): require webhook secret in IntegrationConfig::ne…
devin-ai-integration[bot] 103b7cb
fix(api-integration): parse NangoAuthWebhook and commit lockfile
devin-ai-integration[bot] 265fd40
style: dprint format webhook route
devin-ai-integration[bot] 7b80e50
style(permissions): dprint fmt
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| use axum::{Json, extract::State, http::HeaderMap}; | ||
| use hmac::{Hmac, Mac}; | ||
| use serde::Serialize; | ||
| use sha2::Sha256; | ||
| use utoipa::ToSchema; | ||
|
|
||
| use crate::error::{IntegrationError, Result}; | ||
| use crate::state::AppState; | ||
|
|
||
| type HmacSha256 = Hmac<Sha256>; | ||
|
|
||
| #[derive(Debug, Serialize, ToSchema)] | ||
| pub struct WebhookResponse { | ||
| pub status: String, | ||
| } | ||
|
|
||
| #[utoipa::path( | ||
| post, | ||
| path = "/webhook", | ||
| responses( | ||
| (status = 200, description = "Webhook processed", body = WebhookResponse), | ||
| (status = 401, description = "Invalid signature"), | ||
| (status = 400, description = "Bad request"), | ||
| ), | ||
| tag = "integration", | ||
| )] | ||
| pub async fn nango_webhook( | ||
| State(state): State<AppState>, | ||
| headers: HeaderMap, | ||
| body: String, | ||
| ) -> Result<Json<WebhookResponse>> { | ||
| verify_signature(&state.config.nango_webhook_secret, &body, &headers)?; | ||
|
|
||
| let payload: hypr_nango::ConnectWebhook = | ||
|
Check failure on line 34 in crates/api-integration/src/routes/webhook.rs
|
||
| serde_json::from_str(&body).map_err(|e| IntegrationError::BadRequest(e.to_string()))?; | ||
|
|
||
| tracing::info!( | ||
| webhook_type = %payload.r#type, | ||
| operation = %payload.operation, | ||
| connection_id = %payload.connection_id, | ||
| end_user_id = %payload.end_user.end_user_id, | ||
| "nango webhook received" | ||
| ); | ||
|
|
||
| Ok(Json(WebhookResponse { | ||
| status: "ok".to_string(), | ||
| })) | ||
| } | ||
|
|
||
| fn verify_signature(secret: &str, body: &str, headers: &HeaderMap) -> Result<()> { | ||
| let signature = headers | ||
| .get("x-nango-hmac-sha256") | ||
| .and_then(|h| h.to_str().ok()) | ||
| .ok_or_else(|| IntegrationError::Auth("Missing X-Nango-Hmac-Sha256 header".to_string()))?; | ||
|
|
||
| let mut mac = HmacSha256::new_from_slice(secret.as_bytes()) | ||
| .map_err(|e| IntegrationError::Internal(format!("HMAC init error: {}", e)))?; | ||
| mac.update(body.as_bytes()); | ||
| let expected = format!("{:x}", mac.finalize().into_bytes()); | ||
|
|
||
| if expected != signature { | ||
| return Err(IntegrationError::Auth( | ||
| "Invalid webhook signature".to_string(), | ||
| )); | ||
| } | ||
yujonglee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.