feat: complete login

This commit is contained in:
yanghao05
2025-04-15 21:20:04 +08:00
parent 45a0b6848a
commit ca08568e1a
23 changed files with 842 additions and 28 deletions

View File

@@ -5,18 +5,6 @@ import (
"go.ipao.vip/atom/opt"
)
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
return err
}
return container.Container.Provide(func() (*Config, error) {
return &config, nil
}, o.DiOptions()...)
}
const DefaultPrefix = "WeChat"
func DefaultProvider() container.ProviderContainer {
@@ -28,6 +16,28 @@ func DefaultProvider() container.ProviderContainer {
}
}
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
return err
}
return container.Container.Provide(func() (*Config, *Client, error) {
httpClient := DefaultClient
if config.DevMode {
httpClient = httpClient.DevMode()
}
return &config, New(
WithAppID(config.AppID),
WithAppSecret(config.AppSecret),
WithAESKey(config.EncodingAESKey),
WithToken(config.Token),
WithClient(httpClient),
), nil
}, o.DiOptions()...)
}
type Config struct {
AppID string
AppSecret string

View File

@@ -0,0 +1,59 @@
package wechat
import "github.com/pkg/errors"
// -1 系统繁忙,此时请开发者稍候再试
// 0 请求成功
// 40001 AppSecret错误或者AppSecret不属于这个公众号请开发者确认AppSecret的正确性
// 40002 请确保grant_type字段值为client_credential
// 40164 调用接口的IP地址不在白名单中请在接口IP白名单中进行设置。
// 40243 AppSecret已被冻结请登录MP解冻后再次调用。
// 89503 此IP调用需要管理员确认,请联系管理员
// 89501 此IP正在等待管理员确认,请联系管理员
// 89506 24小时内该IP被管理员拒绝调用两次24小时内不可再使用该IP调用
// 89507 1小时内该IP被管理员拒绝调用一次1小时内不可再使用该IP调用
// 10003 redirect_uri域名与后台配置不一致
// 10004 此公众号被封禁
// 10005 此公众号并没有这些scope的权限
// 10006 必须关注此测试号
// 10009 操作太频繁了,请稍后重试
// 10010 scope不能为空
// 10011 redirect_uri不能为空
// 10012 appid不能为空
// 10013 state不能为空
// 10015 公众号未授权第三方平台,请检查授权状态
// 10016 不支持微信开放平台的Appid请使用公众号Appid
func translateError(errCode int, msg string) error {
if errCode == 0 {
return nil
}
errs := map[int]error{
0: nil,
-1: errors.New("系统繁忙,此时请开发者稍候再试"),
40001: errors.New("AppSecret错误或者AppSecret不属于这个公众号请开发者确认AppSecret的正确性"),
40002: errors.New("请确保grant_type字段值为client_credential"),
40164: errors.New("调用接口的IP地址不在白名单中请在接口IP白名单中进行设置"),
40243: errors.New("AppSecret已被冻结请登录MP解冻后再次调用"),
89503: errors.New("此IP调用需要管理员确认,请联系管理员"),
89501: errors.New("此IP正在等待管理员确认,请联系管理员"),
89506: errors.New("24小时内该IP被管理员拒绝调用两次24小时内不可再使用该IP调用"),
89507: errors.New("1小时内该IP被管理员拒绝调用一次1小时内不可再使用该IP调用"),
10003: errors.New("redirect_uri域名与后台配置不一致"),
10004: errors.New("此公众号被封禁"),
10005: errors.New("此公众号并没有这些scope的权限"),
10006: errors.New("必须关注此测试号"),
10009: errors.New("操作太频繁了,请稍后重试"),
10010: errors.New("scope不能为空"),
10011: errors.New("redirect_uri不能为空"),
10012: errors.New("appid不能为空"),
10013: errors.New("state不能为空"),
10015: errors.New("公众号未授权第三方平台,请检查授权状态"),
10016: errors.New("不支持微信开放平台的Appid请使用公众号Appid"),
}
if err, ok := errs[errCode]; ok {
return err
}
return errors.New(msg)
}

View File

@@ -0,0 +1,14 @@
package wechat
import "math/rand"
// RandomString generate random size string
func randomString(size int) (string, error) {
// generate size string [0-9a-zA-Z]
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, size)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
return string(b), nil
}

View File

@@ -0,0 +1,76 @@
package wechat
import (
"net/url"
"github.com/imroc/req/v3"
)
type Options func(*Client)
func WithAppID(appID string) Options {
return func(we *Client) {
we.appID = appID
}
}
// WithAppSecret sets the app secret
func WithAppSecret(appSecret string) Options {
return func(we *Client) {
we.appSecret = appSecret
}
}
// WithToken sets the token
func WithToken(token string) Options {
return func(we *Client) {
we.token = token
}
}
// WithAESKey sets the AES key
func WithAESKey(aesKey string) Options {
return func(we *Client) {
we.aesKey = aesKey
}
}
// WithClient sets the http client
func WithClient(client *req.Client) Options {
return func(we *Client) {
we.client = client
}
}
type ScopeAuthorizeURLOptions func(url.Values)
func ScopeAuthorizeURLWithScope(scope AuthScope) ScopeAuthorizeURLOptions {
return func(v url.Values) {
v.Set("scope", scope.String())
}
}
func ScopeAuthorizeURLWithRedirectURI(uri string) ScopeAuthorizeURLOptions {
return func(v url.Values) {
v.Set("redirect_uri", uri)
}
}
func ScopeAuthorizeURLWithState(state string) ScopeAuthorizeURLOptions {
return func(v url.Values) {
v.Set("state", state)
}
}
func ScopeAuthorizeURLWithForcePopup() ScopeAuthorizeURLOptions {
return func(v url.Values) {
v.Set("forcePopup", "true")
}
}
func WithVerifySiteKeyPair(key, value string) Options {
return func(we *Client) {
we.verifyKey = key
we.verifyValue = value
}
}

View File

@@ -0,0 +1,16 @@
package wechat
type ErrorResponse struct {
ErrCode int `json:"errcode,omitempty"`
ErrMsg string `json:"errmsg,omitempty"`
}
func (r *ErrorResponse) Error() error {
return translateError(r.ErrCode, r.ErrMsg)
}
type AccessTokenResponse struct {
ErrorResponse
AccessToken string `json:"access_token,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"` // seconds
}

View File

@@ -0,0 +1,245 @@
package wechat
import (
"crypto/sha1"
"encoding/hex"
"net/url"
"sort"
"strings"
"time"
"quyun/pkg/oauth"
"github.com/imroc/req/v3"
"github.com/pkg/errors"
)
const BaseURL = "https://api.weixin.qq.com/"
var DefaultClient = req.
NewClient().
SetBaseURL(BaseURL).
SetCommonHeader("Content-Type", "application/json")
const (
ScopeBase = "snsapi_base"
ScopeUserInfo = "snsapi_userinfo"
)
type AuthScope string
func (s AuthScope) String() string {
return string(s)
}
type Client struct {
client *req.Client
appID string
appSecret string
token string
aesKey string
verifyKey string
verifyValue string
}
func New(options ...Options) *Client {
we := &Client{
client: DefaultClient,
}
for _, opt := range options {
opt(we)
}
return we
}
func (we *Client) VerifySite(key string) (string, error) {
if key == we.verifyKey {
return we.verifyValue, nil
}
return "", errors.New("verify failed")
}
func (we *Client) Verify(signature, timestamp, nonce string) error {
params := []string{signature, timestamp, nonce, we.token}
sort.Strings(params)
str := strings.Join(params, "")
hash := sha1.Sum([]byte(str))
hashStr := hex.EncodeToString(hash[:])
if hashStr == signature {
return errors.New("Signature verification failed")
}
return nil
}
func (we *Client) wrapParams(params map[string]string) map[string]string {
if params == nil {
params = make(map[string]string)
}
params["appid"] = we.appID
params["secret"] = we.appSecret
return params
}
func (we *Client) GetAccessToken() (*AccessTokenResponse, error) {
params := map[string]string{
"grant_type": "client_credential",
}
var data ErrorResponse
resp, err := we.client.R().SetSuccessResult(&data).SetQueryParams(params).Get("/cgi-bin/token")
if err != nil {
return nil, errors.Wrap(err, "call /cgi-bin/token failed")
}
if data.ErrCode != 0 {
return nil, data.Error()
}
var token AccessTokenResponse
if err := resp.Unmarshal(&token); err != nil {
return nil, errors.Wrap(err, "parse response failed")
}
return &token, nil
}
// ScopeAuthorizeURL
func (we *Client) ScopeAuthorizeURL(opts ...ScopeAuthorizeURLOptions) (*url.URL, error) {
params := url.Values{}
params.Add("appid", we.appID)
params.Add("response_type", "code")
for _, opt := range opts {
opt(params)
}
if params.Get("scope") == "" {
params.Add("scope", ScopeBase)
}
u, err := url.Parse("https://open.weixin.qq.com/connect/oauth2/authorize")
if err != nil {
return nil, errors.Wrap(err, "parse url failed")
}
u.Fragment = "wechat_redirect"
u.RawQuery = url.Values(params).Encode()
return u, nil
}
var _ oauth.OAuthInfo = (*AuthorizeAccessToken)(nil)
type AuthorizeAccessToken struct {
ErrorResponse
AccessToken string `json:"access_token,omitempty"`
ExpiresIn int64 `json:"expires_in,omitempty"`
IsSnapshotuser int64 `json:"is_snapshotuser,omitempty"`
Openid string `json:"openid,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
Scope string `json:"scope,omitempty"`
Unionid string `json:"unionid,omitempty"`
}
// GetAccessToken implements oauth.OAuthInfo.
func (a *AuthorizeAccessToken) GetAccessToken() string {
return a.AccessToken
}
// GetExpiredAt implements oauth.OAuthInfo.
func (a *AuthorizeAccessToken) GetExpiredAt() time.Time {
return time.Now().Add(time.Duration(a.ExpiresIn) * time.Second)
}
// GetOpenID implements oauth.OAuthInfo.
func (a *AuthorizeAccessToken) GetOpenID() string {
return a.Openid
}
// GetRefreshToken implements oauth.OAuthInfo.
func (a *AuthorizeAccessToken) GetRefreshToken() string {
return a.RefreshToken
}
// GetUnionID implements oauth.OAuthInfo.
func (a *AuthorizeAccessToken) GetUnionID() string {
return a.Unionid
}
func (we *Client) AuthorizeCode2Token(code string) (*AuthorizeAccessToken, error) {
params := we.wrapParams(map[string]string{
"code": code,
"grant_type": "authorization_code",
})
var data AuthorizeAccessToken
_, err := we.client.R().SetSuccessResult(&data).SetQueryParams(params).Get("/sns/oauth2/access_token")
if err != nil {
return nil, errors.Wrap(err, "call /sns/oauth2/access_token failed")
}
if err := data.Error(); err != nil {
return nil, err
}
return &data, nil
}
func (we *Client) AuthorizeRefreshAccessToken(accessToken string) (*AuthorizeAccessToken, error) {
params := we.wrapParams(map[string]string{
"refresh_token": accessToken,
"grant_type": "refresh_token",
})
var data AuthorizeAccessToken
_, err := we.client.R().SetSuccessResult(&data).SetQueryParams(params).Get("/sns/oauth2/refresh_token")
if err != nil {
return nil, errors.Wrap(err, "call /sns/oauth2/refresh_token failed")
}
if err := data.Error(); err != nil {
return nil, err
}
return &data, nil
}
type AuthorizeUserInfo struct {
ErrorResponse
City string `json:"city,omitempty"`
Country string `json:"country,omitempty"`
Headimgurl string `json:"headimgurl,omitempty"`
Nickname string `json:"nickname,omitempty"`
Openid string `json:"openid,omitempty"`
Privilege []string `json:"privilege,omitempty"`
Province string `json:"province,omitempty"`
Sex int64 `json:"sex,omitempty"`
Unionid string `json:"unionid,omitempty"`
}
func (we *Client) AuthorizeUserInfo(accessToken, openID string) (*AuthorizeUserInfo, error) {
params := (map[string]string{
"access_token": accessToken,
"openid": openID,
})
var data AuthorizeUserInfo
_, err := we.client.R().SetSuccessResult(&data).SetQueryParams(params).Get("/sns/userinfo")
if err != nil {
return nil, errors.Wrap(err, "call /sns/userinfo failed")
}
if err := data.Error(); err != nil {
return nil, err
}
return &data, nil
}

View File

@@ -0,0 +1,86 @@
package wechat
import (
"testing"
log "github.com/sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
)
const (
WechatAppID = "wx45745a8c51091ae0"
WechatAppSecret = "2ab33bc79d9b47efa4abef19d66e1977"
WechatToken = "W8Xhw5TivYBgY"
WechatAesKey = "F6AqCxAV4W1eCrY6llJ2zapphKK49CQN3RgtPDrjhnI"
)
func init() {
log.SetLevel(log.DebugLevel)
}
func getClient() *Client {
return New(
WithAppID(WechatAppID),
WithAppSecret(WechatAppSecret),
WithAESKey(WechatAesKey),
WithToken(WechatToken),
WithClient(DefaultClient.DevMode()),
)
}
func TestWechatClient_GetAccessToken(t *testing.T) {
Convey("Test GetAccessToken", t, func() {
token, err := getClient().GetAccessToken()
So(err, ShouldBeNil)
So(token.AccessToken, ShouldNotBeEmpty)
So(token.ExpiresIn, ShouldBeGreaterThan, 0)
t.Log("Access Token:", token.AccessToken)
})
}
func TestClient_ScopeAuthorizeURL(t *testing.T) {
Convey("Test ScopeAuthorizeURL", t, func() {
url, err := getClient().ScopeAuthorizeURL(
ScopeAuthorizeURLWithScope(ScopeBase),
ScopeAuthorizeURLWithRedirectURI("https://qvyun.mp.jdwan.com/"),
)
So(err, ShouldBeNil)
So(url, ShouldNotBeEmpty)
t.Log("URL:", url)
})
}
func TestClient_AuthorizeCode2Token(t *testing.T) {
code := "011W1sll2Xv4Ae4OjUnl2I7jvd2W1slX"
Convey("Test AuthorizeCode2Token", t, func() {
token, err := getClient().AuthorizeCode2Token(code)
So(err, ShouldBeNil)
t.Logf("token: %+v", token)
})
}
func TestClient_AuthorizeRefreshAccessToken(t *testing.T) {
token := "86_m_EAHq0RKlo6RzzGAsY8gVmiCqHqIiAJufxhm8mK8imyIW6yoE4NTcIr2vaukp7dexPWId0JWP1iZWYaLpXT_MJv1N7YQW8Qt3zOZDpJY90"
Convey("Test AuthorizeCode2Token", t, func() {
token, err := getClient().AuthorizeRefreshAccessToken(token)
So(err, ShouldBeNil)
t.Logf("token: %+v", token)
})
}
func TestClient_AuthorizeUserInfo(t *testing.T) {
token := "86_ZxJa8mIwbml5mDlHHbIUle_UKW8LA75nOuB0wqiome8AX5LlMWU8JwRKMZykdLEjDnKX8EJavz5GeQn3T1ot7TwpULp8imQvNIgFIjC4er8"
openID := "oMLa5tyJ2vRHa-HI4CMEkHztq3eU"
Convey("Test AuthorizeUserInfo", t, func() {
user, err := getClient().AuthorizeUserInfo(token, openID)
So(err, ShouldBeNil)
t.Logf("user: %+v", user)
})
}