Skip to content

Commit

Permalink
Merge pull request #504 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Matrix-X committed Jun 16, 2024
2 parents 209394c + fa7c98a commit 94425dc
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/kernel/baseClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

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

if token == nil {
token = (*app).GetAccessToken()
}
h, err := helper.NewRequestHelper(&helper.Config{
BaseUrl: baseURI,
ClientConfig: &contract.ClientConfig{
Timeout: time.Duration(timeout * float64(time.Second)),
ProxyURI: proxyURI,
},
})
Expand Down
23 changes: 23 additions & 0 deletions src/kernel/support/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package support
import (
"context"
"crypto"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"io/ioutil"
Expand Down Expand Up @@ -186,3 +189,23 @@ func SignSHA256WithRSA(source string, privateKey *rsa.PrivateKey) (signature str
}
return base64.StdEncoding.EncodeToString(signatureByte), nil
}

func SignSHA256WithHMac(sessionKey []byte, input string) ([]byte, error) {
if len(sessionKey) == 0 {
return nil, errors.New("session key is empty")
}

// Create a new HMAC SHA256 object
hmac := hmac.New(sha256.New, sessionKey)

// Write the input string to the HMAC object
inputBytes := []byte(input)
if _, err := hmac.Write(inputBytes); err != nil {
return nil, err
}

// Get the HMAC signature
signature := hmac.Sum(nil)

return signature, nil
}
52 changes: 52 additions & 0 deletions src/miniProgram/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/auth/response"
)

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

return result, err
}

// 检验登录态。
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/checkSessionKey.html
func (comp *Client) CheckSession(ctx context.Context, openId string, sessionKey string) (*response.ResponseCode2Session, error) {

result := &response.ResponseCode2Session{}

config := (*comp.BaseClient.App).GetConfig()

sign, err := support.SignSHA256WithHMac([]byte(sessionKey), "")
if err != nil {
return nil, err
}
params := &object.StringMap{
"appid": config.GetString("app_id", ""),
"secret": config.GetString("secret", ""),
"openid": openId,
"signature": string(sign),
"sig_method": "hmac_sha256",
}

_, err = comp.BaseClient.HttpGet(ctx, "wxa/checksession", params, nil, result)

return result, err
}

// 重置登录态。
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/ResetUserSessionKey.html
func (comp *Client) ResetUserSessionKey(ctx context.Context, openId string, sessionKey string) (*response2.ResponseMiniProgram, error) {

result := &response2.ResponseMiniProgram{}

config := (*comp.BaseClient.App).GetConfig()

sign, err := support.SignSHA256WithHMac([]byte(sessionKey), "")
if err != nil {
return nil, err
}
params := &object.StringMap{
"appid": config.GetString("app_id", ""),
"secret": config.GetString("secret", ""),
"openid": openId,
"signature": string(sign),
"sig_method": "hmac_sha256",
}

_, err = comp.BaseClient.HttpGet(ctx, "wxa/resetusersessionkey", params, nil, result)

return result, err
}

0 comments on commit 94425dc

Please sign in to comment.