|
|
@ -8,36 +8,28 @@ import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"net/url" |
|
|
|
|
|
|
|
// "strings"
|
|
|
|
// "strings"
|
|
|
|
// "time"
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/macaron-contrib/oauth2" |
|
|
|
"github.com/macaron-contrib/oauth2" |
|
|
|
|
|
|
|
|
|
|
|
// "github.com/gogits/gogs/models"
|
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/middleware" |
|
|
|
"github.com/gogits/gogs/modules/middleware" |
|
|
|
"github.com/gogits/gogs/modules/setting" |
|
|
|
"github.com/gogits/gogs/modules/setting" |
|
|
|
"github.com/gogits/gogs/modules/social" |
|
|
|
"github.com/gogits/gogs/modules/social" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func extractPath(next string) string { |
|
|
|
|
|
|
|
n, err := url.Parse(next) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return setting.AppSubUrl + "/" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return n.Path |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func SocialSignIn(ctx *middleware.Context) { |
|
|
|
func SocialSignIn(ctx *middleware.Context) { |
|
|
|
if setting.OauthService == nil { |
|
|
|
if setting.OauthService == nil { |
|
|
|
ctx.Handle(404, "OAuth2 service not enabled", nil) |
|
|
|
ctx.Handle(404, "OAuth2 service not enabled", nil) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
next := setting.AppSubUrl + "/user/login" |
|
|
|
info := ctx.Session.Get(oauth2.KEY_TOKEN) |
|
|
|
info := ctx.Session.Get(oauth2.KEY_TOKEN) |
|
|
|
if info == nil { |
|
|
|
if info == nil { |
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/login") |
|
|
|
ctx.Redirect(next) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -61,39 +53,39 @@ func SocialSignIn(ctx *middleware.Context) { |
|
|
|
} |
|
|
|
} |
|
|
|
log.Info("social.SocialSignIn(social login): %s", ui) |
|
|
|
log.Info("social.SocialSignIn(social login): %s", ui) |
|
|
|
|
|
|
|
|
|
|
|
// oa, err := models.GetOauth2(ui.Identity)
|
|
|
|
oa, err := models.GetOauth2(ui.Identity) |
|
|
|
// switch err {
|
|
|
|
switch err { |
|
|
|
// case nil:
|
|
|
|
case nil: |
|
|
|
// ctx.Session.Set("uid", oa.User.Id)
|
|
|
|
ctx.Session.Set("uid", oa.User.Id) |
|
|
|
// ctx.Session.Set("uname", oa.User.Name)
|
|
|
|
ctx.Session.Set("uname", oa.User.Name) |
|
|
|
// case models.ErrOauth2RecordNotExist:
|
|
|
|
case models.ErrOauth2RecordNotExist: |
|
|
|
// raw, _ := json.Marshal(tk)
|
|
|
|
raw, _ := json.Marshal(tk) |
|
|
|
// oa = &models.Oauth2{
|
|
|
|
oa = &models.Oauth2{ |
|
|
|
// Uid: -1,
|
|
|
|
Uid: -1, |
|
|
|
// Type: connect.Type(),
|
|
|
|
Type: connect.Type(), |
|
|
|
// Identity: ui.Identity,
|
|
|
|
Identity: ui.Identity, |
|
|
|
// Token: string(raw),
|
|
|
|
Token: string(raw), |
|
|
|
// }
|
|
|
|
} |
|
|
|
// log.Trace("social.SocialSignIn(oa): %v", oa)
|
|
|
|
log.Trace("social.SocialSignIn(oa): %v", oa) |
|
|
|
// if err = models.AddOauth2(oa); err != nil {
|
|
|
|
if err = models.AddOauth2(oa); err != nil { |
|
|
|
// log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
|
|
|
|
log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
|
|
|
|
// return
|
|
|
|
return |
|
|
|
// }
|
|
|
|
} |
|
|
|
// case models.ErrOauth2NotAssociated:
|
|
|
|
case models.ErrOauth2NotAssociated: |
|
|
|
// next = setting.AppSubUrl + "/user/sign_up"
|
|
|
|
next = setting.AppSubUrl + "/user/sign_up" |
|
|
|
// default:
|
|
|
|
default: |
|
|
|
// ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
|
|
|
|
ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err) |
|
|
|
// return
|
|
|
|
return |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// oa.Updated = time.Now()
|
|
|
|
oa.Updated = time.Now() |
|
|
|
// if err = models.UpdateOauth2(oa); err != nil {
|
|
|
|
if err = models.UpdateOauth2(oa); err != nil { |
|
|
|
// log.Error(4, "UpdateOauth2: %v", err)
|
|
|
|
log.Error(4, "UpdateOauth2: %v", err) |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ctx.Session.Set("socialId", oa.Id)
|
|
|
|
ctx.Session.Set("socialId", oa.Id) |
|
|
|
// ctx.Session.Set("socialName", ui.Name)
|
|
|
|
ctx.Session.Set("socialName", ui.Name) |
|
|
|
// ctx.Session.Set("socialEmail", ui.Email)
|
|
|
|
ctx.Session.Set("socialEmail", ui.Email) |
|
|
|
// log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
|
|
|
|
log.Trace("social.SocialSignIn(social ID): %v", oa.Id) |
|
|
|
// ctx.Redirect(next)
|
|
|
|
ctx.Redirect(next) |
|
|
|
} |
|
|
|
} |
|
|
|