|
| 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