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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 `xorm:"INDEX created"`
  19. Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
  20. UpdatedUnix int64 `xorm:"INDEX updated"`
  21. HasRecentActivity bool `xorm:"-"`
  22. HasUsed bool `xorm:"-"`
  23. }
  24. // AfterSet is invoked from XORM after setting the value of a field of this object.
  25. func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) {
  26. switch colName {
  27. case "created_unix":
  28. t.Created = time.Unix(t.CreatedUnix, 0).Local()
  29. case "updated_unix":
  30. t.Updated = time.Unix(t.UpdatedUnix, 0).Local()
  31. t.HasUsed = t.Updated.After(t.Created)
  32. t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now())
  33. }
  34. }
  35. // NewAccessToken creates new access token.
  36. func NewAccessToken(t *AccessToken) error {
  37. t.Sha1 = base.EncodeSha1(gouuid.NewV4().String())
  38. _, err := x.Insert(t)
  39. return err
  40. }
  41. // GetAccessTokenBySHA returns access token by given sha1.
  42. func GetAccessTokenBySHA(sha string) (*AccessToken, error) {
  43. if sha == "" {
  44. return nil, ErrAccessTokenEmpty{}
  45. }
  46. t := &AccessToken{Sha1: sha}
  47. has, err := x.Get(t)
  48. if err != nil {
  49. return nil, err
  50. } else if !has {
  51. return nil, ErrAccessTokenNotExist{sha}
  52. }
  53. return t, nil
  54. }
  55. // ListAccessTokens returns a list of access tokens belongs to given user.
  56. func ListAccessTokens(uid int64) ([]*AccessToken, error) {
  57. tokens := make([]*AccessToken, 0, 5)
  58. return tokens, x.
  59. Where("uid=?", uid).
  60. Desc("id").
  61. Find(&tokens)
  62. }
  63. // UpdateAccessToken updates information of access token.
  64. func UpdateAccessToken(t *AccessToken) error {
  65. _, err := x.Id(t.ID).AllCols().Update(t)
  66. return err
  67. }
  68. // DeleteAccessTokenByID deletes access token by given ID.
  69. func DeleteAccessTokenByID(id, userID int64) error {
  70. cnt, err := x.Id(id).Delete(&AccessToken{
  71. UID: userID,
  72. })
  73. if err != nil {
  74. return err
  75. } else if cnt != 1 {
  76. return ErrAccessTokenNotExist{}
  77. }
  78. return nil
  79. }