diff --git a/Readme.md b/Readme.md index 1aae75b..f4bee1d 100644 --- a/Readme.md +++ b/Readme.md @@ -35,6 +35,7 @@ - [内容检测](#内容检测) - [检测图片](#检测图片) - [检测文本](#检测文本) +- [生物认证秘钥签名验证](#生物认证秘钥签名验证) ## 拉取代码 @@ -750,3 +751,26 @@ if err != nil { fmt.Printf("返回结果: %#v", res) ``` + +--- + +## 生物认证秘钥签名验证 + +[官方文档](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/soter/soter.verifySignature.html) + +```go + +import "github.com/medivhzhan/weapp" + +res, err := VerifySignature("access-token", "user_openid", "result-json", "result-json-signature") +if err != nil { + // handle error + return +} + +// res.IsOk +// res.Errcode +// res.Errmsg +fmt.Printf("返回结果: %#v", res) + +``` diff --git a/soter.go b/soter.go new file mode 100644 index 0000000..76cfe84 --- /dev/null +++ b/soter.go @@ -0,0 +1,57 @@ +package weapp + +import ( + "bytes" + "encoding/json" + "net/http" + + "github.com/medivhzhan/weapp/util" +) + +const ( + verifySignatureAPI = "/cgi-bin/soter/verify_signature" +) + +// VerifySignatureResponse 生物认证秘钥签名验证请求返回数据 +type VerifySignatureResponse struct { + Response + IsOk bool `json:"is_ok"` +} + +// VerifySignature 生物认证秘钥签名验证 +// @accessToken 接口调用凭证 +// @openID 用户 openid +// @data 通过 wx.startSoterAuthentication 成功回调获得的 resultJSON 字段 +// @signature 通过 wx.startSoterAuthentication 成功回调获得的 resultJSONSignature 字段 +func VerifySignature(accessToken, openID, data, signature string) (*VerifySignatureResponse, error) { + api, err := util.TokenAPI(BaseURL+verifySignatureAPI, accessToken) + if err != nil { + return nil, err + } + + verifier := map[string]string{ + "openid": openID, + "json_string": data, + "json_signature": signature, + } + + raw, err := json.Marshal(verifier) + if err != nil { + return nil, err + } + + resp, err := http.Post(api, "application/json", bytes.NewReader(raw)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + res := new(VerifySignatureResponse) + + err = json.NewDecoder(resp.Body).Decode(res) + if err != nil { + return nil, err + } + + return res, nil +}