From f48e04ef15182e7ce82061cfe9ffa070e83514da Mon Sep 17 00:00:00 2001 From: Weihao <1340908470@qq.com> Date: Sun, 16 May 2021 00:22:15 +0800 Subject: [PATCH] refactor: remove ClientId/ClientSecret/RedirectUrl in IdProvider int /idp/github.go & google.go & qq.go; alter Scopes in QqIdProvider.Config: profile&email -> get_user_info Signed-off-by: Weihao <1340908470@qq.com> --- idp/github.go | 13 +++---------- idp/google.go | 13 +++---------- idp/qq.go | 23 ++++++++--------------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/idp/github.go b/idp/github.go index 87463138..21b328cf 100644 --- a/idp/github.go +++ b/idp/github.go @@ -25,19 +25,12 @@ import ( ) type GithubIdProvider struct { - Client *http.Client - Config *oauth2.Config - ClientId string - ClientSecret string - RedirectUrl string + Client *http.Client + Config *oauth2.Config } func NewGithubIdProvider(clientId string, clientSecret string, redirectUrl string) *GithubIdProvider { - idp := &GithubIdProvider{ - ClientId: clientId, - ClientSecret: clientSecret, - RedirectUrl: redirectUrl, - } + idp := &GithubIdProvider{} config := idp.getConfig() config.ClientID = clientId diff --git a/idp/google.go b/idp/google.go index df2c6a70..001106f9 100644 --- a/idp/google.go +++ b/idp/google.go @@ -25,19 +25,12 @@ import ( ) type GoogleIdProvider struct { - Client *http.Client - Config *oauth2.Config - ClientId string - ClientSecret string - RedirectUrl string + Client *http.Client + Config *oauth2.Config } func NewGoogleIdProvider(clientId string, clientSecret string, redirectUrl string) *GoogleIdProvider { - idp := &GoogleIdProvider{ - ClientId: clientId, - ClientSecret: clientSecret, - RedirectUrl: redirectUrl, - } + idp := &GoogleIdProvider{} config := idp.getConfig() config.ClientID = clientId diff --git a/idp/qq.go b/idp/qq.go index f3aec345..8844fa1a 100644 --- a/idp/qq.go +++ b/idp/qq.go @@ -27,19 +27,12 @@ import ( ) type QqIdProvider struct { - Client *http.Client - Config *oauth2.Config - ClientId string - ClientSecret string - RedirectUrl string + Client *http.Client + Config *oauth2.Config } func NewQqIdProvider(clientId string, clientSecret string, redirectUrl string) *QqIdProvider { - idp := &QqIdProvider{ - ClientId: clientId, - ClientSecret: clientSecret, - RedirectUrl: redirectUrl, - } + idp := &QqIdProvider{} config := idp.getConfig() config.ClientID = clientId @@ -60,7 +53,7 @@ func (idp *QqIdProvider) getConfig() *oauth2.Config { } var config = &oauth2.Config{ - Scopes: []string{"profile", "email"}, + Scopes: []string{"get_user_info"}, Endpoint: endpoint, } @@ -70,10 +63,10 @@ func (idp *QqIdProvider) getConfig() *oauth2.Config { func (idp *QqIdProvider) GetToken(code string) (*oauth2.Token, error) { params := url.Values{} params.Add("grant_type", "authorization_code") - params.Add("client_id", idp.ClientId) - params.Add("client_secret", idp.ClientSecret) + params.Add("client_id", idp.Config.ClientID) + params.Add("client_secret", idp.Config.ClientSecret) params.Add("code", code) - params.Add("redirect_uri", idp.RedirectUrl) + params.Add("redirect_uri", idp.Config.RedirectURL) getAccessTokenUrl := fmt.Sprintf("https://graph.qq.com/oauth2.0/token?%s", params.Encode()) tokenResponse, err := idp.Client.Get(getAccessTokenUrl) @@ -113,7 +106,7 @@ func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) { return nil, errors.New("openId is empty") } - getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token.AccessToken, idp.ClientId, openId) + getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token.AccessToken, idp.Config.ClientID, openId) getUserInfoResponse, err := idp.Client.Get(getUserInfoUrl) if err != nil { return nil, err