Skip to content

Commit

Permalink
添加功能: 生物认证秘钥签名验证
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Jun 25, 2019
1 parent e52af3e commit b9540d7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [内容检测](#内容检测)
- [检测图片](#检测图片)
- [检测文本](#检测文本)
- [生物认证秘钥签名验证](#生物认证秘钥签名验证)

## 拉取代码

Expand Down Expand Up @@ -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)

```
57 changes: 57 additions & 0 deletions soter.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b9540d7

Please sign in to comment.