token.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "gitote/gitote/pkg/tool"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. gouuid "github.com/satori/go.uuid"
  12. )
  13. // AccessToken represents a personal access token.
  14. type AccessToken struct {
  15. ID int64
  16. UID int64 `xorm:"INDEX"`
  17. Name string
  18. Sha1 string `xorm:"UNIQUE VARCHAR(40)"`
  19. Created time.Time `xorm:"-" json:"-"`
  20. CreatedUnix int64
  21. Updated time.Time `xorm:"-" json:"-"` // Note: Updated must below Created for AfterSet.
  22. UpdatedUnix int64
  23. HasRecentActivity bool `xorm:"-" json:"-"`
  24. HasUsed bool `xorm:"-" json:"-"`
  25. }
  26. // BeforeInsert will be invoked by XORM before inserting a record
  27. func (t *AccessToken) BeforeInsert() {
  28. t.CreatedUnix = time.Now().Unix()
  29. }
  30. // BeforeUpdate is invoked from XORM before updating this object.
  31. func (t *AccessToken) BeforeUpdate() {
  32. t.UpdatedUnix = time.Now().Unix()
  33. }
  34. // AfterSet is invoked from XORM after setting the values of all fields of this object.
  35. func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) {
  36. switch colName {
  37. case "created_unix":
  38. t.Created = time.Unix(t.CreatedUnix, 0).Local()
  39. case "updated_unix":
  40. t.Updated = time.Unix(t.UpdatedUnix, 0).Local()
  41. t.HasUsed = t.Updated.After(t.Created)
  42. t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now())
  43. }
  44. }
  45. // NewAccessToken creates new access token.
  46. func NewAccessToken(t *AccessToken) error {
  47. t.Sha1 = tool.SHA1(gouuid.NewV4().String())
  48. _, err := x.Insert(t)
  49. return err
  50. }
  51. // GetAccessTokenBySHA returns access token by given sha1.
  52. func GetAccessTokenBySHA(sha string) (*AccessToken, error) {
  53. if sha == "" {
  54. return nil, ErrAccessTokenEmpty{}
  55. }
  56. t := &AccessToken{Sha1: sha}
  57. has, err := x.Get(t)
  58. if err != nil {
  59. return nil, err
  60. } else if !has {
  61. return nil, ErrAccessTokenNotExist{sha}
  62. }
  63. return t, nil
  64. }
  65. // ListAccessTokens returns a list of access tokens belongs to given user.
  66. func ListAccessTokens(uid int64) ([]*AccessToken, error) {
  67. tokens := make([]*AccessToken, 0, 5)
  68. return tokens, x.Where("uid=?", uid).Desc("id").Find(&tokens)
  69. }
  70. // UpdateAccessToken updates information of access token.
  71. func UpdateAccessToken(t *AccessToken) error {
  72. _, err := x.Id(t.ID).AllCols().Update(t)
  73. return err
  74. }
  75. // DeleteAccessTokenOfUserByID deletes access token by given ID.
  76. func DeleteAccessTokenOfUserByID(userID, id int64) error {
  77. _, err := x.Delete(&AccessToken{
  78. ID: id,
  79. UID: userID,
  80. })
  81. return err
  82. }