summaryrefslogtreecommitdiffstats
path: root/models/oauth2.go
blob: 4da98006708218d40ef39f8413594e7d43c29363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package models

import "errors"

// OT: Oauth2 Type
const (
	OT_GITHUB = iota + 1
	OT_GOOGLE
	OT_TWITTER
)

var (
	ErrOauth2RecordNotExists       = errors.New("not exists oauth2 record")
	ErrOauth2NotAssociatedWithUser = errors.New("not associated with user")
)

type Oauth2 struct {
	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
}

func GetOauth2(identity string) (oa *Oauth2, err error) {
	oa = &Oauth2{}
	oa.Identity = identity
	exists, err := orm.Get(oa)
	if err != nil {
		return
	}
	if !exists {
		return nil, ErrOauth2RecordNotExists
	}
	if oa.Uid == 0 {
		return oa, ErrOauth2NotAssociatedWithUser
	}
	oa.User, err = GetUserById(oa.Uid)
	return
}