oauth2.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 (
  6. "errors"
  7. "time"
  8. )
  9. type OauthType int
  10. const (
  11. GITHUB OauthType = iota + 1
  12. GOOGLE
  13. TWITTER
  14. QQ
  15. WEIBO
  16. BITBUCKET
  17. FACEBOOK
  18. )
  19. var (
  20. ErrOauth2RecordNotExist = errors.New("OAuth2 record does not exist")
  21. ErrOauth2NotAssociated = errors.New("OAuth2 is not associated with user")
  22. )
  23. type Oauth2 struct {
  24. Id int64
  25. Uid int64 `xorm:"unique(s)"` // userId
  26. User *User `xorm:"-"`
  27. Type int `xorm:"unique(s) unique(oauth)"` // twitter,github,google...
  28. Identity string `xorm:"unique(s) unique(oauth)"` // id..
  29. Token string `xorm:"TEXT not null"`
  30. Created time.Time `xorm:"CREATED"`
  31. Updated time.Time
  32. HasRecentActivity bool `xorm:"-"`
  33. }
  34. func BindUserOauth2(userId, oauthId int64) error {
  35. _, err := x.Id(oauthId).Update(&Oauth2{Uid: userId})
  36. return err
  37. }
  38. func AddOauth2(oa *Oauth2) error {
  39. _, err := x.Insert(oa)
  40. return err
  41. }
  42. func GetOauth2(identity string) (oa *Oauth2, err error) {
  43. oa = &Oauth2{Identity: identity}
  44. isExist, err := x.Get(oa)
  45. if err != nil {
  46. return
  47. } else if !isExist {
  48. return nil, ErrOauth2RecordNotExist
  49. } else if oa.Uid == -1 {
  50. return oa, ErrOauth2NotAssociated
  51. }
  52. oa.User, err = GetUserById(oa.Uid)
  53. return oa, err
  54. }
  55. func GetOauth2ById(id int64) (oa *Oauth2, err error) {
  56. oa = new(Oauth2)
  57. has, err := x.Id(id).Get(oa)
  58. if err != nil {
  59. return nil, err
  60. } else if !has {
  61. return nil, ErrOauth2RecordNotExist
  62. }
  63. return oa, nil
  64. }
  65. // UpdateOauth2 updates given OAuth2.
  66. func UpdateOauth2(oa *Oauth2) error {
  67. _, err := x.Id(oa.Id).AllCols().Update(oa)
  68. return err
  69. }
  70. // GetOauthByUserId returns list of oauthes that are related to given user.
  71. func GetOauthByUserId(uid int64) ([]*Oauth2, error) {
  72. socials := make([]*Oauth2, 0, 5)
  73. err := x.Find(&socials, Oauth2{Uid: uid})
  74. if err != nil {
  75. return nil, err
  76. }
  77. for _, social := range socials {
  78. social.HasRecentActivity = social.Updated.Add(7 * 24 * time.Hour).After(time.Now())
  79. }
  80. return socials, err
  81. }
  82. // DeleteOauth2ById deletes a oauth2 by ID.
  83. func DeleteOauth2ById(id int64) error {
  84. _, err := x.Delete(&Oauth2{Id: id})
  85. return err
  86. }
  87. // CleanUnbindOauth deletes all unbind OAuthes.
  88. func CleanUnbindOauth() error {
  89. _, err := x.Delete(&Oauth2{Uid: -1})
  90. return err
  91. }