2014-04-01 19:39:04 -07: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 09:56:40 -07:00
|
|
|
|
2014-04-01 19:39:04 -07:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-04-13 15:12:07 -07:00
|
|
|
"errors"
|
2014-04-12 08:19:17 -07:00
|
|
|
"fmt"
|
2014-04-09 09:07:57 -07:00
|
|
|
"net/url"
|
2014-11-28 19:20:13 -07:00
|
|
|
// "strings"
|
|
|
|
// "time"
|
2014-04-01 19:39:04 -07:00
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
"github.com/macaron-contrib/oauth2"
|
|
|
|
|
|
|
|
// "github.com/gogits/gogs/models"
|
2014-04-01 19:39:04 -07:00
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-04-07 03:01:25 -07:00
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
2014-05-25 17:11:25 -07:00
|
|
|
"github.com/gogits/gogs/modules/setting"
|
2014-04-13 15:12:07 -07:00
|
|
|
"github.com/gogits/gogs/modules/social"
|
2014-04-01 19:39:04 -07:00
|
|
|
)
|
|
|
|
|
2014-04-09 09:07:57 -07:00
|
|
|
func extractPath(next string) string {
|
|
|
|
n, err := url.Parse(next)
|
|
|
|
if err != nil {
|
2014-09-19 17:11:34 -07:00
|
|
|
return setting.AppSubUrl + "/"
|
2014-04-09 09:07:57 -07:00
|
|
|
}
|
|
|
|
return n.Path
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:24:27 -07:00
|
|
|
func SocialSignIn(ctx *middleware.Context) {
|
2014-05-25 17:11:25 -07:00
|
|
|
if setting.OauthService == nil {
|
2014-11-28 19:20:13 -07:00
|
|
|
ctx.Handle(404, "OAuth2 service not enabled", nil)
|
2014-04-09 09:07:57 -07:00
|
|
|
return
|
|
|
|
}
|
2014-04-13 15:12:07 -07:00
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
info := ctx.Session.Get(oauth2.KEY_TOKEN)
|
|
|
|
if info == nil {
|
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/login")
|
2014-04-12 08:19:17 -07:00
|
|
|
return
|
2014-04-09 09:07:57 -07:00
|
|
|
}
|
2014-04-13 15:12:07 -07:00
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
name := ctx.Params(":name")
|
|
|
|
connect, ok := social.SocialMap[name]
|
|
|
|
if !ok {
|
|
|
|
ctx.Handle(404, "social login not enabled", errors.New(name))
|
2014-04-07 09:56:40 -07:00
|
|
|
return
|
|
|
|
}
|
2014-04-09 09:07:57 -07:00
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
tk := new(oauth2.Token)
|
|
|
|
if err := json.Unmarshal(info.([]byte), tk); err != nil {
|
|
|
|
ctx.Handle(500, "Unmarshal token", err)
|
2014-04-12 08:19:17 -07:00
|
|
|
return
|
2014-04-09 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2014-04-12 10:15:19 -07:00
|
|
|
ui, err := connect.UserInfo(tk, ctx.Req.URL)
|
2014-04-12 08:19:17 -07:00
|
|
|
if err != nil {
|
2014-11-28 19:20:13 -07:00
|
|
|
ctx.Handle(500, fmt.Sprintf("UserInfo(%s)", name), err)
|
2014-04-07 03:01:25 -07:00
|
|
|
return
|
|
|
|
}
|
2014-04-13 15:12:07 -07:00
|
|
|
log.Info("social.SocialSignIn(social login): %s", ui)
|
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
// oa, err := models.GetOauth2(ui.Identity)
|
|
|
|
// switch err {
|
|
|
|
// case nil:
|
|
|
|
// ctx.Session.Set("uid", oa.User.Id)
|
|
|
|
// ctx.Session.Set("uname", oa.User.Name)
|
|
|
|
// case models.ErrOauth2RecordNotExist:
|
|
|
|
// raw, _ := json.Marshal(tk)
|
|
|
|
// oa = &models.Oauth2{
|
|
|
|
// Uid: -1,
|
|
|
|
// Type: connect.Type(),
|
|
|
|
// Identity: ui.Identity,
|
|
|
|
// Token: string(raw),
|
|
|
|
// }
|
|
|
|
// log.Trace("social.SocialSignIn(oa): %v", oa)
|
|
|
|
// if err = models.AddOauth2(oa); err != nil {
|
|
|
|
// log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// case models.ErrOauth2NotAssociated:
|
|
|
|
// next = setting.AppSubUrl + "/user/sign_up"
|
|
|
|
// default:
|
|
|
|
// ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
|
|
|
|
// return
|
|
|
|
// }
|
2014-04-13 15:12:07 -07:00
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
// oa.Updated = time.Now()
|
|
|
|
// if err = models.UpdateOauth2(oa); err != nil {
|
|
|
|
// log.Error(4, "UpdateOauth2: %v", err)
|
|
|
|
// }
|
2014-08-09 17:25:02 -07:00
|
|
|
|
2014-11-28 19:20:13 -07:00
|
|
|
// ctx.Session.Set("socialId", oa.Id)
|
|
|
|
// ctx.Session.Set("socialName", ui.Name)
|
|
|
|
// ctx.Session.Set("socialEmail", ui.Email)
|
|
|
|
// log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
|
|
|
|
// ctx.Redirect(next)
|
2014-04-01 19:39:04 -07:00
|
|
|
}
|