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