forgejo-federation/modules/social/social.go

333 lines
8.5 KiB
Go
Raw Normal View History

2014-04-14 03:42:07 +05:30
// 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 07:50:13 +05:30
"io/ioutil"
2014-04-14 03:42:07 +05:30
"net/http"
"net/url"
"strconv"
2014-11-29 07:50:13 +05:30
"github.com/macaron-contrib/oauth2"
2014-04-14 03:42:07 +05:30
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/log"
2014-05-26 05:41:25 +05:30
"github.com/gogits/gogs/modules/setting"
2014-04-14 03:42:07 +05:30
)
type BasicUserInfo struct {
Identity string
Name string
Email string
}
type SocialConnector interface {
Type() int
2014-11-29 07:50:13 +05:30
UserInfo(*oauth2.Token, *url.URL) (*BasicUserInfo, error)
2014-04-14 03:42:07 +05:30
}
var (
2014-11-29 07:50:13 +05:30
SocialMap = make(map[string]SocialConnector)
2014-04-14 03:42:07 +05:30
)
func NewOauthService() {
2014-05-26 05:41:25 +05:30
if !setting.Cfg.MustBool("oauth", "ENABLED") {
2014-04-14 03:42:07 +05:30
return
}
2014-11-29 08:08:35 +05:30
oauth2.AppSubUrl = setting.AppSubUrl
2014-05-26 05:41:25 +05:30
setting.OauthService = &setting.Oauther{}
setting.OauthService.OauthInfos = make(map[string]*setting.OauthInfo)
2014-04-14 03:42:07 +05:30
2014-11-29 07:50:13 +05:30
socialConfigs := make(map[string]*oauth2.Options)
2014-04-14 06:30:12 +05:30
allOauthes := []string{"github", "google", "qq", "twitter", "weibo"}
2014-04-14 03:42:07 +05:30
// Load all OAuth config data.
for _, name := range allOauthes {
2014-11-29 07:50:13 +05:30
if !setting.Cfg.MustBool("oauth."+name, "ENABLED") {
continue
}
2014-05-26 05:41:25 +05:30
setting.OauthService.OauthInfos[name] = &setting.OauthInfo{
2014-11-29 07:50:13 +05:30
Options: oauth2.Options{
ClientID: setting.Cfg.MustValue("oauth."+name, "CLIENT_ID"),
ClientSecret: setting.Cfg.MustValue("oauth."+name, "CLIENT_SECRET"),
Scopes: setting.Cfg.MustValueArray("oauth."+name, "SCOPES", " "),
PathLogin: "/user/login/oauth2/" + name,
PathCallback: setting.AppSubUrl + "/user/login/" + name,
RedirectURL: setting.AppUrl + "user/login/" + name,
},
AuthUrl: setting.Cfg.MustValue("oauth."+name, "AUTH_URL"),
TokenUrl: setting.Cfg.MustValue("oauth."+name, "TOKEN_URL"),
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
socialConfigs[name] = &oauth2.Options{
ClientID: setting.OauthService.OauthInfos[name].ClientID,
2014-05-26 05:41:25 +05:30
ClientSecret: setting.OauthService.OauthInfos[name].ClientSecret,
2014-11-29 07:50:13 +05:30
Scopes: setting.OauthService.OauthInfos[name].Scopes,
2014-04-14 03:42:07 +05:30
}
}
enabledOauths := make([]string, 0, 10)
// GitHub.
2014-05-26 05:41:25 +05:30
if setting.Cfg.MustBool("oauth.github", "ENABLED") {
setting.OauthService.GitHub = true
2014-04-14 03:42:07 +05:30
newGitHubOauth(socialConfigs["github"])
enabledOauths = append(enabledOauths, "GitHub")
}
// Google.
2014-05-26 05:41:25 +05:30
if setting.Cfg.MustBool("oauth.google", "ENABLED") {
setting.OauthService.Google = true
2014-04-14 03:42:07 +05:30
newGoogleOauth(socialConfigs["google"])
enabledOauths = append(enabledOauths, "Google")
}
// QQ.
2014-05-26 05:41:25 +05:30
if setting.Cfg.MustBool("oauth.qq", "ENABLED") {
setting.OauthService.Tencent = true
2014-04-14 03:42:07 +05:30
newTencentOauth(socialConfigs["qq"])
enabledOauths = append(enabledOauths, "QQ")
}
// Twitter.
2014-11-29 07:50:13 +05:30
// if setting.Cfg.MustBool("oauth.twitter", "ENABLED") {
// setting.OauthService.Twitter = true
// newTwitterOauth(socialConfigs["twitter"])
// enabledOauths = append(enabledOauths, "Twitter")
// }
2014-04-14 03:42:07 +05:30
2014-04-14 06:30:12 +05:30
// Weibo.
2014-05-26 05:41:25 +05:30
if setting.Cfg.MustBool("oauth.weibo", "ENABLED") {
setting.OauthService.Weibo = true
2014-04-14 06:30:12 +05:30
newWeiboOauth(socialConfigs["weibo"])
enabledOauths = append(enabledOauths, "Weibo")
}
2014-04-14 03:42:07 +05:30
log.Info("Oauth Service Enabled %s", enabledOauths)
}
// ________.__ __ ___ ___ ___.
// / _____/|__|/ |_ / | \ __ _\_ |__
// / \ ___| \ __\/ ~ \ | \ __ \
// \ \_\ \ || | \ Y / | / \_\ \
// \______ /__||__| \___|_ /|____/|___ /
// \/ \/ \/
type SocialGithub struct {
2014-11-29 07:50:13 +05:30
opts *oauth2.Options
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
func newGitHubOauth(opts *oauth2.Options) {
SocialMap["github"] = &SocialGithub{opts}
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
func (s *SocialGithub) Type() int {
return int(models.GITHUB)
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
func (s *SocialGithub) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
transport := s.opts.NewTransportFromToken(token)
2014-04-14 03:42:07 +05:30
var data struct {
Id int `json:"id"`
Name string `json:"login"`
Email string `json:"email"`
}
2014-11-29 07:50:13 +05:30
r, err := transport.Client().Get("https://api.github.com/user")
2014-04-14 03:42:07 +05:30
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 07:50:13 +05:30
opts *oauth2.Options
2014-04-14 03:42:07 +05:30
}
func (s *SocialGoogle) Type() int {
2014-06-21 10:21:41 +05:30
return int(models.GOOGLE)
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
func newGoogleOauth(opts *oauth2.Options) {
SocialMap["google"] = &SocialGoogle{opts}
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
func (s *SocialGoogle) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
transport := s.opts.NewTransportFromToken(token)
2014-04-14 03:42:07 +05:30
var data struct {
Id string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
2014-11-29 07:50:13 +05:30
r, err := transport.Client().Get("https://www.googleapis.com/userinfo/v2/me")
2014-04-14 03:42:07 +05:30
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 07:50:13 +05:30
opts *oauth2.Options
}
func newTencentOauth(opts *oauth2.Options) {
SocialMap["qq"] = &SocialTencent{opts}
2014-04-14 03:42:07 +05:30
}
func (s *SocialTencent) Type() int {
2014-06-21 10:21:41 +05:30
return int(models.QQ)
2014-04-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
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-14 03:42:07 +05:30
}
2014-11-29 07:50:13 +05:30
defer r.Body.Close()
2014-04-14 03:42:07 +05:30
2014-11-29 07:50:13 +05:30
body, err := ioutil.ReadAll(r.Body)
2014-04-14 03:42:07 +05:30
if err != nil {
return nil, err
}
2014-11-29 07:50:13 +05:30
vals, err := url.ParseQuery(string(body))
if err != nil {
2014-04-14 03:42:07 +05:30
return nil, err
}
2014-11-29 07:50:13 +05:30
2014-04-14 03:42:07 +05:30
return &BasicUserInfo{
2014-11-29 07:50:13 +05:30
Identity: vals.Get("openid"),
2014-04-14 03:42:07 +05:30
}, nil
}
// ___________ .__ __ __
// \__ ___/_ _ _|__|/ |__/ |_ ___________
// | | \ \/ \/ / \ __\ __\/ __ \_ __ \
// | | \ /| || | | | \ ___/| | \/
// |____| \/\_/ |__||__| |__| \___ >__|
// \/
2014-11-29 07:50:13 +05:30
// 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 06:30:12 +05:30
// __ __ ._____.
// / \ / \ ____ |__\_ |__ ____
// \ \/\/ // __ \| || __ \ / _ \
// \ /\ ___/| || \_\ ( <_> )
// \__/\ / \___ >__||___ /\____/
// \/ \/ \/
type SocialWeibo struct {
2014-11-29 07:50:13 +05:30
opts *oauth2.Options
2014-04-14 06:30:12 +05:30
}
2014-11-29 07:50:13 +05:30
func newWeiboOauth(opts *oauth2.Options) {
SocialMap["weibo"] = &SocialWeibo{opts}
2014-04-14 06:30:12 +05:30
}
2014-11-29 07:50:13 +05:30
func (s *SocialWeibo) Type() int {
return int(models.WEIBO)
2014-04-14 06:30:12 +05:30
}
2014-11-29 07:50:13 +05:30
func (s *SocialWeibo) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
transport := s.opts.NewTransportFromToken(token)
2014-04-14 06:30:12 +05:30
var data struct {
Name string `json:"name"`
}
2014-04-14 14:37:19 +05:30
var urls = url.Values{
"access_token": {token.AccessToken},
2014-11-29 07:50:13 +05:30
"uid": {token.Extra("uid")},
2014-04-14 14:37:19 +05:30
}
2014-04-14 06:30:12 +05:30
reqUrl := "https://api.weibo.com/2/users/show.json"
2014-04-14 14:37:19 +05:30
r, err := transport.Client().Get(reqUrl + "?" + urls.Encode())
2014-04-14 06:30:12 +05:30
if err != nil {
return nil, err
}
defer r.Body.Close()
2014-11-29 07:50:13 +05:30
2014-04-14 06:30:12 +05:30
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}
return &BasicUserInfo{
2014-11-29 07:50:13 +05:30
Identity: token.Extra("uid"),
2014-04-14 06:30:12 +05:30
Name: data.Name,
}, nil
}