Skip to content
Open
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
3 changes: 2 additions & 1 deletion webhooks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -109,7 +110,7 @@ func (r *WebhookService) VerifySignatureWithTolerance(body []byte, headers http.
// tolerance specifies the maximum age of the webhook.
// now allows specifying the current time for testing purposes.
func (r *WebhookService) VerifySignatureWithToleranceAndTime(body []byte, headers http.Header, tolerance time.Duration, now time.Time, opts ...option.RequestOption) error {
cfg, err := requestconfig.PreRequestOptions(r.Options...)
cfg, err := requestconfig.PreRequestOptions(slices.Concat(r.Options, opts)...)
if err != nil {
return err
}
Expand Down
35 changes: 35 additions & 0 deletions webhooks/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ func TestWebhookService_VerifySignature_ValidSignature(t *testing.T) {
}
}

func TestWebhookService_VerifySignature_MethodLevelSecret(t *testing.T) {
client := openai.NewClient(option.WithAPIKey("test-key"))

fixedTime := time.Unix(testTimestamp, 0)
err := client.Webhooks.VerifySignatureWithToleranceAndTime(
[]byte(testPayload),
createTestHeaders(),
5*time.Minute,
fixedTime,
option.WithWebhookSecret(testSecret),
)
if err != nil {
t.Errorf("VerifySignatureWithToleranceAndTime should have succeeded with a method-level secret: %v", err)
}
}

func TestWebhookService_VerifySignature_MethodLevelSecretOverridesClientSecret(t *testing.T) {
client := openai.NewClient(
option.WithAPIKey("test-key"),
option.WithWebhookSecret("wrong-secret"),
)

fixedTime := time.Unix(testTimestamp, 0)
err := client.Webhooks.VerifySignatureWithToleranceAndTime(
[]byte(testPayload),
createTestHeaders(),
5*time.Minute,
fixedTime,
option.WithWebhookSecret(testSecret),
)
if err != nil {
t.Errorf("VerifySignatureWithToleranceAndTime should have preferred the method-level secret: %v", err)
}
}

func TestWebhookService_VerifySignature_InvalidSignature(t *testing.T) {
client := openai.NewClient(
option.WithAPIKey("test-key"),
Expand Down