commit
d60f7b85e5
@ -0,0 +1,73 @@ |
||||
// 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 user |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"net/http" |
||||
"net/url" |
||||
"strconv" |
||||
"strings" |
||||
|
||||
"code.google.com/p/goauth2/oauth" |
||||
"github.com/gogits/gogs/models" |
||||
"github.com/gogits/gogs/modules/base" |
||||
) |
||||
|
||||
type SocialGithub struct { |
||||
Token *oauth.Token |
||||
*oauth.Transport |
||||
} |
||||
|
||||
func (s *SocialGithub) Type() int { |
||||
return models.OT_GITHUB |
||||
} |
||||
|
||||
func init() { |
||||
github := &SocialGithub{} |
||||
name := "github" |
||||
config := &oauth.Config{ |
||||
ClientId: "09383403ff2dc16daaa1", //base.OauthService.GitHub.ClientId, // FIXME: panic when set
|
||||
ClientSecret: "0e4aa0c3630df396cdcea01a9d45cacf79925fea", //base.OauthService.GitHub.ClientSecret,
|
||||
RedirectURL: strings.TrimSuffix(base.AppUrl, "/") + "/user/login/" + name, //ctx.Req.URL.RequestURI(),
|
||||
Scope: "https://api.github.com/user", |
||||
AuthURL: "https://github.com/login/oauth/authorize", |
||||
TokenURL: "https://github.com/login/oauth/access_token", |
||||
} |
||||
github.Transport = &oauth.Transport{ |
||||
Config: config, |
||||
Transport: http.DefaultTransport, |
||||
} |
||||
SocialMap[name] = github |
||||
} |
||||
|
||||
func (s *SocialGithub) SetRedirectUrl(url string) { |
||||
s.Transport.Config.RedirectURL = url |
||||
} |
||||
|
||||
func (s *SocialGithub) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) { |
||||
transport := &oauth.Transport{ |
||||
Token: token, |
||||
} |
||||
var data struct { |
||||
Id int `json:"id"` |
||||
Name string `json:"login"` |
||||
Email string `json:"email"` |
||||
} |
||||
var err error |
||||
r, err := transport.Client().Get(s.Transport.Scope) |
||||
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 |
||||
} |
@ -0,0 +1,71 @@ |
||||
// 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 user |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"net/http" |
||||
"net/url" |
||||
"github.com/gogits/gogs/models" |
||||
|
||||
"code.google.com/p/goauth2/oauth" |
||||
) |
||||
|
||||
type SocialGoogle struct { |
||||
Token *oauth.Token |
||||
*oauth.Transport |
||||
} |
||||
|
||||
func (s *SocialGoogle) Type() int { |
||||
return models.OT_GOOGLE |
||||
} |
||||
|
||||
func init() { |
||||
google := &SocialGoogle{} |
||||
name := "google" |
||||
// get client id and secret from
|
||||
// https://console.developers.google.com/project
|
||||
config := &oauth.Config{ |
||||
ClientId: "849753812404-mpd7ilvlb8c7213qn6bre6p6djjskti9.apps.googleusercontent.com", //base.OauthService.GitHub.ClientId, // FIXME: panic when set
|
||||
ClientSecret: "VukKc4MwaJUSmiyv3D7ANVCa", //base.OauthService.GitHub.ClientSecret,
|
||||
Scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile", |
||||
AuthURL: "https://accounts.google.com/o/oauth2/auth", |
||||
TokenURL: "https://accounts.google.com/o/oauth2/token", |
||||
} |
||||
google.Transport = &oauth.Transport{ |
||||
Config: config, |
||||
Transport: http.DefaultTransport, |
||||
} |
||||
SocialMap[name] = google |
||||
} |
||||
|
||||
func (s *SocialGoogle) SetRedirectUrl(url string) { |
||||
s.Transport.Config.RedirectURL = url |
||||
} |
||||
|
||||
func (s *SocialGoogle) UserInfo(token *oauth.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 |
||||
} |
@ -0,0 +1,83 @@ |
||||
// 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.
|
||||
|
||||
// api reference: http://wiki.open.t.qq.com/index.php/OAuth2.0%E9%89%B4%E6%9D%83/Authorization_code%E6%8E%88%E6%9D%83%E6%A1%88%E4%BE%8B
|
||||
package user |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"net/http" |
||||
"net/url" |
||||
"github.com/gogits/gogs/models" |
||||
|
||||
"code.google.com/p/goauth2/oauth" |
||||
) |
||||
|
||||
type SocialQQ struct { |
||||
Token *oauth.Token |
||||
*oauth.Transport |
||||
reqUrl string |
||||
} |
||||
|
||||
func (s *SocialQQ) Type() int { |
||||
return models.OT_QQ |
||||
} |
||||
|
||||
func init() { |
||||
qq := &SocialQQ{} |
||||
name := "qq" |
||||
config := &oauth.Config{ |
||||
ClientId: "801497180", //base.OauthService.GitHub.ClientId, // FIXME: panic when set
|
||||
ClientSecret: "16cd53b8ad2e16a36fc2c8f87d9388f2", //base.OauthService.GitHub.ClientSecret,
|
||||
Scope: "all", |
||||
AuthURL: "https://open.t.qq.com/cgi-bin/oauth2/authorize", |
||||
TokenURL: "https://open.t.qq.com/cgi-bin/oauth2/access_token", |
||||
} |
||||
qq.reqUrl = "https://open.t.qq.com/api/user/info" |
||||
qq.Transport = &oauth.Transport{ |
||||
Config: config, |
||||
Transport: http.DefaultTransport, |
||||
} |
||||
SocialMap[name] = qq |
||||
} |
||||
|
||||
func (s *SocialQQ) SetRedirectUrl(url string) { |
||||
s.Transport.Config.RedirectURL = url |
||||
} |
||||
|
||||
func (s *SocialQQ) UserInfo(token *oauth.Token, URL *url.URL) (*BasicUserInfo, error) { |
||||
var data struct { |
||||
Data struct { |
||||
Id string `json:"openid"` |
||||
Name string `json:"name"` |
||||
Email string `json:"email"` |
||||
} `json:"data"` |
||||
} |
||||
var err error |
||||
// https://open.t.qq.com/api/user/info?
|
||||
//oauth_consumer_key=APP_KEY&
|
||||
//access_token=ACCESSTOKEN&openid=openid
|
||||
//clientip=CLIENTIP&oauth_version=2.a
|
||||
//scope=all
|
||||
var urls = url.Values{ |
||||
"oauth_consumer_key": {s.Transport.Config.ClientId}, |
||||
"access_token": {token.AccessToken}, |
||||
"openid": URL.Query()["openid"], |
||||
"oauth_version": {"2.a"}, |
||||
"scope": {"all"}, |
||||
} |
||||
r, err := http.Get(s.reqUrl + "?" + urls.Encode()) |
||||
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.Data.Id, |
||||
Name: data.Data.Name, |
||||
Email: data.Data.Email, |
||||
}, nil |
||||
} |
Loading…
Reference in new issue