From b9540d7a0a8a96ada81b80ce94d386c103a19d7f Mon Sep 17 00:00:00 2001 From: Medivh Date: Tue, 25 Jun 2019 18:16:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD:=20?= =?UTF-8?q?=E7=94=9F=E7=89=A9=E8=AE=A4=E8=AF=81=E7=A7=98=E9=92=A5=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 24 +++++++++++++++++++++++ soter.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 soter.go 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 +}