-
Notifications
You must be signed in to change notification settings - Fork 460
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
webhook: export ComputeSignature #602
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ import ( | |
) | ||
|
||
const ( | ||
// Signatures older than this will be rejected by ConstructEvent | ||
// DefaultTolerance signatures older than this will be rejected by ConstructEvent | ||
DefaultTolerance time.Duration = 300 * time.Second | ||
signingVersion string = "v1" | ||
) | ||
|
@@ -27,10 +27,10 @@ var ( | |
ErrNoValidSignature error = errors.New("Webhook had no valid signature") | ||
) | ||
|
||
// Computes a webhook signature using Stripe's v1 signing method. See | ||
// ComputeSignature computes a webhook signature using Stripe's v1 signing method. See | ||
// https://stripe.com/docs/webhooks#signatures | ||
func computeSignature(t time.Time, payload []byte, secret string) []byte { | ||
mac := hmac.New(sha256.New, []byte(secret)) | ||
func ComputeSignature(t time.Time, payload, secret []byte) []byte { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a lot for trying to follow the advice I was giving in #497! Contrary to what I said there though, I now see that unfortunately changing the signature to Sorry for the reversal, but for those reasons, I think it actually might be better to leave |
||
mac := hmac.New(sha256.New, secret) | ||
mac.Write([]byte(fmt.Sprintf("%d", t.Unix()))) | ||
mac.Write([]byte(".")) | ||
mac.Write(payload) | ||
|
@@ -95,7 +95,7 @@ func parseSignatureHeader(header string) (*signedHeader, error) { | |
// your signing secret from the Stripe dashboard: | ||
// https://dashboard.stripe.com/webhooks | ||
// | ||
func ConstructEvent(payload []byte, header string, secret string) (stripe.Event, error) { | ||
func ConstructEvent(payload []byte, header string, secret []byte) (stripe.Event, error) { | ||
return ConstructEventWithTolerance(payload, header, secret, DefaultTolerance) | ||
} | ||
|
||
|
@@ -109,7 +109,7 @@ func ConstructEvent(payload []byte, header string, secret string) (stripe.Event, | |
// your signing secret from the Stripe dashboard: | ||
// https://dashboard.stripe.com/webhooks | ||
// | ||
func ConstructEventWithTolerance(payload []byte, header string, secret string, tolerance time.Duration) (stripe.Event, error) { | ||
func ConstructEventWithTolerance(payload []byte, header string, secret []byte, tolerance time.Duration) (stripe.Event, error) { | ||
return constructEvent(payload, header, secret, tolerance, true) | ||
} | ||
|
||
|
@@ -122,11 +122,11 @@ func ConstructEventWithTolerance(payload []byte, header string, secret string, t | |
// your signing secret from the Stripe dashboard: | ||
// https://dashboard.stripe.com/webhooks | ||
// | ||
func ConstructEventIgnoringTolerance(payload []byte, header string, secret string) (stripe.Event, error) { | ||
func ConstructEventIgnoringTolerance(payload []byte, header string, secret []byte) (stripe.Event, error) { | ||
return constructEvent(payload, header, secret, 0*time.Second, false) | ||
} | ||
|
||
func constructEvent(payload []byte, sigHeader string, secret string, tolerance time.Duration, enforceTolerance bool) (stripe.Event, error) { | ||
func constructEvent(payload []byte, sigHeader string, secret []byte, tolerance time.Duration, enforceTolerance bool) (stripe.Event, error) { | ||
e := stripe.Event{} | ||
|
||
if err := json.Unmarshal(payload, &e); err != nil { | ||
|
@@ -138,7 +138,7 @@ func constructEvent(payload []byte, sigHeader string, secret string, tolerance t | |
return e, err | ||
} | ||
|
||
expectedSignature := computeSignature(header.timestamp, payload, secret) | ||
expectedSignature := ComputeSignature(header.timestamp, payload, secret) | ||
expiredTimestamp := time.Since(header.timestamp) > tolerance | ||
if enforceTolerance && expiredTimestamp { | ||
return e, ErrTooOld | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix here, but could you make this a sensible sentence? Like
DefaultTolerance is the amount of drift in time that's allowed by ConstructEven as it's validating a signature.