Skip to content

Commit 75d2aff

Browse files
committed
add oauth2 qq support
1 parent f92851e commit 75d2aff

File tree

5 files changed

+100
-6
lines changed

5 files changed

+100
-6
lines changed

models/oauth2.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
OT_GITHUB = iota + 1
1414
OT_GOOGLE
1515
OT_TWITTER
16+
OT_QQ
1617
)
1718

1819
var (

routers/user/social.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type BasicUserInfo struct {
2828
type SocialConnector interface {
2929
Type() int
3030
SetRedirectUrl(string)
31-
UserInfo(*oauth.Token) (*BasicUserInfo, error)
31+
UserInfo(*oauth.Token, *url.URL) (*BasicUserInfo, error)
3232

3333
AuthCodeURL(string) string
3434
Exchange(string) (*oauth.Token, error)
@@ -63,13 +63,13 @@ func SocialSignIn(params martini.Params, ctx *middleware.Context) {
6363
code := ctx.Query("code")
6464
if code == "" {
6565
// redirect to social login page
66-
connect.SetRedirectUrl(strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.RequestURI())
66+
connect.SetRedirectUrl(strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.Host + ctx.Req.URL.Path)
6767
ctx.Redirect(connect.AuthCodeURL(next))
6868
return
6969
}
7070

7171
// handle call back
72-
tk, err := connect.Exchange(code) // transport.Exchange(code)
72+
tk, err := connect.Exchange(code) // exchange for token
7373
if err != nil {
7474
log.Error("oauth2 handle callback error: %v", err)
7575
ctx.Handle(500, "exchange code error", nil)
@@ -78,7 +78,7 @@ func SocialSignIn(params martini.Params, ctx *middleware.Context) {
7878
next = extractPath(ctx.Query("state"))
7979
log.Trace("success get token")
8080

81-
ui, err := connect.UserInfo(tk)
81+
ui, err := connect.UserInfo(tk, ctx.Req.URL)
8282
if err != nil {
8383
ctx.Handle(500, fmt.Sprintf("get infomation from %s error: %v", name, err), nil)
8484
log.Error("social connect error: %s", err)

routers/user/social_github.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package user
26

37
import (
48
"encoding/json"
59
"net/http"
10+
"net/url"
611
"strconv"
712
"strings"
813

@@ -42,7 +47,7 @@ func (s *SocialGithub) SetRedirectUrl(url string) {
4247
s.Transport.Config.RedirectURL = url
4348
}
4449

45-
func (s *SocialGithub) UserInfo(token *oauth.Token) (*BasicUserInfo, error) {
50+
func (s *SocialGithub) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) {
4651
transport := &oauth.Transport{
4752
Token: token,
4853
}

routers/user/social_google.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package user
26

37
import (
48
"encoding/json"
59
"net/http"
10+
"net/url"
611
"github.com/gogits/gogs/models"
712

813
"code.google.com/p/goauth2/oauth"
@@ -40,7 +45,7 @@ func (s *SocialGoogle) SetRedirectUrl(url string) {
4045
s.Transport.Config.RedirectURL = url
4146
}
4247

43-
func (s *SocialGoogle) UserInfo(token *oauth.Token) (*BasicUserInfo, error) {
48+
func (s *SocialGoogle) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) {
4449
transport := &oauth.Transport{Token: token}
4550
var data struct {
4651
Id string `json:"id"`

routers/user/social_qq.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// 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
6+
package user
7+
8+
import (
9+
"encoding/json"
10+
"net/http"
11+
"net/url"
12+
"github.com/gogits/gogs/models"
13+
14+
"code.google.com/p/goauth2/oauth"
15+
)
16+
17+
type SocialQQ struct {
18+
Token *oauth.Token
19+
*oauth.Transport
20+
reqUrl string
21+
}
22+
23+
func (s *SocialQQ) Type() int {
24+
return models.OT_QQ
25+
}
26+
27+
func init() {
28+
qq := &SocialQQ{}
29+
name := "qq"
30+
config := &oauth.Config{
31+
ClientId: "801497180", //base.OauthService.GitHub.ClientId, // FIXME: panic when set
32+
ClientSecret: "16cd53b8ad2e16a36fc2c8f87d9388f2", //base.OauthService.GitHub.ClientSecret,
33+
Scope: "all",
34+
AuthURL: "https://open.t.qq.com/cgi-bin/oauth2/authorize",
35+
TokenURL: "https://open.t.qq.com/cgi-bin/oauth2/access_token",
36+
}
37+
qq.reqUrl = "https://open.t.qq.com/api/user/info"
38+
qq.Transport = &oauth.Transport{
39+
Config: config,
40+
Transport: http.DefaultTransport,
41+
}
42+
SocialMap[name] = qq
43+
}
44+
45+
func (s *SocialQQ) SetRedirectUrl(url string) {
46+
s.Transport.Config.RedirectURL = url
47+
}
48+
49+
func (s *SocialQQ) UserInfo(token *oauth.Token, URL *url.URL) (*BasicUserInfo, error) {
50+
var data struct {
51+
Data struct {
52+
Id string `json:"openid"`
53+
Name string `json:"name"`
54+
Email string `json:"email"`
55+
} `json:"data"`
56+
}
57+
var err error
58+
// https://open.t.qq.com/api/user/info?
59+
//oauth_consumer_key=APP_KEY&
60+
//access_token=ACCESSTOKEN&openid=openid
61+
//clientip=CLIENTIP&oauth_version=2.a
62+
//scope=all
63+
var urls = url.Values{
64+
"oauth_consumer_key": {s.Transport.Config.ClientId},
65+
"access_token": {token.AccessToken},
66+
"openid": URL.Query()["openid"],
67+
"oauth_version": {"2.a"},
68+
"scope": {"all"},
69+
}
70+
r, err := http.Get(s.reqUrl + "?" + urls.Encode())
71+
if err != nil {
72+
return nil, err
73+
}
74+
defer r.Body.Close()
75+
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
76+
return nil, err
77+
}
78+
return &BasicUserInfo{
79+
Identity: data.Data.Id,
80+
Name: data.Data.Name,
81+
Email: data.Data.Email,
82+
}, nil
83+
}

0 commit comments

Comments
 (0)