Skip to content

Helper method to receive WebhookEvent #109

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
Mar 30, 2022
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 auth/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ func (p *FileBasedKeyProvider) GetSecret(key string) string {
func (p *FileBasedKeyProvider) NumKeys() int {
return len(p.keys)
}

type SimpleKeyProvider struct {
apiKey string
apiSecret string
}

func NewSimpleKeyProvider(apiKey, apiSecret string) *SimpleKeyProvider {
return &SimpleKeyProvider{
apiKey: apiKey,
apiSecret: apiSecret,
}
}

func (p *SimpleKeyProvider) GetSecret(key string) string {
if key == p.apiKey {
return p.apiSecret
}
return ""
}

func (p *SimpleKeyProvider) NumKeys() int {
return 1
}
20 changes: 20 additions & 0 deletions webhook/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"io/ioutil"
"net/http"

"google.golang.org/protobuf/encoding/protojson"

"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
)

// Receive reads and verifies incoming webhook is signed with key/secret pair
Expand Down Expand Up @@ -48,3 +51,20 @@ func Receive(r *http.Request, provider auth.KeyProvider) ([]byte, error) {

return data, nil
}

// ReceiveWebhookEvent reads and verifies incoming webhook, and returns a parsed WebhookEvent
func ReceiveWebhookEvent(r *http.Request, provider auth.KeyProvider) (*livekit.WebhookEvent, error) {
data, err := Receive(r, provider)
if err != nil {
return nil, err
}
unmarshalOpts := protojson.UnmarshalOptions{
DiscardUnknown: true,
AllowPartial: true,
}
event := livekit.WebhookEvent{}
if err = unmarshalOpts.Unmarshal(data, &event); err != nil {
return nil, err
}
return &event, nil
}
76 changes: 57 additions & 19 deletions webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,83 @@ import (
"encoding/json"
"net"
"net/http"
"sync"
"testing"

"github.com/stretchr/testify/require"

"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/webhook"
)

const (
apiKey = "mykey"
apiSecret = "mysecret"
)

func TestWebHook(t *testing.T) {
s := newServer(":8765")
require.NoError(t, s.Start())
defer s.Stop()

authProvider := auth.NewFileBasedKeyProviderFromMap(map[string]string{
"mykey": "mysecret",
authProvider := auth.NewSimpleKeyProvider(
apiKey, apiSecret,
)
notifier := webhook.NewNotifier(apiKey, apiSecret, []string{
"http://localhost:8765",
})

payload := map[string]interface{}{
"test": "payload",
"nested": map[string]interface{}{
"structure": true,
},
}
t.Run("test json payload", func(t *testing.T) {
payload := map[string]interface{}{
"test": "payload",
"nested": map[string]interface{}{
"structure": true,
},
}

s.handler = func(r *http.Request) {
// receive logic
data, err := webhook.Receive(r, authProvider)
require.NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(1)
s.handler = func(r *http.Request) {
defer wg.Done()
// receive logic
data, err := webhook.Receive(r, authProvider)
require.NoError(t, err)

var decoded map[string]interface{}
require.NoError(t, json.Unmarshal(data, &decoded))
var decoded map[string]interface{}
require.NoError(t, json.Unmarshal(data, &decoded))

require.EqualValues(t, decoded, payload)
}
require.EqualValues(t, decoded, payload)
}

notifier := webhook.NewNotifier("mykey", "mysecret", []string{
"http://localhost:8765",
require.NoError(t, notifier.Notify(context.Background(), payload))
wg.Wait()
})
require.NoError(t, notifier.Notify(context.Background(), payload))

t.Run("test event payload", func(t *testing.T) {
event := &livekit.WebhookEvent{
Event: webhook.EventTrackPublished,
Participant: &livekit.ParticipantInfo{
Identity: "test",
},
Track: &livekit.TrackInfo{
Sid: "TR_abcde",
},
}

wg := sync.WaitGroup{}
wg.Add(1)
s.handler = func(r *http.Request) {
defer wg.Done()
decodedEvent, err := webhook.ReceiveWebhookEvent(r, authProvider)
require.NoError(t, err)

require.EqualValues(t, event, decodedEvent)
}
require.NoError(t, notifier.Notify(context.Background(), event))
wg.Wait()
})

}

type testServer struct {
Expand Down