-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Kachit/signature
Signature
- Loading branch information
Showing
7 changed files
with
219 additions
and
5 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
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,64 @@ | ||
package dusupay | ||
|
||
import ( | ||
"bytes" | ||
"crypto" | ||
"crypto/rsa" | ||
"crypto/sha512" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
//IncomingWebhookInterface interface | ||
type IncomingWebhookInterface interface { | ||
BuildPayloadString(url string) string | ||
} | ||
|
||
//NewSignatureValidator method | ||
func NewSignatureValidator(publicKeyBytes []byte) (*SignatureValidator, error) { | ||
block, _ := pem.Decode(publicKeyBytes) | ||
if block == nil { | ||
return nil, errors.New("wrong public key data") | ||
} | ||
publicKey, err := parsePublicKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &SignatureValidator{publicKey}, nil | ||
} | ||
|
||
//SignatureValidator struct | ||
type SignatureValidator struct { | ||
publicKey *rsa.PublicKey | ||
} | ||
|
||
//ValidateSignature method (see https://docs.dusupay.com/webhooks-and-redirects/webhooks/signature-verification) | ||
func (sv *SignatureValidator) ValidateSignature(webhook IncomingWebhookInterface, webhookUrl string, signature string) error { | ||
messageBytes := bytes.NewBufferString(webhook.BuildPayloadString(webhookUrl)) | ||
hash := sha512.New() | ||
hash.Write(messageBytes.Bytes()) | ||
digest := hash.Sum(nil) | ||
|
||
data, err := base64.StdEncoding.DecodeString(signature) | ||
if err != nil { | ||
return err | ||
} | ||
return rsa.VerifyPKCS1v15(sv.publicKey, crypto.SHA512, digest, data) | ||
} | ||
|
||
//parsePublicKey method | ||
func parsePublicKey(rawBytes []byte) (*rsa.PublicKey, error) { | ||
key, err := x509.ParsePKIXPublicKey(rawBytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsePublicKey wrong parse PKIX: %v", err) | ||
} | ||
switch pk := key.(type) { | ||
case *rsa.PublicKey: | ||
return pk, nil | ||
default: | ||
return nil, errors.New("parsePublicKey: PublicKey must be of type rsa.PublicKey") | ||
} | ||
} |
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,72 @@ | ||
package dusupay | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
type SignatureTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestValidateSignatureCollectionWebhookSuccess() { | ||
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem") | ||
webhook := &CollectionWebhook{ID: 226, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"} | ||
validator, _ := NewSignatureValidator(rawBytes) | ||
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature) | ||
assert.NoError(suite.T(), err) | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestValidateSignatureCollectionWebhookWrongPayload() { | ||
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem") | ||
webhook := &CollectionWebhook{ID: 225, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"} | ||
validator, _ := NewSignatureValidator(rawBytes) | ||
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature) | ||
assert.Error(suite.T(), err) | ||
assert.Equal(suite.T(), "crypto/rsa: verification error", err.Error()) | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestValidateSignatureCollectionWebhookNonBase64Signature() { | ||
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem") | ||
webhook := &CollectionWebhook{ID: 225, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"} | ||
validator, _ := NewSignatureValidator(rawBytes) | ||
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", "qwerty") | ||
assert.Error(suite.T(), err) | ||
assert.Equal(suite.T(), "illegal base64 data at input byte 4", err.Error()) | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestValidateSignaturePayoutWebhookSuccess() { | ||
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem") | ||
webhook := &PayoutWebhook{ID: 226, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"} | ||
validator, _ := NewSignatureValidator(rawBytes) | ||
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature) | ||
assert.NoError(suite.T(), err) | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestValidateSignatureRefundWebhookSuccess() { | ||
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem") | ||
webhook := &RefundWebhook{ID: 226, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"} | ||
validator, _ := NewSignatureValidator(rawBytes) | ||
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature) | ||
assert.NoError(suite.T(), err) | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestParsePublicKeyError() { | ||
pk, err := parsePublicKey([]byte(`foo`)) | ||
assert.Error(suite.T(), err) | ||
assert.Contains(suite.T(), err.Error(), "parsePublicKey wrong parse PKIX: asn1: structure error: tags don't match") | ||
assert.Nil(suite.T(), pk) | ||
} | ||
|
||
func (suite *SignatureTestSuite) TestNewSignatureValidatorError() { | ||
validator, err := NewSignatureValidator([]byte(`foo`)) | ||
assert.Error(suite.T(), err) | ||
assert.Contains(suite.T(), err.Error(), "wrong public key data") | ||
assert.Nil(suite.T(), validator) | ||
} | ||
|
||
func TestSignatureTestSuite(t *testing.T) { | ||
suite.Run(t, new(SignatureTestSuite)) | ||
} |
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,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmiVRA6VUyxaLCj1BC2Ij | ||
ImHSyPxmS7Zvoyima3WJRcm1AtMdibLdWB4NJJFN7CyGjjZ18R7lB1CJjxWNbFIE | ||
pwM/NahyBDYXfYwyzkkiqLIwndSLIBzpLTBUJj0EArLCVHDLAIooYP3DWkSz6yj2 | ||
S3WT4NgYAptJP0yFv9rsnfzIMFXGiPUQeVEuoxiUXX/usTg1wWXLVZtL/BLJS98D | ||
Sr7X65gwfEqgCaQOCJWRoUlsjfnNi3uEqSmQisRqrFk5eiOsfo4WaBohhOStu4nU | ||
GKy/G/2JxOTJVaXvyRv8Ejj5mxxyMp0XcjdpSOa6OajxVgrOIRVGnJxwSWLQRpOu | ||
FwIDAQAB | ||
-----END PUBLIC KEY----- |
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
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