You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/models/oauth2.go

49 lines
976 B

11 years ago
package models
11 years ago
import "errors"
11 years ago
// OT: Oauth2 Type
const (
OT_GITHUB = iota + 1
OT_GOOGLE
OT_TWITTER
)
11 years ago
var (
ErrOauth2RecordNotExists = errors.New("not exists oauth2 record")
ErrOauth2NotAssociatedWithUser = errors.New("not associated with user")
)
11 years ago
type Oauth2 struct {
11 years ago
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"`
}
func AddOauth2(oa *Oauth2) (err error) {
if _, err = orm.Insert(oa); err != nil {
return err
}
return nil
}
11 years ago
func GetOauth2(identity string) (oa *Oauth2, err error) {
oa = &Oauth2{}
oa.Identity = identity
exists, err := orm.Get(oa)
if err != nil {
return
}
if !exists {
11 years ago
return nil, ErrOauth2RecordNotExists
}
11 years ago
if oa.Uid == 0 {
return oa, ErrOauth2NotAssociatedWithUser
}
oa.User, err = GetUserById(oa.Uid)
return
11 years ago
}