token.go 2.7 KB

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