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

50 lines
1.1 KiB

11 years ago
// 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.
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
11 years ago
Uid int64 // userId
11 years ago
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) {
11 years ago
oa = &Oauth2{Identity: identity}
isExist, err := orm.Get(oa)
if err != nil {
return
11 years ago
} else if !isExist {
11 years ago
return nil, ErrOauth2RecordNotExists
11 years ago
} else if oa.Uid == 0 {
11 years ago
return oa, ErrOauth2NotAssociatedWithUser
}
oa.User, err = GetUserById(oa.Uid)
return oa, err
11 years ago
}