-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support and example for secure webhook feature (#389)
* eistats-1123: added support and example for secure webhook feature * Updated securewebhook struct to have bool-pointer + added unittests * created separate example file + added README * updated link in README + updated comments in the example code * removed return statement from SetEnable()
- Loading branch information
Showing
5 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/sendgrid/rest" | ||
"github.com/sendgrid/sendgrid-go" | ||
"github.com/sendgrid/sendgrid-go/helpers/securewebhook" | ||
) | ||
|
||
// EnableSecureWebhook : Enables Signed Event Webhook. | ||
// PATCH /user/webhooks/event/settings/signed | ||
func EnableSecureWebhook() { | ||
var err error | ||
apiKey := os.Getenv("SENDGRID_API_KEY") | ||
host := "https://api.sendgrid.com" | ||
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/event/settings/signed", host) | ||
request.Method = rest.Patch | ||
s := securewebhook.NewSettings() | ||
s.SetEnable(true) | ||
request.Body, err = securewebhook.GetRequestBody(s) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
response, err := sendgrid.MakeRequest(request) | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
fmt.Println(response.StatusCode) | ||
fmt.Println(response.Body) | ||
fmt.Println(response.Headers) | ||
} | ||
} | ||
|
||
// GetPublicKeyForWebhook : Get Public Key for Event Webhook. | ||
// Get /user/webhooks/event/settings/signed | ||
func GetPublicKeyForWebhook() { | ||
apiKey := os.Getenv("SENDGRID_API_KEY") | ||
host := "https://api.sendgrid.com" | ||
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/event/settings/signed", host) | ||
request.Method = rest.Get | ||
response, err := sendgrid.MakeRequest(request) | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
fmt.Println(response.StatusCode) | ||
fmt.Println(response.Body) | ||
fmt.Println(response.Headers) | ||
} | ||
} | ||
|
||
func main() { | ||
// add your function calls here | ||
} |
This file contains 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 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,30 @@ | ||
**This helper allows you to quickly and easily enable/disable SecureWebhook feature or get the public key through Twilio SendGrid.** | ||
|
||
## Dependencies | ||
|
||
- [rest](https://github.com/sendgrid/rest) | ||
|
||
# Quick Start | ||
|
||
Run the [example](https://github.com/sendgrid/sendgrid-go/blob/master/examples/securewebhook/securewebhook.go) (make sure you have set your environment variable to include your SENDGRID_API_KEY). | ||
```bash | ||
go run examples/securewebhook/securewebhook.go | ||
``` | ||
|
||
## Usage | ||
|
||
- See the [example](https://github.com/sendgrid/sendgrid-go/blob/master/examples/securewebhook/securewebhook.go) for a complete working example. | ||
- [Documentation](https://sendgrid.com/docs/for-developers/tracking-events/) | ||
|
||
## Test | ||
|
||
```bash | ||
go test ./... -v | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
cd helpers/securewebhook | ||
go test -v | ||
``` |
This file contains 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,29 @@ | ||
package securewebhook | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// Settings ... | ||
type Settings struct { | ||
Enable *bool `json:"enabled,omitempty"` | ||
} | ||
|
||
// NewSettings ... | ||
func NewSettings() *Settings { | ||
return &Settings{} | ||
} | ||
|
||
// SetEnable ... | ||
func (s *Settings) SetEnable(enable bool) { | ||
s.Enable = &enable | ||
} | ||
|
||
// GetRequestBody ... | ||
func GetRequestBody(s *Settings) ([]byte, error) { | ||
b, err := json.Marshal(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return b, nil | ||
} |
This file contains 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,42 @@ | ||
package securewebhook | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSecureWebhookNewSettings(t *testing.T) { | ||
assert.NotNil(t, NewSettings(), "NewSettings() shouldn't return nil") | ||
} | ||
|
||
func TestSecureWebhookSetEnable(t *testing.T) { | ||
s := NewSettings() | ||
assert.NotNil(t, NewSettings(), "NewSettings() shouldn't return nil") | ||
|
||
s.SetEnable(true) | ||
assert.Equal(t, true, *s.Enable, fmt.Sprintf("SecureWebhook.Enable should be 'true', got %v", *s.Enable)) | ||
|
||
s.SetEnable(false) | ||
assert.Equal(t, false, *s.Enable, fmt.Sprintf("SecureWebhook.Enable should be 'false', got %v", *s.Enable)) | ||
} | ||
|
||
func TestSecureWebhookGetRequestBody(t *testing.T) { | ||
expectedJSONEnabled := []byte(`{"enabled":true}`) | ||
expectedJSONDisabled := []byte(`{"enabled":false}`) | ||
|
||
s := NewSettings() | ||
assert.NotNil(t, NewSettings(), "NewSettings() shouldn't return nil") | ||
|
||
s.SetEnable(false) | ||
actualJSON, err := GetRequestBody(s) | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedJSONDisabled, actualJSON, fmt.Sprintf("SecureWebhook.Enable should be '%b', got %b", expectedJSONDisabled, actualJSON)) | ||
|
||
s.SetEnable(true) | ||
actualJSON, err = GetRequestBody(s) | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedJSONEnabled, actualJSON, fmt.Sprintf("SecureWebhook.Enable should be '%b', got %b", expectedJSONEnabled, actualJSON)) | ||
} |