Merge pull request #61 from 1340908470/refactor-IdProvider

refactor: remove ClientId/ClientSecret/RedirectUrl in IdProvider
This commit is contained in:
hsluoyz 2021-05-16 09:46:24 +08:00 committed by GitHub
commit 338c589e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 35 deletions

View File

@ -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

View File

@ -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

View File

@ -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