mirror of https://github.com/go-gitea/gitea.git
add id for oauth2
This commit is contained in:
parent
22feddf804
commit
d4565483e6
|
@ -2,12 +2,12 @@
|
||||||
"paths": ["."],
|
"paths": ["."],
|
||||||
"depth": 2,
|
"depth": 2,
|
||||||
"exclude": [],
|
"exclude": [],
|
||||||
"include": ["\\.go$"],
|
"include": ["\\.go$", "\\.ini$"],
|
||||||
"command": [
|
"command": [
|
||||||
"bash", "-c", "go build && ./gogs web"
|
"bash", "-c", "go build && ./gogs web"
|
||||||
],
|
],
|
||||||
"env": {
|
"env": {
|
||||||
"POWERED_BY": "github.com/shxsun/fswatch"
|
"POWERED_BY": "github.com/shxsun/fswatch"
|
||||||
},
|
},
|
||||||
"enable-restart": true
|
"enable-restart": false
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// OT: Oauth2 Type
|
// OT: Oauth2 Type
|
||||||
const (
|
const (
|
||||||
|
@ -9,12 +12,18 @@ const (
|
||||||
OT_TWITTER
|
OT_TWITTER
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrOauth2RecordNotExists = errors.New("not exists oauth2 record")
|
||||||
|
ErrOauth2NotAssociatedWithUser = errors.New("not associated with user")
|
||||||
|
)
|
||||||
|
|
||||||
type Oauth2 struct {
|
type Oauth2 struct {
|
||||||
|
Id int64
|
||||||
Uid int64 `xorm:"pk"` // userId
|
Uid int64 `xorm:"pk"` // userId
|
||||||
|
User *User `xorm:"-"`
|
||||||
Type int `xorm:"pk unique(oauth)"` // twitter,github,google...
|
Type int `xorm:"pk unique(oauth)"` // twitter,github,google...
|
||||||
Identity string `xorm:"pk unique(oauth)"` // id..
|
Identity string `xorm:"pk unique(oauth)"` // id..
|
||||||
Token string `xorm:"VARCHAR(200) not null"`
|
Token string `xorm:"VARCHAR(200) not null"`
|
||||||
//RefreshTime time.Time `xorm:"created"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddOauth2(oa *Oauth2) (err error) {
|
func AddOauth2(oa *Oauth2) (err error) {
|
||||||
|
@ -24,8 +33,8 @@ func AddOauth2(oa *Oauth2) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOauth2User(identity string) (u *User, err error) {
|
func GetOauth2(identity string) (oa *Oauth2, err error) {
|
||||||
oa := &Oauth2{}
|
oa = &Oauth2{}
|
||||||
oa.Identity = identity
|
oa.Identity = identity
|
||||||
exists, err := orm.Get(oa)
|
exists, err := orm.Get(oa)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -35,5 +44,9 @@ func GetOauth2User(identity string) (u *User, err error) {
|
||||||
err = fmt.Errorf("not exists oauth2: %s", identity)
|
err = fmt.Errorf("not exists oauth2: %s", identity)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return GetUserById(oa.Uid)
|
if oa.Uid == 0 {
|
||||||
|
return oa, ErrOauth2NotAssociatedWithUser
|
||||||
|
}
|
||||||
|
oa.User, err = GetUserById(oa.Uid)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"code.google.com/p/goauth2/oauth"
|
"code.google.com/p/goauth2/oauth"
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/base"
|
|
||||||
"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/oauth2"
|
"github.com/gogits/gogs/modules/oauth2"
|
||||||
|
@ -85,7 +84,6 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
var u *models.User
|
|
||||||
if err = gh.Update(); err != nil {
|
if err = gh.Update(); err != nil {
|
||||||
// FIXME: handle error page
|
// FIXME: handle error page
|
||||||
log.Error("connect with github error: %s", err)
|
log.Error("connect with github error: %s", err)
|
||||||
|
@ -93,20 +91,14 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
|
||||||
}
|
}
|
||||||
var soc SocialConnector = gh
|
var soc SocialConnector = gh
|
||||||
log.Info("login: %s", soc.Name())
|
log.Info("login: %s", soc.Name())
|
||||||
// FIXME: login here, user email to check auth, if not registe, then generate a uniq username
|
oa, err := models.GetOauth2(soc.Identity())
|
||||||
if u, err = models.GetOauth2User(soc.Identity()); err != nil {
|
switch err {
|
||||||
u = &models.User{
|
case nil:
|
||||||
Name: soc.Name(),
|
ctx.Session.Set("userId", oa.User.Id)
|
||||||
Email: soc.Email(),
|
ctx.Session.Set("userName", oa.User.Name)
|
||||||
Passwd: "123456",
|
case models.ErrOauth2RecordNotExists:
|
||||||
IsActive: !base.Service.RegisterEmailConfirm,
|
oa = &models.Oauth2{}
|
||||||
}
|
oa.Uid = 0
|
||||||
if u, err = models.RegisterUser(u); err != nil {
|
|
||||||
log.Error("register user: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
oa := &models.Oauth2{}
|
|
||||||
oa.Uid = u.Id
|
|
||||||
oa.Type = soc.Type()
|
oa.Type = soc.Type()
|
||||||
oa.Token = soc.Token()
|
oa.Token = soc.Token()
|
||||||
oa.Identity = soc.Identity()
|
oa.Identity = soc.Identity()
|
||||||
|
@ -115,8 +107,10 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
|
||||||
log.Error("add oauth2 %v", err)
|
log.Error("add oauth2 %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case models.ErrOauth2NotAssociatedWithUser:
|
||||||
|
// pass
|
||||||
}
|
}
|
||||||
ctx.Session.Set("userId", u.Id)
|
ctx.Session.Set("socialId", oa.Id)
|
||||||
ctx.Session.Set("userName", u.Name)
|
log.Info("socialId: %v", oa.Id)
|
||||||
ctx.Redirect("/")
|
ctx.Redirect("/")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue