-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
95 lines (83 loc) · 3.1 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package auth
import (
"context"
"encoding/json"
"fmt"
"time"
openContext "github.com/donetkit/wechat/openplatform/context"
"github.com/donetkit/wechat/openplatform/miniprogram/encryptor"
"github.com/donetkit/wechat/util"
)
const (
code2SessionURL = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s"
// 开放平台授权 小程序 SessionKey
MiniProgramSessionKeyCacheKey = "WeiXin:Container:Open:MiniProgramSessionKey:%s:%s"
)
// Auth 登录/用户信息
type Auth struct {
*openContext.Context
appID string // 小程序appId
}
// NewAuth new auth
func NewAuth(opContext *openContext.Context, appID string) *Auth {
return &Auth{Context: opContext, appID: appID}
}
// ResCode2Session 登录凭证校验的返回结果
type ResCode2Session struct {
util.CommonError
OpenID string `json:"openid"` // 用户唯一标识
SessionKey string `json:"session_key"` // 会话密钥
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
}
// Code2Session 登录凭证校验。
func (auth *Auth) Code2Session(ctx context.Context, jsCode string) (result ResCode2Session, err error) {
componentAK, err := auth.GetComponentAccessToken(ctx)
urlStr := fmt.Sprintf(code2SessionURL, auth.appID, jsCode, auth.Context.AppID, componentAK)
var response []byte
response, err = util.HTTPGet(urlStr)
if err != nil {
return
}
err = json.Unmarshal(response, &result)
if err != nil {
return
}
if result.ErrCode != 0 {
err = fmt.Errorf("Code2Session error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
return
}
miniProgramSessionKey := MiniProgramSessionKey{
OpenId: result.OpenID,
UnionId: result.UnionID,
SessionKey: result.SessionKey,
}
res, err := json.Marshal(miniProgramSessionKey)
if err != nil {
err = fmt.Errorf("json Marshal %s SessionKey", "")
return
}
miniProgramSessionKeyCacheKey := fmt.Sprintf(MiniProgramSessionKeyCacheKey, auth.appID, result.OpenID)
auth.Cache.WithContext(ctx).Set(miniProgramSessionKeyCacheKey, string(res), 15*24*3600*time.Second)
return
}
// GetSessionKey 登录SessionKey。
func (auth *Auth) GetSessionKey(ctx context.Context, openID string) (result string, err error) {
miniProgramSessionKeyCacheKey := fmt.Sprintf(MiniProgramSessionKeyCacheKey, auth.appID, openID)
val := auth.Cache.WithContext(ctx).Get(miniProgramSessionKeyCacheKey)
if val == nil {
return "", fmt.Errorf("SessionKey Cache %s", "")
}
miniProgramSessionKey := MiniProgramSessionKey{}
err = json.Unmarshal([]byte(val.(string)), &miniProgramSessionKey)
if err != nil {
return "", fmt.Errorf("json Marshal Session Key %s", "")
}
return miniProgramSessionKey.SessionKey, nil
}
func (auth *Auth) GetDecryptData(sessionKey, encryptedData, iv string) (*encryptor.PlainData, error) {
return encryptor.NewEncryptor(auth.Context, auth.appID).Decrypt(sessionKey, encryptedData, iv)
}
// GetPaidUnionID 用户支付完成后,获取该用户的 UnionId,无需用户授权
func (auth *Auth) GetPaidUnionID() {
//TODO
}