diff --git a/providers/wechat/session.go b/providers/wechat/session.go new file mode 100644 index 000000000..ab0330a77 --- /dev/null +++ b/providers/wechat/session.go @@ -0,0 +1,67 @@ +package wechat + +import ( + "encoding/json" + "errors" + "strings" + "time" + + "github.com/markbates/goth" +) + +// Session stores data during the auth process with Wechat. +type Session struct { + AuthURL string + AccessToken string + RefreshToken string + ExpiresAt time.Time + Openid string + Unionid string +} + +var _ goth.Session = &Session{} + +// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Wepay provider. +func (s Session) GetAuthURL() (string, error) { + if s.AuthURL == "" { + return "", errors.New(goth.NoAuthUrlErrorMessage) + } + return s.AuthURL, nil +} + +// Authorize the session with Wepay and return the access token to be stored for future use. +func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { + p := provider.(*Provider) + token, openid, err := p.fetchToken(params.Get("code")) + + if err != nil { + return "", err + } + + if !token.Valid() { + return "", errors.New("invalid token received from provider") + } + + s.AccessToken = token.AccessToken + s.RefreshToken = token.RefreshToken + s.ExpiresAt = token.Expiry + s.Openid = openid + return token.AccessToken, err +} + +// Marshal the session into a string +func (s Session) Marshal() string { + b, _ := json.Marshal(s) + return string(b) +} + +func (s Session) String() string { + return s.Marshal() +} + +// UnmarshalSession wil unmarshal a JSON string into a session. +func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { + s := &Session{} + err := json.NewDecoder(strings.NewReader(data)).Decode(s) + return s, err +} diff --git a/providers/wechat/session_test.go b/providers/wechat/session_test.go new file mode 100644 index 000000000..df0d49f13 --- /dev/null +++ b/providers/wechat/session_test.go @@ -0,0 +1,48 @@ +package wechat_test + +import ( + "testing" + + "github.com/markbates/goth" + "github.com/markbates/goth/providers/wechat" + "github.com/stretchr/testify/assert" +) + +func Test_Implements_Session(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &wechat.Session{} + + a.Implements((*goth.Session)(nil), s) +} + +func Test_GetAuthURL(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &wechat.Session{} + + _, err := s.GetAuthURL() + a.Error(err) + + s.AuthURL = "/foo" + + url, _ := s.GetAuthURL() + a.Equal(url, "/foo") +} + +func Test_ToJSON(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &wechat.Session{} + + data := s.Marshal() + a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) +} + +func Test_String(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &wechat.Session{} + + a.Equal(s.String(), s.Marshal()) +} diff --git a/providers/wechat/wechat.go b/providers/wechat/wechat.go new file mode 100644 index 000000000..416b5eb04 --- /dev/null +++ b/providers/wechat/wechat.go @@ -0,0 +1,237 @@ +package wechat + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "time" + + "github.com/markbates/goth" + "golang.org/x/oauth2" +) + +const ( + AuthURL = "https://open.weixin.qq.com/connect/qrconnect" + TokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token" + + ScopeSnsapiLogin = "snsapi_login" + + ProfileURL = "https://api.weixin.qq.com/sns/userinfo" +) + +type Provider struct { + providerName string + config *oauth2.Config + httpClient *http.Client + ClientID string + ClientSecret string + RedirectURL string + Lang WechatLangType + + AuthURL string + TokenURL string + ProfileURL string +} + +type WechatLangType string + +const ( + WECHAT_LANG_CN WechatLangType = "cn" + WECHAT_LANG_EN WechatLangType = "en" +) + +// New creates a new Wechat provider, and sets up important connection details. +// You should always call `wechat.New` to get a new Provider. Never try to create +// one manually. +func New(clientID, clientSecret, redirectURL string, lang WechatLangType) *Provider { + p := &Provider{ + providerName: "wechat", + ClientID: clientID, + ClientSecret: clientSecret, + RedirectURL: redirectURL, + Lang: lang, + AuthURL: AuthURL, + TokenURL: TokenURL, + ProfileURL: ProfileURL, + } + p.config = newConfig(p) + return p +} + +// Name is the name used to retrieve this provider later. +func (p *Provider) Name() string { + return p.providerName +} + +// SetName is to update the name of the provider (needed in case of multiple providers of 1 type) +func (p *Provider) SetName(name string) { + p.providerName = name +} + +func (p *Provider) Client() *http.Client { + return goth.HTTPClientWithFallBack(p.httpClient) +} + +// Debug is a no-op for the wechat package. +func (p *Provider) Debug(debug bool) {} + +// BeginAuth asks Wechat for an authentication end-point. +func (p *Provider) BeginAuth(state string) (goth.Session, error) { + params := url.Values{} + params.Add("appid", p.ClientID) + params.Add("response_type", "code") + params.Add("state", state) + params.Add("scope", ScopeSnsapiLogin) + params.Add("redirect_uri", p.RedirectURL) + session := &Session{ + AuthURL: fmt.Sprintf("%s?%s", p.AuthURL, params.Encode()), + } + return session, nil +} + +// FetchUser will go to Wepay and access basic information about the user. +func (p *Provider) FetchUser(session goth.Session) (goth.User, error) { + s := session.(*Session) + user := goth.User{ + AccessToken: s.AccessToken, + Provider: p.Name(), + RefreshToken: s.RefreshToken, + ExpiresAt: s.ExpiresAt, + } + + if user.AccessToken == "" { + // data is not yet retrieved since accessToken is still empty + return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName) + } + + params := url.Values{} + params.Add("access_token", s.AccessToken) + params.Add("openid", s.Openid) + params.Add("lang", string(p.Lang)) + + url := fmt.Sprintf("%s?%s", p.ProfileURL, params.Encode()) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return user, err + } + resp, err := p.Client().Do(req) + if err != nil { + if resp != nil { + resp.Body.Close() + } + return user, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, resp.StatusCode) + } + + err = userFromReader(resp.Body, &user) + return user, err +} + +func newConfig(provider *Provider) *oauth2.Config { + c := &oauth2.Config{ + ClientID: provider.ClientID, + ClientSecret: provider.ClientSecret, + RedirectURL: provider.RedirectURL, + Endpoint: oauth2.Endpoint{ + AuthURL: provider.AuthURL, + TokenURL: provider.TokenURL, + }, + Scopes: []string{}, + } + + c.Scopes = append(c.Scopes, ScopeSnsapiLogin) + + return c +} + +func userFromReader(r io.Reader, user *goth.User) error { + u := struct { + Openid string `json:"openid"` + Nickname string `json:"nickname"` + Sex int `json:"sex"` + Province string `json:"province"` + City string `json:"city"` + Country string `json:"country"` + AvatarURL string `json:"headimgurl"` + Unionid string `json:"unionid"` + Code int `json:"errcode"` + Msg string `json:"errmsg"` + }{} + err := json.NewDecoder(r).Decode(&u) + if err != nil { + return err + } + + if len(u.Msg) > 0 { + return fmt.Errorf("CODE: %d, MSG: %s", u.Code, u.Msg) + } + + user.Email = fmt.Sprintf("%s@wechat.com", u.Openid) + user.Name = u.Nickname + user.UserID = u.Openid + user.NickName = u.Nickname + user.Location = u.City + user.AvatarURL = u.AvatarURL + user.RawData = map[string]interface{}{ + "Unionid": u.Unionid, + } + return nil +} + +// RefreshTokenAvailable refresh token is provided by auth provider or not +func (p *Provider) RefreshTokenAvailable() bool { + return false +} + +// RefreshToken get new access token based on the refresh token +func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) { + + return nil, nil +} + +func (p *Provider) fetchToken(code string) (*oauth2.Token, string, error) { + + params := url.Values{} + params.Add("appid", p.ClientID) + params.Add("secret", p.ClientSecret) + params.Add("grant_type", "authorization_code") + params.Add("code", code) + url := fmt.Sprintf("%s?%s", p.TokenURL, params.Encode()) + resp, err := p.Client().Get(url) + + if err != nil { + return nil, "", err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, "", fmt.Errorf("wechat /gettoken returns code: %d", resp.StatusCode) + } + + obj := struct { + AccessToken string `json:"access_token"` + ExpiresIn time.Duration `json:"expires_in"` + Openid string `json:"openid"` + Code int `json:"errcode"` + Msg string `json:"errmsg"` + }{} + if err = json.NewDecoder(resp.Body).Decode(&obj); err != nil { + return nil, "", err + } + if obj.Code != 0 { + return nil, "", fmt.Errorf("CODE: %d, MSG: %s", obj.Code, obj.Msg) + } + + token := &oauth2.Token{ + AccessToken: obj.AccessToken, + Expiry: time.Now().Add(obj.ExpiresIn * time.Second), + } + + return token, obj.Openid, nil +} diff --git a/providers/wechat/wechat_test.go b/providers/wechat/wechat_test.go new file mode 100644 index 000000000..317f2112d --- /dev/null +++ b/providers/wechat/wechat_test.go @@ -0,0 +1,53 @@ +package wechat_test + +import ( + "os" + "testing" + + "github.com/markbates/goth" + "github.com/markbates/goth/providers/wechat" + "github.com/stretchr/testify/assert" +) + +func Test_New(t *testing.T) { + t.Parallel() + a := assert.New(t) + p := provider() + + a.Equal(p.ClientID, os.Getenv("WECHAT_KEY")) + a.Equal(p.ClientSecret, os.Getenv("WECHAT_SECRET")) + a.Equal(p.RedirectURL, "/foo") +} + +func Test_Implements_Provider(t *testing.T) { + t.Parallel() + a := assert.New(t) + a.Implements((*goth.Provider)(nil), provider()) +} + +func Test_BeginAuth(t *testing.T) { + t.Parallel() + a := assert.New(t) + p := provider() + session, err := p.BeginAuth("test_state") + s := session.(*wechat.Session) + a.NoError(err) + a.Contains(s.AuthURL, "open.weixin.qq.com/connect/qrconnect") +} + +func Test_SessionFromJSON(t *testing.T) { + t.Parallel() + a := assert.New(t) + + p := provider() + session, err := p.UnmarshalSession(`{"AuthURL":"https://open.weixin.qq.com/connect/qrconnect","AccessToken":"1234567890"}`) + a.NoError(err) + + s := session.(*wechat.Session) + a.Equal(s.AuthURL, "https://open.weixin.qq.com/connect/qrconnect") + a.Equal(s.AccessToken, "1234567890") +} + +func provider() *wechat.Provider { + return wechat.New(os.Getenv("WECHAT_KEY"), os.Getenv("WECHAT_SECRET"), "/foo", wechat.WECHAT_LANG_CN) +}