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.

token.go 2.6KB

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