casdoor/idp/qq.go

119 lines
3.1 KiB
Go
Raw Normal View History

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 (
2021-03-23 23:23:59 +08:00
"context"
2021-03-22 20:00:35 +08:00
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"golang.org/x/oauth2"
)
type QqIdProvider struct {
2021-03-23 23:23:59 +08:00
Client *http.Client
Config *oauth2.Config
2021-03-22 20:00:35 +08:00
ClientId string
2021-03-23 23:23:59 +08:00
ClientSecret string
RedirectUrl string
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 {
idp := &QqIdProvider{
ClientId: clientId,
ClientSecret: clientSecret,
RedirectUrl: redirectUrl,
}
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{
Scopes: []string{"profile", "email"},
Endpoint: endpoint,
}
return config
}
2021-03-23 23:23:59 +08:00
func (idp *QqIdProvider) GetToken(code string) (*oauth2.Token, error) {
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, idp.Client)
return idp.Config.Exchange(ctx, code)
}
func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
userInfo := &UserInfo{}
2021-03-22 20:00:35 +08:00
type userInfoFromQq struct {
Ret int `json:"ret"`
Nickname string `json:"nickname"`
AvatarUrl string `json:"figureurl_qq_1"`
}
getOpenIdUrl := fmt.Sprintf("https://graph.qq.com/oauth2.0/me?access_token=%s", token)
2021-03-23 23:23:59 +08:00
openIdResponse, err := idp.Client.Get(getOpenIdUrl)
2021-03-22 20:00:35 +08:00
if err != nil {
panic(err)
}
defer openIdResponse.Body.Close()
openIdContent, err := ioutil.ReadAll(openIdResponse.Body)
openIdReg := regexp.MustCompile("\"openid\":\"(.*?)\"}")
openIdRegRes := openIdReg.FindAllStringSubmatch(string(openIdContent), -1)
openId := openIdRegRes[0][1]
if openId == "" {
2021-03-23 23:23:59 +08:00
return userInfo, errors.New("openId is empty")
2021-03-22 20:00:35 +08:00
}
getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token, idp.ClientId, openId)
2021-03-23 23:23:59 +08:00
getUserInfoResponse, err := idp.Client.Get(getUserInfoUrl)
2021-03-22 20:00:35 +08:00
if err != nil {
panic(err)
}
defer getUserInfoResponse.Body.Close()
userInfoContent, err := ioutil.ReadAll(getUserInfoResponse.Body)
2021-03-23 23:23:59 +08:00
var info userInfoFromQq
err = json.Unmarshal(userInfoContent, &info)
if err != nil || info.Ret != 0 {
return userInfo, err
2021-03-22 20:00:35 +08:00
}
2021-03-23 23:23:59 +08:00
userInfo.Username = info.Nickname
userInfo.AvatarUrl = userInfo.AvatarUrl
2021-03-22 20:00:35 +08:00
2021-03-23 23:23:59 +08:00
return userInfo, nil
2021-03-22 20:00:35 +08:00
}