2021-03-22 20:00:35 +08:00
|
|
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package idp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-03-23 23:48:18 +08:00
|
|
|
"net/url"
|
2021-03-22 20:00:35 +08:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QqIdProvider struct {
|
2021-05-16 00:22:15 +08:00
|
|
|
Client *http.Client
|
|
|
|
Config *oauth2.Config
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:23:59 +08:00
|
|
|
func NewQqIdProvider(clientId string, clientSecret string, redirectUrl string) *QqIdProvider {
|
2021-05-16 00:22:15 +08:00
|
|
|
idp := &QqIdProvider{}
|
2021-03-23 23:23:59 +08:00
|
|
|
|
|
|
|
config := idp.getConfig()
|
|
|
|
config.ClientID = clientId
|
|
|
|
config.ClientSecret = clientSecret
|
|
|
|
config.RedirectURL = redirectUrl
|
|
|
|
idp.Config = config
|
|
|
|
|
|
|
|
return idp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idp *QqIdProvider) SetHttpClient(client *http.Client) {
|
|
|
|
idp.Client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idp *QqIdProvider) getConfig() *oauth2.Config {
|
2021-03-22 20:00:35 +08:00
|
|
|
var endpoint = oauth2.Endpoint{
|
|
|
|
TokenURL: "https://graph.qq.com/oauth2.0/token",
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = &oauth2.Config{
|
2021-05-16 00:22:15 +08:00
|
|
|
Scopes: []string{"get_user_info"},
|
2021-03-22 20:00:35 +08:00
|
|
|
Endpoint: endpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:23:59 +08:00
|
|
|
func (idp *QqIdProvider) GetToken(code string) (*oauth2.Token, error) {
|
2021-03-23 23:48:18 +08:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("grant_type", "authorization_code")
|
2021-05-16 00:22:15 +08:00
|
|
|
params.Add("client_id", idp.Config.ClientID)
|
|
|
|
params.Add("client_secret", idp.Config.ClientSecret)
|
2021-03-23 23:48:18 +08:00
|
|
|
params.Add("code", code)
|
2021-05-16 00:22:15 +08:00
|
|
|
params.Add("redirect_uri", idp.Config.RedirectURL)
|
2021-03-23 23:48:18 +08:00
|
|
|
|
|
|
|
getAccessTokenUrl := fmt.Sprintf("https://graph.qq.com/oauth2.0/token?%s", params.Encode())
|
2021-05-31 00:13:38 +08:00
|
|
|
resp, err := idp.Client.Get(getAccessTokenUrl)
|
2021-03-23 23:48:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-23 23:23:59 +08:00
|
|
|
|
2021-05-31 00:13:38 +08:00
|
|
|
defer resp.Body.Close()
|
|
|
|
tokenContent, err := ioutil.ReadAll(resp.Body)
|
2021-03-22 20:00:35 +08:00
|
|
|
|
2021-05-31 00:13:38 +08:00
|
|
|
re := regexp.MustCompile("token=(.*?)&")
|
|
|
|
matched := re.FindAllStringSubmatch(string(tokenContent), -1)
|
|
|
|
accessToken := matched[0][1]
|
2021-03-23 23:48:18 +08:00
|
|
|
token := &oauth2.Token{
|
2021-05-31 00:13:38 +08:00
|
|
|
AccessToken: accessToken,
|
2021-03-23 23:48:18 +08:00
|
|
|
TokenType: "Bearer",
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|
2021-03-23 23:48:18 +08:00
|
|
|
return token, nil
|
|
|
|
}
|
2021-03-22 20:00:35 +08:00
|
|
|
|
2021-03-23 23:48:18 +08:00
|
|
|
func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
|
|
|
getOpenIdUrl := fmt.Sprintf("https://graph.qq.com/oauth2.0/me?access_token=%s", token.AccessToken)
|
2021-05-31 00:13:38 +08:00
|
|
|
resp, err := idp.Client.Get(getOpenIdUrl)
|
2021-03-22 20:00:35 +08:00
|
|
|
if err != nil {
|
2021-03-23 23:48:18 +08:00
|
|
|
return nil, err
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|
2021-03-23 23:48:18 +08:00
|
|
|
|
2021-05-31 00:13:38 +08:00
|
|
|
defer resp.Body.Close()
|
|
|
|
openIdBody, err := ioutil.ReadAll(resp.Body)
|
2021-03-22 20:00:35 +08:00
|
|
|
|
2021-05-31 00:13:38 +08:00
|
|
|
re := regexp.MustCompile("\"openid\":\"(.*?)\"}")
|
|
|
|
matched := re.FindAllStringSubmatch(string(openIdBody), -1)
|
|
|
|
openId := matched[0][1]
|
2021-03-22 20:00:35 +08:00
|
|
|
if openId == "" {
|
2021-03-23 23:48:18 +08:00
|
|
|
return nil, errors.New("openId is empty")
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|
|
|
|
|
2021-05-16 00:22:15 +08:00
|
|
|
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)
|
2021-05-31 00:13:38 +08:00
|
|
|
resp, err = idp.Client.Get(getUserInfoUrl)
|
2021-03-22 20:00:35 +08:00
|
|
|
if err != nil {
|
2021-03-23 23:48:18 +08:00
|
|
|
return nil, err
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|
2021-03-23 23:48:18 +08:00
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Ret int `json:"ret"`
|
|
|
|
Nickname string `json:"nickname"`
|
|
|
|
AvatarUrl string `json:"figureurl_qq_1"`
|
|
|
|
}
|
|
|
|
|
2021-05-31 00:13:38 +08:00
|
|
|
defer resp.Body.Close()
|
|
|
|
userInfoContent, err := ioutil.ReadAll(resp.Body)
|
2021-03-23 23:48:18 +08:00
|
|
|
var userResponse response
|
2021-03-24 00:11:20 +08:00
|
|
|
err = json.Unmarshal(userInfoContent, &userResponse)
|
2021-03-23 23:48:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if userResponse.Ret != 0 {
|
|
|
|
return nil, errors.New(fmt.Sprintf("ret expected 0, got %d", userResponse.Ret))
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 00:13:38 +08:00
|
|
|
userInfo := UserInfo{
|
|
|
|
Username: openId,
|
|
|
|
DisplayName: userResponse.Nickname,
|
|
|
|
AvatarUrl: userResponse.AvatarUrl,
|
|
|
|
}
|
|
|
|
return &userInfo, nil
|
2021-03-22 20:00:35 +08:00
|
|
|
}
|