|
|
|
@@ -1,4 +1,4 @@
|
|
|
|
|
package oauth2
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
@@ -7,16 +7,35 @@ import (
|
|
|
|
|
"git.esin.io/lab/weixin/clientapi/request"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Oauth2Client struct {
|
|
|
|
|
type Config struct {
|
|
|
|
|
ClientID string
|
|
|
|
|
ClientSecret string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
appId string
|
|
|
|
|
appSecret string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetCodeURLRequest struct {
|
|
|
|
|
RedirectURL, State, Scope string
|
|
|
|
|
func NewClient(cfg *Config) *Client {
|
|
|
|
|
return &Client{
|
|
|
|
|
appId: cfg.ClientID,
|
|
|
|
|
appSecret: cfg.ClientSecret,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Oauth2Client) GetCodeURL(redirectURL, state, scope string) string {
|
|
|
|
|
type Scope string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ScopeBase Scope = "snsapi_base"
|
|
|
|
|
ScopeUserinfo Scope = "snsapi_userinfo"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (scope Scope) String() string {
|
|
|
|
|
return string(scope)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Client) GetCodeURL(redirectURL, state string, scope Scope) string {
|
|
|
|
|
endpoint := url.URL{
|
|
|
|
|
Scheme: "https",
|
|
|
|
|
Host: "open.weixin.qq.com",
|
|
|
|
@@ -25,7 +44,7 @@ func (c Oauth2Client) GetCodeURL(redirectURL, state, scope string) string {
|
|
|
|
|
"appid": {c.appId},
|
|
|
|
|
"redirect_uri": {redirectURL},
|
|
|
|
|
"response_type": {"code"},
|
|
|
|
|
"scope": {scope},
|
|
|
|
|
"scope": {scope.String()},
|
|
|
|
|
"state": {state},
|
|
|
|
|
}.Encode(),
|
|
|
|
|
Fragment: "wechat_redirect",
|
|
|
|
@@ -33,7 +52,7 @@ func (c Oauth2Client) GetCodeURL(redirectURL, state, scope string) string {
|
|
|
|
|
return endpoint.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Oauth2Client) ExchangeToken(ctx context.Context, code string) (*Token, error) {
|
|
|
|
|
func (c Client) ExchangeToken(ctx context.Context, code string) (*Token, error) {
|
|
|
|
|
req := request.New(
|
|
|
|
|
"/sns/oauth2/access_token",
|
|
|
|
|
url.Values{
|
|
|
|
@@ -54,7 +73,19 @@ func (c Oauth2Client) ExchangeToken(ctx context.Context, code string) (*Token, e
|
|
|
|
|
return &resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Oauth2Client) GetUserinfo(ctx context.Context, accessToken, openid string) (*Userinfo, error) {
|
|
|
|
|
type Lang string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
LangZhCN Lang = "zh_CN"
|
|
|
|
|
LangZhTW Lang = "zh_TW"
|
|
|
|
|
LangEn Lang = "en"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (lang Lang) String() string {
|
|
|
|
|
return string(lang)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Client) GetUserinfo(ctx context.Context, accessToken, openid string, lang Lang) (*Userinfo, error) {
|
|
|
|
|
req := request.New(
|
|
|
|
|
"/sns/userinfo",
|
|
|
|
|
url.Values{
|
|
|
|
@@ -74,7 +105,7 @@ func (c Oauth2Client) GetUserinfo(ctx context.Context, accessToken, openid strin
|
|
|
|
|
return &resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Oauth2Client) RefreshToken(ctx context.Context, refreshToken, openid string) (*Token, error) {
|
|
|
|
|
func (c Client) RefreshToken(ctx context.Context, refreshToken string) (*Token, error) {
|
|
|
|
|
req := request.New(
|
|
|
|
|
"/sns/oauth2/refresh_token",
|
|
|
|
|
url.Values{
|
|
|
|
@@ -95,7 +126,7 @@ func (c Oauth2Client) RefreshToken(ctx context.Context, refreshToken, openid str
|
|
|
|
|
return &resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Oauth2Client) ValidateToken(ctx context.Context, accessToken, openid string) error {
|
|
|
|
|
func (c Client) ValidateToken(ctx context.Context, accessToken, openid string) error {
|
|
|
|
|
req := request.New(
|
|
|
|
|
"/sns/auth",
|
|
|
|
|
url.Values{
|
|
|
|
@@ -115,7 +146,7 @@ func (c Oauth2Client) ValidateToken(ctx context.Context, accessToken, openid str
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Oauth2Client) GetClientCredential(ctx context.Context) (*ClientCredential, error) {
|
|
|
|
|
func (c Client) GetClientCredential(ctx context.Context) (*ClientCredential, error) {
|
|
|
|
|
req := request.New(
|
|
|
|
|
"/cgi-bin/token",
|
|
|
|
|
url.Values{
|