2014-04-02 08:09:04 +05:30
|
|
|
// 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.
|
2014-04-07 22:26:40 +05:30
|
|
|
|
2014-04-02 08:09:04 +05:30
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-04-09 21:37:57 +05:30
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-04-07 15:31:25 +05:30
|
|
|
"strconv"
|
2014-04-09 21:37:57 +05:30
|
|
|
"strings"
|
2014-04-02 08:09:04 +05:30
|
|
|
|
2014-04-07 22:26:40 +05:30
|
|
|
"code.google.com/p/goauth2/oauth"
|
|
|
|
|
2014-04-07 15:31:25 +05:30
|
|
|
"github.com/gogits/gogs/models"
|
2014-04-09 21:37:57 +05:30
|
|
|
"github.com/gogits/gogs/modules/base"
|
2014-04-02 08:09:04 +05:30
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-04-07 15:31:25 +05:30
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
2014-04-02 08:09:04 +05:30
|
|
|
)
|
|
|
|
|
2014-04-07 15:31:25 +05:30
|
|
|
type SocialConnector interface {
|
|
|
|
Identity() string
|
|
|
|
Name() string
|
|
|
|
Email() string
|
2014-04-11 22:31:30 +05:30
|
|
|
TokenString() string
|
2014-04-07 15:31:25 +05:30
|
|
|
}
|
2014-04-02 08:09:04 +05:30
|
|
|
|
2014-04-07 15:31:25 +05:30
|
|
|
type SocialGithub struct {
|
|
|
|
data struct {
|
2014-04-02 08:09:04 +05:30
|
|
|
Id int `json:"id"`
|
|
|
|
Name string `json:"login"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
2014-04-11 22:31:30 +05:30
|
|
|
Token *oauth.Token
|
2014-04-07 15:31:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SocialGithub) Identity() string {
|
|
|
|
return strconv.Itoa(s.data.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SocialGithub) Name() string {
|
|
|
|
return s.data.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SocialGithub) Email() string {
|
|
|
|
return s.data.Email
|
|
|
|
}
|
2014-04-02 08:09:04 +05:30
|
|
|
|
2014-04-11 22:31:30 +05:30
|
|
|
func (s *SocialGithub) TokenString() string {
|
|
|
|
data, _ := json.Marshal(s.Token)
|
2014-04-07 15:31:25 +05:30
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Github API refer: https://developer.github.com/v3/users/
|
|
|
|
func (s *SocialGithub) Update() error {
|
2014-04-02 08:09:04 +05:30
|
|
|
scope := "https://api.github.com/user"
|
2014-04-07 15:31:25 +05:30
|
|
|
transport := &oauth.Transport{
|
2014-04-11 22:31:30 +05:30
|
|
|
Token: s.Token,
|
2014-04-07 15:31:25 +05:30
|
|
|
}
|
|
|
|
log.Debug("update github info")
|
2014-04-02 08:09:04 +05:30
|
|
|
r, err := transport.Client().Get(scope)
|
|
|
|
if err != nil {
|
2014-04-07 15:31:25 +05:30
|
|
|
return err
|
2014-04-02 08:09:04 +05:30
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
2014-04-07 15:31:25 +05:30
|
|
|
return json.NewDecoder(r.Body).Decode(&s.data)
|
|
|
|
}
|
2014-04-02 08:09:04 +05:30
|
|
|
|
2014-04-09 21:37:57 +05:30
|
|
|
func extractPath(next string) string {
|
|
|
|
n, err := url.Parse(next)
|
|
|
|
if err != nil {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
return n.Path
|
|
|
|
}
|
|
|
|
|
2014-04-07 15:31:25 +05:30
|
|
|
// github && google && ...
|
2014-04-12 07:12:09 +05:30
|
|
|
func SocialSignIn(ctx *middleware.Context) {
|
|
|
|
//if base.OauthService != nil && base.OauthService.GitHub.Enabled {
|
|
|
|
//}
|
|
|
|
|
2014-04-09 21:37:57 +05:30
|
|
|
var socid int64
|
|
|
|
var ok bool
|
|
|
|
next := extractPath(ctx.Query("next"))
|
|
|
|
log.Debug("social signed check %s", next)
|
|
|
|
if socid, ok = ctx.Session.Get("socialId").(int64); ok && socid != 0 {
|
|
|
|
// already login
|
|
|
|
ctx.Redirect(next)
|
|
|
|
log.Info("login soc id: %v", socid)
|
|
|
|
return
|
|
|
|
}
|
2014-04-11 02:06:50 +05:30
|
|
|
|
2014-04-09 21:37:57 +05:30
|
|
|
config := &oauth.Config{
|
2014-04-11 02:06:50 +05:30
|
|
|
ClientId: base.OauthService.GitHub.ClientId,
|
|
|
|
ClientSecret: base.OauthService.GitHub.ClientSecret,
|
2014-04-09 21:37:57 +05:30
|
|
|
RedirectURL: strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.RequestURI(),
|
|
|
|
Scope: base.OauthService.GitHub.Scopes,
|
|
|
|
AuthURL: "https://github.com/login/oauth/authorize",
|
|
|
|
TokenURL: "https://github.com/login/oauth/access_token",
|
2014-04-02 08:09:04 +05:30
|
|
|
}
|
2014-04-09 21:37:57 +05:30
|
|
|
transport := &oauth.Transport{
|
|
|
|
Config: config,
|
|
|
|
Transport: http.DefaultTransport,
|
|
|
|
}
|
|
|
|
code := ctx.Query("code")
|
|
|
|
if code == "" {
|
|
|
|
// redirect to social login page
|
|
|
|
ctx.Redirect(config.AuthCodeURL(next))
|
2014-04-07 22:26:40 +05:30
|
|
|
return
|
|
|
|
}
|
2014-04-09 21:37:57 +05:30
|
|
|
|
|
|
|
// handle call back
|
|
|
|
tk, err := transport.Exchange(code)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("oauth2 handle callback error: %v", err)
|
|
|
|
return // FIXME, need error page 501
|
|
|
|
}
|
|
|
|
next = extractPath(ctx.Query("state"))
|
|
|
|
log.Debug("success token: %v", tk)
|
|
|
|
|
2014-04-11 22:31:30 +05:30
|
|
|
gh := &SocialGithub{Token: tk}
|
2014-04-07 15:31:25 +05:30
|
|
|
if err = gh.Update(); err != nil {
|
2014-04-09 21:37:57 +05:30
|
|
|
// FIXME: handle error page 501
|
2014-04-07 15:31:25 +05:30
|
|
|
log.Error("connect with github error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var soc SocialConnector = gh
|
|
|
|
log.Info("login: %s", soc.Name())
|
2014-04-08 21:56:12 +05:30
|
|
|
oa, err := models.GetOauth2(soc.Identity())
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
ctx.Session.Set("userId", oa.User.Id)
|
|
|
|
ctx.Session.Set("userName", oa.User.Name)
|
|
|
|
case models.ErrOauth2RecordNotExists:
|
|
|
|
oa = &models.Oauth2{}
|
2014-04-11 22:31:30 +05:30
|
|
|
oa.Uid = -1
|
|
|
|
oa.Type = models.OT_GITHUB
|
|
|
|
oa.Token = soc.TokenString()
|
2014-04-07 15:31:25 +05:30
|
|
|
oa.Identity = soc.Identity()
|
2014-04-09 21:37:57 +05:30
|
|
|
log.Debug("oa: %v", oa)
|
2014-04-07 15:31:25 +05:30
|
|
|
if err = models.AddOauth2(oa); err != nil {
|
2014-04-09 21:37:57 +05:30
|
|
|
log.Error("add oauth2 %v", err) // 501
|
2014-04-07 15:31:25 +05:30
|
|
|
return
|
|
|
|
}
|
2014-04-08 21:56:12 +05:30
|
|
|
case models.ErrOauth2NotAssociatedWithUser:
|
2014-04-12 07:12:09 +05:30
|
|
|
ctx.Session.Set("socialId", oa.Id)
|
2014-04-11 22:31:30 +05:30
|
|
|
ctx.Session.Set("socialName", soc.Name())
|
|
|
|
ctx.Session.Set("socialEmail", soc.Email())
|
|
|
|
ctx.Redirect("/user/sign_up")
|
|
|
|
return
|
2014-04-08 22:01:09 +05:30
|
|
|
default:
|
2014-04-09 00:57:35 +05:30
|
|
|
log.Error(err.Error()) // FIXME: handle error page
|
2014-04-08 22:01:09 +05:30
|
|
|
return
|
2014-04-07 15:31:25 +05:30
|
|
|
}
|
2014-04-08 21:56:12 +05:30
|
|
|
ctx.Session.Set("socialId", oa.Id)
|
2014-04-09 21:37:57 +05:30
|
|
|
log.Debug("socialId: %v", oa.Id)
|
|
|
|
ctx.Redirect(next)
|
2014-04-02 08:09:04 +05:30
|
|
|
}
|