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.

49 lines
1.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import "errors"
  6. // OT: Oauth2 Type
  7. const (
  8. OT_GITHUB = iota + 1
  9. OT_GOOGLE
  10. OT_TWITTER
  11. )
  12. var (
  13. ErrOauth2RecordNotExists = errors.New("not exists oauth2 record")
  14. ErrOauth2NotAssociatedWithUser = errors.New("not associated with user")
  15. )
  16. type Oauth2 struct {
  17. Id int64
  18. Uid int64 // userId
  19. User *User `xorm:"-"`
  20. Type int `xorm:"pk unique(oauth)"` // twitter,github,google...
  21. Identity string `xorm:"pk unique(oauth)"` // id..
  22. Token string `xorm:"VARCHAR(200) not null"`
  23. }
  24. func AddOauth2(oa *Oauth2) (err error) {
  25. if _, err = orm.Insert(oa); err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. func GetOauth2(identity string) (oa *Oauth2, err error) {
  31. oa = &Oauth2{Identity: identity}
  32. isExist, err := orm.Get(oa)
  33. if err != nil {
  34. return
  35. } else if !isExist {
  36. return nil, ErrOauth2RecordNotExists
  37. } else if oa.Uid == 0 {
  38. return oa, ErrOauth2NotAssociatedWithUser
  39. }
  40. oa.User, err = GetUserById(oa.Uid)
  41. return oa, err
  42. }