2014-04-01 21:39:04 -05:00
|
|
|
// 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 11:56:40 -05:00
|
|
|
|
2014-04-01 21:39:04 -05:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-04-12 10:19:17 -05:00
|
|
|
"fmt"
|
2014-04-09 11:07:57 -05:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2014-04-01 21:39:04 -05:00
|
|
|
|
2014-04-07 11:56:40 -05:00
|
|
|
"code.google.com/p/goauth2/oauth"
|
|
|
|
|
2014-04-12 10:19:17 -05:00
|
|
|
"github.com/go-martini/martini"
|
2014-04-07 05:01:25 -05:00
|
|
|
"github.com/gogits/gogs/models"
|
2014-04-09 11:07:57 -05:00
|
|
|
"github.com/gogits/gogs/modules/base"
|
2014-04-01 21:39:04 -05:00
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-04-07 05:01:25 -05:00
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
2014-04-01 21:39:04 -05:00
|
|
|
)
|
|
|
|
|
2014-04-12 10:19:17 -05:00
|
|
|
type BasicUserInfo struct {
|
|
|
|
Identity string
|
|
|
|
Name string
|
|
|
|
Email string
|
2014-04-07 05:01:25 -05:00
|
|
|
}
|
|
|
|
|
2014-04-12 10:19:17 -05:00
|
|
|
type SocialConnector interface {
|
|
|
|
Type() int
|
|
|
|
SetRedirectUrl(string)
|
2014-04-12 12:15:19 -05:00
|
|
|
UserInfo(*oauth.Token, *url.URL) (*BasicUserInfo, error)
|
2014-04-07 05:01:25 -05:00
|
|
|
|
2014-04-12 10:19:17 -05:00
|
|
|
AuthCodeURL(string) string
|
|
|
|
Exchange(string) (*oauth.Token, error)
|
2014-04-07 05:01:25 -05:00
|
|
|
}
|
2014-04-01 21:39:04 -05:00
|
|
|
|
2014-04-09 11:07:57 -05:00
|
|
|
func extractPath(next string) string {
|
|
|
|
n, err := url.Parse(next)
|
|
|
|
if err != nil {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
return n.Path
|
|
|
|
}
|
|
|
|
|
2014-04-12 10:19:17 -05:00
|
|
|
var (
|
|
|
|
SocialBaseUrl = "/user/login"
|
|
|
|
SocialMap = make(map[string]SocialConnector)
|
|
|
|
)
|
2014-04-11 20:42:09 -05:00
|
|
|
|
2014-04-12 10:19:17 -05:00
|
|
|
// github && google && ...
|
|
|
|
func SocialSignIn(params martini.Params, ctx *middleware.Context) {
|
|
|
|
if base.OauthService == nil || !base.OauthService.GitHub.Enabled {
|
|
|
|
ctx.Handle(404, "social login not enabled", nil)
|
2014-04-09 11:07:57 -05:00
|
|
|
return
|
|
|
|
}
|
2014-04-12 10:19:17 -05:00
|
|
|
next := extractPath(ctx.Query("next"))
|
|
|
|
name := params["name"]
|
|
|
|
connect, ok := SocialMap[name]
|
|
|
|
if !ok {
|
|
|
|
ctx.Handle(404, "social login", nil)
|
|
|
|
return
|
2014-04-09 11:07:57 -05:00
|
|
|
}
|
|
|
|
code := ctx.Query("code")
|
|
|
|
if code == "" {
|
|
|
|
// redirect to social login page
|
2014-04-12 12:15:19 -05:00
|
|
|
connect.SetRedirectUrl(strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.Host + ctx.Req.URL.Path)
|
2014-04-12 10:19:17 -05:00
|
|
|
ctx.Redirect(connect.AuthCodeURL(next))
|
2014-04-07 11:56:40 -05:00
|
|
|
return
|
|
|
|
}
|
2014-04-09 11:07:57 -05:00
|
|
|
|
|
|
|
// handle call back
|
2014-04-12 12:15:19 -05:00
|
|
|
tk, err := connect.Exchange(code) // exchange for token
|
2014-04-09 11:07:57 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("oauth2 handle callback error: %v", err)
|
2014-04-12 10:19:17 -05:00
|
|
|
ctx.Handle(500, "exchange code error", nil)
|
|
|
|
return
|
2014-04-09 11:07:57 -05:00
|
|
|
}
|
|
|
|
next = extractPath(ctx.Query("state"))
|
2014-04-12 10:19:17 -05:00
|
|
|
log.Trace("success get token")
|
2014-04-09 11:07:57 -05:00
|
|
|
|
2014-04-12 12:15:19 -05:00
|
|
|
ui, err := connect.UserInfo(tk, ctx.Req.URL)
|
2014-04-12 10:19:17 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, fmt.Sprintf("get infomation from %s error: %v", name, err), nil)
|
|
|
|
log.Error("social connect error: %s", err)
|
2014-04-07 05:01:25 -05:00
|
|
|
return
|
|
|
|
}
|
2014-04-12 10:19:17 -05:00
|
|
|
log.Info("social login: %s", ui)
|
|
|
|
oa, err := models.GetOauth2(ui.Identity)
|
2014-04-08 11:26:12 -05:00
|
|
|
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-12 10:19:17 -05:00
|
|
|
raw, _ := json.Marshal(tk) // json encode
|
|
|
|
oa.Token = string(raw)
|
2014-04-11 12:01:30 -05:00
|
|
|
oa.Uid = -1
|
2014-04-12 10:19:17 -05:00
|
|
|
oa.Type = connect.Type()
|
|
|
|
oa.Identity = ui.Identity
|
|
|
|
log.Trace("oa: %v", oa)
|
2014-04-07 05:01:25 -05:00
|
|
|
if err = models.AddOauth2(oa); err != nil {
|
2014-04-09 11:07:57 -05:00
|
|
|
log.Error("add oauth2 %v", err) // 501
|
2014-04-07 05:01:25 -05:00
|
|
|
return
|
|
|
|
}
|
2014-04-08 11:26:12 -05:00
|
|
|
case models.ErrOauth2NotAssociatedWithUser:
|
2014-04-12 10:19:17 -05:00
|
|
|
next = "/user/sign_up"
|
2014-04-08 11:31:09 -05:00
|
|
|
default:
|
2014-04-12 10:19:17 -05:00
|
|
|
log.Error("other error: %v", err)
|
|
|
|
ctx.Handle(500, err.Error(), nil)
|
2014-04-08 11:31:09 -05:00
|
|
|
return
|
2014-04-07 05:01:25 -05:00
|
|
|
}
|
2014-04-08 11:26:12 -05:00
|
|
|
ctx.Session.Set("socialId", oa.Id)
|
2014-04-12 10:19:17 -05:00
|
|
|
ctx.Session.Set("socialName", ui.Name)
|
|
|
|
ctx.Session.Set("socialEmail", ui.Email)
|
|
|
|
log.Trace("socialId: %v", oa.Id)
|
2014-04-09 11:07:57 -05:00
|
|
|
ctx.Redirect(next)
|
2014-04-01 21:39:04 -05:00
|
|
|
}
|