token.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "time"
  7. "github.com/go-xorm/xorm"
  8. "github.com/gogs/gogs/models/errors"
  9. "github.com/gogs/gogs/pkg/tool"
  10. gouuid "github.com/satori/go.uuid"
  11. )
  12. // AccessToken represents a personal access token.
  13. type AccessToken struct {
  14. ID int64
  15. UID int64 `xorm:"INDEX"`
  16. Name string
  17. Sha1 string `xorm:"UNIQUE VARCHAR(40)"`
  18. Created time.Time `xorm:"-" json:"-"`
  19. CreatedUnix int64
  20. Updated time.Time `xorm:"-" json:"-"` // Note: Updated must below Created for AfterSet.
  21. UpdatedUnix int64
  22. HasRecentActivity bool `xorm:"-" json:"-"`
  23. HasUsed bool `xorm:"-" json:"-"`
  24. }
  25. func (t *AccessToken) BeforeInsert() {
  26. t.CreatedUnix = time.Now().Unix()
  27. }
  28. func (t *AccessToken) BeforeUpdate() {
  29. t.UpdatedUnix = time.Now().Unix()
  30. }
  31. func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) {
  32. switch colName {
  33. case "created_unix":
  34. t.Created = time.Unix(t.CreatedUnix, 0).Local()
  35. case "updated_unix":
  36. t.Updated = time.Unix(t.UpdatedUnix, 0).Local()
  37. t.HasUsed = t.Updated.After(t.Created)
  38. t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now())
  39. }
  40. }
  41. // NewAccessToken creates new access token.
  42. func NewAccessToken(t *AccessToken) error {
  43. t.Sha1 = tool.SHA1(gouuid.NewV4().String())
  44. has, err := x.Get(&AccessToken{
  45. UID: t.UID,
  46. Name: t.Name,
  47. })
  48. if err != nil {
  49. return err
  50. } else if has {
  51. return errors.AccessTokenNameAlreadyExist{t.Name}
  52. }
  53. _, err = x.Insert(t)
  54. return err
  55. }
  56. // GetAccessTokenBySHA returns access token by given sha1.
  57. func GetAccessTokenBySHA(sha string) (*AccessToken, error) {
  58. if sha == "" {
  59. return nil, ErrAccessTokenEmpty{}
  60. }
  61. t := &AccessToken{Sha1: sha}
  62. has, err := x.Get(t)
  63. if err != nil {
  64. return nil, err
  65. } else if !has {
  66. return nil, ErrAccessTokenNotExist{sha}
  67. }
  68. return t, nil
  69. }
  70. // ListAccessTokens returns a list of access tokens belongs to given user.
  71. func ListAccessTokens(uid int64) ([]*AccessToken, error) {
  72. tokens := make([]*AccessToken, 0, 5)
  73. return tokens, x.Where("uid=?", uid).Desc("id").Find(&tokens)
  74. }
  75. // UpdateAccessToken updates information of access token.
  76. func UpdateAccessToken(t *AccessToken) error {
  77. _, err := x.Id(t.ID).AllCols().Update(t)
  78. return err
  79. }
  80. // DeleteAccessTokenOfUserByID deletes access token by given ID.
  81. func DeleteAccessTokenOfUserByID(userID, id int64) error {
  82. _, err := x.Delete(&AccessToken{
  83. ID: id,
  84. UID: userID,
  85. })
  86. return err
  87. }