Skip to content

Commit 94425dc

Browse files
authored
Merge pull request #504 from ArtisanCloud/develop
Develop
2 parents 209394c + fa7c98a commit 94425dc

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/kernel/baseClient.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"strconv"
1111
"strings"
12+
"time"
1213

1314
"github.com/ArtisanCloud/PowerLibs/v3/http/contract"
1415
"github.com/ArtisanCloud/PowerLibs/v3/http/helper"
@@ -53,13 +54,15 @@ func NewBaseClient(app *ApplicationInterface, token *AccessToken) (*BaseClient,
5354
config := (*app).GetConfig()
5455
baseURI := config.GetString("http.base_uri", "/")
5556
proxyURI := config.GetString("http.proxy_uri", "")
57+
timeout := config.GetFloat64("http.timeout", 5)
5658

5759
if token == nil {
5860
token = (*app).GetAccessToken()
5961
}
6062
h, err := helper.NewRequestHelper(&helper.Config{
6163
BaseUrl: baseURI,
6264
ClientConfig: &contract.ClientConfig{
65+
Timeout: time.Duration(timeout * float64(time.Second)),
6366
ProxyURI: proxyURI,
6467
},
6568
})

src/kernel/support/signer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package support
33
import (
44
"context"
55
"crypto"
6+
"crypto/hmac"
67
"crypto/rand"
78
"crypto/rsa"
9+
"crypto/sha256"
810
"crypto/x509"
911
"encoding/base64"
1012
"encoding/pem"
13+
"errors"
1114
"fmt"
1215
"github.com/ArtisanCloud/PowerLibs/v3/object"
1316
"io/ioutil"
@@ -186,3 +189,23 @@ func SignSHA256WithRSA(source string, privateKey *rsa.PrivateKey) (signature str
186189
}
187190
return base64.StdEncoding.EncodeToString(signatureByte), nil
188191
}
192+
193+
func SignSHA256WithHMac(sessionKey []byte, input string) ([]byte, error) {
194+
if len(sessionKey) == 0 {
195+
return nil, errors.New("session key is empty")
196+
}
197+
198+
// Create a new HMAC SHA256 object
199+
hmac := hmac.New(sha256.New, sessionKey)
200+
201+
// Write the input string to the HMAC object
202+
inputBytes := []byte(input)
203+
if _, err := hmac.Write(inputBytes); err != nil {
204+
return nil, err
205+
}
206+
207+
// Get the HMAC signature
208+
signature := hmac.Sum(nil)
209+
210+
return signature, nil
211+
}

src/miniProgram/auth/client.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"github.com/ArtisanCloud/PowerLibs/v3/object"
66
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
7+
response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
8+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
79
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/auth/response"
810
)
911

@@ -40,3 +42,53 @@ func (comp *Client) Session(ctx context.Context, code string) (*response.Respons
4042

4143
return result, err
4244
}
45+
46+
// 检验登录态。
47+
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/checkSessionKey.html
48+
func (comp *Client) CheckSession(ctx context.Context, openId string, sessionKey string) (*response.ResponseCode2Session, error) {
49+
50+
result := &response.ResponseCode2Session{}
51+
52+
config := (*comp.BaseClient.App).GetConfig()
53+
54+
sign, err := support.SignSHA256WithHMac([]byte(sessionKey), "")
55+
if err != nil {
56+
return nil, err
57+
}
58+
params := &object.StringMap{
59+
"appid": config.GetString("app_id", ""),
60+
"secret": config.GetString("secret", ""),
61+
"openid": openId,
62+
"signature": string(sign),
63+
"sig_method": "hmac_sha256",
64+
}
65+
66+
_, err = comp.BaseClient.HttpGet(ctx, "wxa/checksession", params, nil, result)
67+
68+
return result, err
69+
}
70+
71+
// 重置登录态。
72+
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/ResetUserSessionKey.html
73+
func (comp *Client) ResetUserSessionKey(ctx context.Context, openId string, sessionKey string) (*response2.ResponseMiniProgram, error) {
74+
75+
result := &response2.ResponseMiniProgram{}
76+
77+
config := (*comp.BaseClient.App).GetConfig()
78+
79+
sign, err := support.SignSHA256WithHMac([]byte(sessionKey), "")
80+
if err != nil {
81+
return nil, err
82+
}
83+
params := &object.StringMap{
84+
"appid": config.GetString("app_id", ""),
85+
"secret": config.GetString("secret", ""),
86+
"openid": openId,
87+
"signature": string(sign),
88+
"sig_method": "hmac_sha256",
89+
}
90+
91+
_, err = comp.BaseClient.HttpGet(ctx, "wxa/resetusersessionkey", params, nil, result)
92+
93+
return result, err
94+
}

0 commit comments

Comments
 (0)