oauth2.go 775 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import "fmt"
  3. // OT: Oauth2 Type
  4. const (
  5. OT_GITHUB = iota + 1
  6. OT_GOOGLE
  7. OT_TWITTER
  8. )
  9. type Oauth2 struct {
  10. Uid int64 `xorm:"pk"` // userId
  11. Type int `xorm:"pk unique(oauth)"` // twitter,github,google...
  12. Identity string `xorm:"pk unique(oauth)"` // id..
  13. Token string `xorm:"VARCHAR(200) not null"`
  14. //RefreshTime time.Time `xorm:"created"`
  15. }
  16. func AddOauth2(oa *Oauth2) (err error) {
  17. if _, err = orm.Insert(oa); err != nil {
  18. return err
  19. }
  20. return nil
  21. }
  22. func GetOauth2User(identity string) (u *User, err error) {
  23. oa := &Oauth2{}
  24. oa.Identity = identity
  25. exists, err := orm.Get(oa)
  26. if err != nil {
  27. return
  28. }
  29. if !exists {
  30. err = fmt.Errorf("not exists oauth2: %s", identity)
  31. return
  32. }
  33. return GetUserById(oa.Uid)
  34. }