Twilio SendGrid's Event Webhook will notify a URL via HTTP POST
with information about events that occur as your mail is processed. This article covers all you need to know to secure the Event Webhook, allowing you to verify that incoming requests originate from Twilio SendGrid.
For more information about working with the Event Webhook and the data it provides, see Getting Started with the Event Webhook and the Twilio SendGrid Event Webhook Overview.
Make sure your API Key Permissions are set to Full Access.
Twilio SendGrid provides two methods for securing the Event Webhook: cryptographic signing and OAuth 2.0. These two security methods are independent of one another and can be used simultaneously.
When using the Signed Event Webhook, Twilio SendGrid will generate a private and public key pair. The private key will be used to generate a signature that is posted to your HTTP webhook in the X-Twilio-Email-Event-Webhook-Signature
header.
You can verify the signature with the public key provided. More information is provided in the Verification section of this page.
To enable signature verification and generate a key pair:
Navigate to Settings > Mail Settings in the sidebar navigation.
Under Webhook Settings, click Event Webhooks:
Once you have begun creating or editing a Webhook, you will manage its security features in the Security features section of the dialog that opens.
Scroll to Security features and toggle Enable Signed Event Webhook. Please note, you must click Save in order to generate a verification key and enable signature verification. If you click Test Integration before clicking Save, signature verification will not be tested because the key pair is only generated once the Webhook is saved.
To disable signature verification, which will delete the Webhook's existing key pair:
You can also manage signature verification for the Event Webhook using SendGrid's API. See the Event Webhook API reference for more information.
Twilio SendGrid generates the private and public key pair using the Elliptic Curve Digital Signature Algorithm (ECDSA).
We recommend using a package or library suitable for your language to verify the signature. Libraries are listed below in the "Sample verification libraries" section of this page. The general steps required to verify a signature are outlined below with Go code samples.
When verifying the signature, be aware that we deliver a payload that must be used in its raw bytes form. Transformations from raw bytes to a JSON string may remove characters that were used as part of the generated signature.
GET
request by including the parameters listed (Headers and Responses) on the Get an Event Webhook documentation."X-Twilio-Email-Event-Webhook-Signature"
HTTP header.1// Golang Example2s := http.Request.Header.Get("X-Twilio-Email-Event-Webhook-Signature")
"X-Twilio-Email-Event-Webhook-Timestamp"
HTTP header.1// Golang Example2ts := http.Request.Header.Get("X-Twilio-Email-Event-Webhook-Timestamp")
{r value},{s value}
.1// Golang Example2signatureBytes, _ := base64.StdEncoding.DecodeString(s)3ecdsaSig := struct {4R *big.Int5S *big.Int6}78asn1.Unmarshal(signatureBytes, &ecdsaSig)
1// Golang Example2tsBytes := []byte(ts)3payload, _ := ioutil.ReadAll(http.Request.Body)4h := sha256.New()5h.Write(tsBytes)6h.Write(payload)7hashedPayload := h.Sum(nil)
1// Golang Example2// uses https://golang.org/pkg/crypto/ecdsa/ to perform the verification3ecdsa.Verify(publicKey, hashedPayload, ecdsaSig.R, ecdsaSig.S)
The Twilio SendGrid API libraries contain helpers to assist you when verifying the ECDSA signature. The links below will take you to the Event Webhook helper in each library.
OAuth offers an additional and separate way of providing security controls for the Event Webhook. OAuth is an open authorization protocol used to share resources with applications. Rather than sharing your username and password with an application, granting total access to your account, OAuth enables scoped access to your resources. For more on OAuth and how it works, see the OAuth community site.
The Twilio SendGrid Event Webhook uses the Client Credentials OAuth grant type, which is an authorization workflow meant for machine-to-machine communication. This authorization method creates a token that Twilio SendGrid can pass to your app in an Authorization header, allowing you to verify that the request originated from Twilio SendGrid.
OAuth can be confusing. To help illuminate the process, we have provided a description of the setup flow here.
POST
requests to this URL. To ensure that the requests you receive are actually from Twilio SendGrid, you implement OAuth.POST
URL endpoint for your app and a URL to an authorization server or OAuth service.POST
URL endpoint. This will be done in an Authorization header.To enable OAuth 2.0:
Navigate to Settings > Mail Settings in the sidebar navigation.
Under Webhook Settings, click Event Webhooks:
Once you have begun creating or editing a Webhook, you will manage its security features in the Security features section of the dialog that opens. Unlike signature verification, OAuth verification can be completed without first saving the Webhook.
Scroll to Security features and toggle Enable OAuth to reveal Client ID, Secret Token, and Token URL fields.
Please note, it is your responsibility to verify the access token used in requests to your HTTP POST
URL.
To disable OAuth 2.0:
POST
URL by Twilio SendGrid will no longer contain an access token.Twilio SendGrid allows you to manage OAuth setup using the API. See the Event Webhook API reference for more information.
Using the v2 Web API's eventnotify
API call will overwrite any previously configured Event Webhook notification settings, including OAuth 2.0. If your OAuth 2.0 settings are overwritten, please configure them again using either the Mail Settings page or the SendGrid v3 API
Once you have established an OAuth Access Token per the above directions, SendGrid will cache that token so that it can be used in any further webhook requests without each time requesting that token again from your OAuth service.
Upon receiving a webhook request with an invalid or expired token, your webhook needs to return a status code of 4xx (typically 400, 401 or 403) and the body payload must contain one of the following strings:
"invalid_request
", "invalid_token
", "insufficient_scope
".
More specifically, SendGrid follows RFC 6750, which defines the error codes as follows:
invalid_request
The request is missing a required parameter, includes an
unsupported parameter or parameter value, repeats the same
parameter, uses more than one method for including an access
token, or is otherwise malformed. The resource server SHOULD
respond with the HTTP 400 (Bad Request) status code.
invalid_token
The access token provided is expired, revoked, malformed, or
invalid for other reasons. The resource SHOULD respond with
the HTTP 401 (Unauthorized) status code.
insufficient_scope
The request requires higher privileges than provided by the
access token. The resource server SHOULD respond with the HTTP
403 (Forbidden) status code.
Upon receiving any of these error codes from your webhook, SendGrid will request a new OAuth token from your OAuth service and retry the webhook.
Now that you know how to secure the Event Webhook, you can begin using your event data to better understand your email. See the Event Webhook Reference for more information about the data it provides.