-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
355 additions
and
25 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 |
---|---|---|
|
@@ -13,13 +13,13 @@ jobs: | |
strategy: | ||
matrix: | ||
go: | ||
- "1.22" | ||
- "1.21" | ||
- "1.20" | ||
- "1.19" | ||
- "1.18" | ||
- "1.17" | ||
- "1.16" | ||
- "1.15" | ||
- "1.14" | ||
- "1.13" | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
|
@@ -39,10 +39,10 @@ jobs: | |
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: "1.16" | ||
go-version: "1.19" | ||
- name: staticcheck | ||
run: | | ||
go get -u honnef.co/go/tools/cmd/staticcheck@v0.2.2 && | ||
go install honnef.co/go/tools/cmd/staticcheck@latest && | ||
$HOME/go/bin/staticcheck ./... | ||
- name: Revive Action | ||
uses: morphy2k/[email protected] | ||
|
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,45 @@ | ||
// Copyright 2024 Tencent Inc. All rights reserved. | ||
|
||
// Package verifiers 微信支付 API v3 Go SDK 数字签名验证器 | ||
package verifiers | ||
|
||
import ( | ||
"context" | ||
"crypto" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
) | ||
|
||
// SHA256WithRSAPubkeyVerifier 数字签名验证器,使用微信支付提供的公钥验证签名 | ||
type SHA256WithRSAPubkeyVerifier struct { | ||
keyID string | ||
publicKey rsa.PublicKey | ||
} | ||
|
||
// Verify 使用微信支付提供的公钥验证签名 | ||
func (v *SHA256WithRSAPubkeyVerifier) Verify(ctx context.Context, serialNumber, message, signature string) error { | ||
if ctx == nil { | ||
return fmt.Errorf("verify failed: context is nil") | ||
} | ||
if v.keyID != serialNumber { | ||
return fmt.Errorf("verify failed: key-id[%s] does not match serial number[%s]", v.keyID, serialNumber) | ||
} | ||
|
||
sigBytes, err := base64.StdEncoding.DecodeString(signature) | ||
if err != nil { | ||
return fmt.Errorf("verify failed: signature is not base64 encoded") | ||
} | ||
hashed := sha256.Sum256([]byte(message)) | ||
err = rsa.VerifyPKCS1v15(&v.publicKey, crypto.SHA256, hashed[:], sigBytes) | ||
if err != nil { | ||
return fmt.Errorf("verify signature with public key error:%s", err.Error()) | ||
} | ||
return nil | ||
} | ||
|
||
// NewSHA256WithRSAPubkeyVerifier 使用 rsa.PublicKey 初始化验签器 | ||
func NewSHA256WithRSAPubkeyVerifier(keyID string, publicKey rsa.PublicKey) *SHA256WithRSAPubkeyVerifier { | ||
return &SHA256WithRSAPubkeyVerifier{keyID: keyID, publicKey: publicKey} | ||
} |
154 changes: 154 additions & 0 deletions
154
core/auth/verifiers/sha256withrsa_pubkey_verifier_test.go
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,154 @@ | ||
// Copyright 2021 Tencent Inc. All rights reserved. | ||
|
||
package verifiers | ||
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"testing" | ||
|
||
"github.com/wechatpay-apiv3/wechatpay-go/utils" | ||
) | ||
|
||
const ( | ||
testPubKeyID = "F5765756002FDD77" | ||
testPubKey = `-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2VCTd91fnUn73Xy9DLvt | ||
/V62TVxRTEEstVdeRaZ3B3leO0pldE806mXO4RwdHXagHQ4vGeZN0yqm++rDsGK+ | ||
U3AH7kejyD2pXshNP9Cq5YwbptiLGtjcquw4HNxJQUOmDeJf2vg6byms9RUipiq4 | ||
SzbJKqJFlUpbuIPDpSpWz10PYmyCNeDGUUK65E5h2B834uxl1zNLYQCrkdBzb8oU | ||
xwYeP5a2DNxmjL5lsJML7DGr5znsevnoqGRwTm9fxCGfy8wus7hwKz6clt3Whmmd | ||
a7UAdb1c08hEQFVRbF14AR73xbnd8N0obCWJPCbzMCtkaSef4FdEEgEXJiw0VAJT | ||
8wIDAQAB | ||
-----END PUBLIC KEY-----` | ||
// testExpectedSignature = "BKyAfU4iMCuvXMXS0Wzam3V/cnxZ+JaqigPM5OhljS2iOT95OO6Fsuml2JkFANJU9" + | ||
// "K6q9bLlDhPXuoVz+pp4hAm6pHU4ld815U4jsKu1RkyaII+1CYBUYC8TK0XtJ8FwUXXz8vZHh58rrAVN1XwNyv" + | ||
// "D1vfpxrMT4SL536GLwvpUHlCqIMzoZUguLli/K8V29QiOhuH6IEqLNJn8e9b3nwNcQ7be3CzYGpDAKBfDGPCq" + | ||
// "Cv8Rw5zndhlffk2FEA70G4hvMwe51qMN/RAJbknXG23bSlObuTCN7Ndj1aJGH6/L+hdwfLpUtJm4QYVazzW7D" + | ||
// "FD27EpSQEqA8bX9+8m1rLg==" | ||
) | ||
|
||
var ( | ||
pubKey *rsa.PublicKey | ||
) | ||
|
||
func init() { | ||
var err error | ||
pubKey, err = utils.LoadPublicKey(testPubKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestWechatPayPubKeyVerifier(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
serialNumber string | ||
message string | ||
signature string | ||
} | ||
tests := []struct { | ||
name string | ||
fields *rsa.PublicKey | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "verify success", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: testPubKeyID, | ||
signature: testExpectedSignature, | ||
message: "source", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "verify failed", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: testPubKeyID, | ||
signature: testExpectedSignature, | ||
message: "wrong source", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "verify failed with null context", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: nil, | ||
serialNumber: testWechatPayVerifierPlatformSerialNumber, | ||
signature: testExpectedSignature, | ||
message: "source", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "verify failed with empty keyId", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: "", | ||
signature: testExpectedSignature, | ||
message: "source", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "verify failed with empty message", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: testPubKeyID, | ||
signature: testExpectedSignature, | ||
message: "", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "verify failed with empty signature", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: testPubKeyID, | ||
signature: "", | ||
message: "source", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "verify failed with non-base64 signature", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: testPubKeyID, | ||
signature: "invalid base64 signature", | ||
message: "source", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "verify failed with no corresponding pubkey", | ||
fields: pubKey, | ||
args: args{ | ||
ctx: context.Background(), | ||
serialNumber: "invalid serial number", | ||
signature: testExpectedSignature, | ||
message: "source", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var verifier = NewSHA256WithRSAPubkeyVerifier(testPubKeyID, *tt.fields) | ||
if err := verifier.Verify(tt.args.ctx, tt.args.serialNumber, tt.args.message, | ||
tt.args.signature); (err != nil) != tt.wantErr { | ||
t.Errorf("Verify() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.