1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-16 07:04:25 +00:00
gitea/modules/social/social.go

334 lines
8.5 KiB
Go
Raw Normal View History

2014-04-13 22:12:07 +00:00
// Copyright 2014 Google Inc. All Rights Reserved.
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package social
import (
"encoding/json"
2014-11-29 02:20:13 +00:00
"io/ioutil"
2014-04-13 22:12:07 +00:00
"net/http"
"net/url"
"strconv"
2014-11-29 02:20:13 +00:00
"github.com/macaron-contrib/oauth2"
2014-04-13 22:12:07 +00:00
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/log"
2014-05-26 00:11:25 +00:00
"github.com/gogits/gogs/modules/setting"
2014-04-13 22:12:07 +00:00
)
type BasicUserInfo struct {
Identity string
Name string
Email string
}
type SocialConnector interface {
Type() int
2014-11-29 02:20:13 +00:00
UserInfo(*oauth2.Token, *url.URL) (*BasicUserInfo, error)
2014-04-13 22:12:07 +00:00
}
var (
2014-11-29 02:20:13 +00:00
SocialMap = make(map[string]SocialConnector)
2014-04-13 22:12:07 +00:00
)
func NewOauthService() {
2014-12-31 10:37:29 +00:00
if !setting.Cfg.Section("oauth").Key("ENABLED").MustBool() {
2014-04-13 22:12:07 +00:00
return
}
2014-11-29 02:38:35 +00:00
oauth2.AppSubUrl = setting.AppSubUrl
2014-05-26 00:11:25 +00:00
setting.OauthService = &setting.Oauther{}
setting.OauthService.OauthInfos = make(map[string]*setting.OauthInfo)
2014-04-13 22:12:07 +00:00
2014-11-29 02:20:13 +00:00
socialConfigs := make(map[string]*oauth2.Options)
2014-04-14 01:00:12 +00:00
allOauthes := []string{"github", "google", "qq", "twitter", "weibo"}
2014-04-13 22:12:07 +00:00
// Load all OAuth config data.
for _, name := range allOauthes {
2014-12-31 10:37:29 +00:00
sec := setting.Cfg.Section("oauth." + name)
if !sec.Key("ENABLED").MustBool() {
2014-11-29 02:20:13 +00:00
continue
}
2014-05-26 00:11:25 +00:00
setting.OauthService.OauthInfos[name] = &setting.OauthInfo{
2014-11-29 02:20:13 +00:00
Options: oauth2.Options{
2014-12-31 10:37:29 +00:00
ClientID: sec.Key("CLIENT_ID").String(),
ClientSecret: sec.Key("CLIENT_SECRET").String(),
Scopes: sec.Key("SCOPES").Strings(" "),
2014-11-29 02:20:13 +00:00
PathLogin: "/user/login/oauth2/" + name,
PathCallback: setting.AppSubUrl + "/user/login/" + name,
RedirectURL: setting.AppUrl + "user/login/" + name,
},
2014-12-31 10:37:29 +00:00
AuthUrl: sec.Key("AUTH_URL").String(),
TokenUrl: sec.Key("TOKEN_URL").String(),
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
socialConfigs[name] = &oauth2.Options{
ClientID: setting.OauthService.OauthInfos[name].ClientID,
2014-05-26 00:11:25 +00:00
ClientSecret: setting.OauthService.OauthInfos[name].ClientSecret,
2014-11-29 02:20:13 +00:00
Scopes: setting.OauthService.OauthInfos[name].Scopes,
2014-04-13 22:12:07 +00:00
}
}
enabledOauths := make([]string, 0, 10)
// GitHub.
2014-12-31 10:37:29 +00:00
if setting.Cfg.Section("oauth.github").Key("ENABLED").MustBool() {
2014-05-26 00:11:25 +00:00
setting.OauthService.GitHub = true
2014-04-13 22:12:07 +00:00
newGitHubOauth(socialConfigs["github"])
enabledOauths = append(enabledOauths, "GitHub")
}
// Google.
2014-12-31 10:37:29 +00:00
if setting.Cfg.Section("oauth.google").Key("ENABLED").MustBool() {
2014-05-26 00:11:25 +00:00
setting.OauthService.Google = true
2014-04-13 22:12:07 +00:00
newGoogleOauth(socialConfigs["google"])
enabledOauths = append(enabledOauths, "Google")
}
// QQ.
2014-12-31 10:37:29 +00:00
if setting.Cfg.Section("oauth.qq").Key("ENABLED").MustBool() {
2014-05-26 00:11:25 +00:00
setting.OauthService.Tencent = true
2014-04-13 22:12:07 +00:00
newTencentOauth(socialConfigs["qq"])
enabledOauths = append(enabledOauths, "QQ")
}
// Twitter.
2014-12-31 10:37:29 +00:00
// if setting.Cfg.Section("oauth.twitter").Key( "ENABLED").MustBool() {
2014-11-29 02:20:13 +00:00
// setting.OauthService.Twitter = true
// newTwitterOauth(socialConfigs["twitter"])
// enabledOauths = append(enabledOauths, "Twitter")
// }
2014-04-13 22:12:07 +00:00
2014-04-14 01:00:12 +00:00
// Weibo.
2014-12-31 10:37:29 +00:00
if setting.Cfg.Section("oauth.weibo").Key("ENABLED").MustBool() {
2014-05-26 00:11:25 +00:00
setting.OauthService.Weibo = true
2014-04-14 01:00:12 +00:00
newWeiboOauth(socialConfigs["weibo"])
enabledOauths = append(enabledOauths, "Weibo")
}
2014-04-13 22:12:07 +00:00
log.Info("Oauth Service Enabled %s", enabledOauths)
}
// ________.__ __ ___ ___ ___.
// / _____/|__|/ |_ / | \ __ _\_ |__
// / \ ___| \ __\/ ~ \ | \ __ \
// \ \_\ \ || | \ Y / | / \_\ \
// \______ /__||__| \___|_ /|____/|___ /
// \/ \/ \/
type SocialGithub struct {
2014-11-29 02:20:13 +00:00
opts *oauth2.Options
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
func newGitHubOauth(opts *oauth2.Options) {
SocialMap["github"] = &SocialGithub{opts}
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
func (s *SocialGithub) Type() int {
return int(models.GITHUB)
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
func (s *SocialGithub) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
transport := s.opts.NewTransportFromToken(token)
2014-04-13 22:12:07 +00:00
var data struct {
Id int `json:"id"`
Name string `json:"login"`
Email string `json:"email"`
}
2014-11-29 02:20:13 +00:00
r, err := transport.Client().Get("https://api.github.com/user")
2014-04-13 22:12:07 +00:00
if err != nil {
return nil, err
}
defer r.Body.Close()
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}
return &BasicUserInfo{
Identity: strconv.Itoa(data.Id),
Name: data.Name,
Email: data.Email,
}, nil
}
// ________ .__
// / _____/ ____ ____ ____ | | ____
// / \ ___ / _ \ / _ \ / ___\| | _/ __ \
// \ \_\ ( <_> | <_> ) /_/ > |_\ ___/
// \______ /\____/ \____/\___ /|____/\___ >
// \/ /_____/ \/
type SocialGoogle struct {
2014-11-29 02:20:13 +00:00
opts *oauth2.Options
2014-04-13 22:12:07 +00:00
}
func (s *SocialGoogle) Type() int {
2014-06-21 04:51:41 +00:00
return int(models.GOOGLE)
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
func newGoogleOauth(opts *oauth2.Options) {
SocialMap["google"] = &SocialGoogle{opts}
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
func (s *SocialGoogle) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
transport := s.opts.NewTransportFromToken(token)
2014-04-13 22:12:07 +00:00
var data struct {
Id string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
2014-11-29 02:20:13 +00:00
r, err := transport.Client().Get("https://www.googleapis.com/userinfo/v2/me")
2014-04-13 22:12:07 +00:00
if err != nil {
return nil, err
}
defer r.Body.Close()
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}
return &BasicUserInfo{
Identity: data.Id,
Name: data.Name,
Email: data.Email,
}, nil
}
// ________ ________
// \_____ \ \_____ \
// / / \ \ / / \ \
// / \_/. \/ \_/. \
// \_____\ \_/\_____\ \_/
// \__> \__>
type SocialTencent struct {
2014-11-29 02:20:13 +00:00
opts *oauth2.Options
}
func newTencentOauth(opts *oauth2.Options) {
SocialMap["qq"] = &SocialTencent{opts}
2014-04-13 22:12:07 +00:00
}
func (s *SocialTencent) Type() int {
2014-06-21 04:51:41 +00:00
return int(models.QQ)
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
func (s *SocialTencent) UserInfo(token *oauth2.Token, URL *url.URL) (*BasicUserInfo, error) {
r, err := http.Get("https://graph.z.qq.com/moc2/me?access_token=" + url.QueryEscape(token.AccessToken))
if err != nil {
return nil, err
2014-04-13 22:12:07 +00:00
}
2014-11-29 02:20:13 +00:00
defer r.Body.Close()
2014-04-13 22:12:07 +00:00
2014-11-29 02:20:13 +00:00
body, err := ioutil.ReadAll(r.Body)
2014-04-13 22:12:07 +00:00
if err != nil {
return nil, err
}
2014-11-29 02:20:13 +00:00
vals, err := url.ParseQuery(string(body))
if err != nil {
2014-04-13 22:12:07 +00:00
return nil, err
}
2014-11-29 02:20:13 +00:00
2014-04-13 22:12:07 +00:00
return &BasicUserInfo{
2014-11-29 02:20:13 +00:00
Identity: vals.Get("openid"),
2014-04-13 22:12:07 +00:00
}, nil
}
// ___________ .__ __ __
// \__ ___/_ _ _|__|/ |__/ |_ ___________
// | | \ \/ \/ / \ __\ __\/ __ \_ __ \
// | | \ /| || | | | \ ___/| | \/
// |____| \/\_/ |__||__| |__| \___ >__|
// \/
2014-11-29 02:20:13 +00:00
// type SocialTwitter struct {
// Token *oauth2.Token
// *oauth2.Transport
// }
// func (s *SocialTwitter) Type() int {
// return int(models.TWITTER)
// }
// func newTwitterOauth(config *oauth2.Config) {
// SocialMap["twitter"] = &SocialTwitter{
// Transport: &oauth.Transport{
// Config: config,
// Transport: http.DefaultTransport,
// },
// }
// }
// func (s *SocialTwitter) SetRedirectUrl(url string) {
// s.Transport.Config.RedirectURL = url
// }
// //https://github.com/mrjones/oauth
// func (s *SocialTwitter) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
// // transport := &oauth.Transport{Token: token}
// // var data struct {
// // Id string `json:"id"`
// // Name string `json:"name"`
// // Email string `json:"email"`
// // }
// // var err error
// // reqUrl := "https://www.googleapis.com/oauth2/v1/userinfo"
// // r, err := transport.Client().Get(reqUrl)
// // if err != nil {
// // return nil, err
// // }
// // defer r.Body.Close()
// // if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
// // return nil, err
// // }
// // return &BasicUserInfo{
// // Identity: data.Id,
// // Name: data.Name,
// // Email: data.Email,
// // }, nil
// return nil, nil
// }
2014-04-14 01:00:12 +00:00
// __ __ ._____.
// / \ / \ ____ |__\_ |__ ____
// \ \/\/ // __ \| || __ \ / _ \
// \ /\ ___/| || \_\ ( <_> )
// \__/\ / \___ >__||___ /\____/
// \/ \/ \/
type SocialWeibo struct {
2014-11-29 02:20:13 +00:00
opts *oauth2.Options
2014-04-14 01:00:12 +00:00
}
2014-11-29 02:20:13 +00:00
func newWeiboOauth(opts *oauth2.Options) {
SocialMap["weibo"] = &SocialWeibo{opts}
2014-04-14 01:00:12 +00:00
}
2014-11-29 02:20:13 +00:00
func (s *SocialWeibo) Type() int {
return int(models.WEIBO)
2014-04-14 01:00:12 +00:00
}
2014-11-29 02:20:13 +00:00
func (s *SocialWeibo) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
transport := s.opts.NewTransportFromToken(token)
2014-04-14 01:00:12 +00:00
var data struct {
Name string `json:"name"`
}
2014-04-14 09:07:19 +00:00
var urls = url.Values{
"access_token": {token.AccessToken},
2014-11-29 02:20:13 +00:00
"uid": {token.Extra("uid")},
2014-04-14 09:07:19 +00:00
}
2014-04-14 01:00:12 +00:00
reqUrl := "https://api.weibo.com/2/users/show.json"
2014-04-14 09:07:19 +00:00
r, err := transport.Client().Get(reqUrl + "?" + urls.Encode())
2014-04-14 01:00:12 +00:00
if err != nil {
return nil, err
}
defer r.Body.Close()
2014-11-29 02:20:13 +00:00
2014-04-14 01:00:12 +00:00
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}
return &BasicUserInfo{
2014-11-29 02:20:13 +00:00
Identity: token.Extra("uid"),
2014-04-14 01:00:12 +00:00
Name: data.Name,
}, nil
}