Git with a cup of tea, painless self-hosted git service Mirror for internal git.with.parts use https://git.with.parts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/modules/social/social.go

333 lines
8.5 KiB

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