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.

130 lines
3.6 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
9 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "crypto/subtle"
  8. "time"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/generate"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. gouuid "github.com/google/uuid"
  13. )
  14. // AccessToken represents a personal access token.
  15. type AccessToken struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. UID int64 `xorm:"INDEX"`
  18. Name string
  19. Token string `xorm:"-"`
  20. TokenHash string `xorm:"UNIQUE"` // sha256 of token
  21. TokenSalt string
  22. TokenLastEight string `xorm:"token_last_eight"`
  23. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  24. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  25. HasRecentActivity bool `xorm:"-"`
  26. HasUsed bool `xorm:"-"`
  27. }
  28. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  29. func (t *AccessToken) AfterLoad() {
  30. t.HasUsed = t.UpdatedUnix > t.CreatedUnix
  31. t.HasRecentActivity = t.UpdatedUnix.AddDuration(7*24*time.Hour) > timeutil.TimeStampNow()
  32. }
  33. // NewAccessToken creates new access token.
  34. func NewAccessToken(t *AccessToken) error {
  35. salt, err := generate.GetRandomString(10)
  36. if err != nil {
  37. return err
  38. }
  39. t.TokenSalt = salt
  40. t.Token = base.EncodeSha1(gouuid.New().String())
  41. t.TokenHash = hashToken(t.Token, t.TokenSalt)
  42. t.TokenLastEight = t.Token[len(t.Token)-8:]
  43. _, err = x.Insert(t)
  44. return err
  45. }
  46. // GetAccessTokenBySHA returns access token by given token value
  47. func GetAccessTokenBySHA(token string) (*AccessToken, error) {
  48. if token == "" {
  49. return nil, ErrAccessTokenEmpty{}
  50. }
  51. if len(token) < 8 {
  52. return nil, ErrAccessTokenNotExist{token}
  53. }
  54. var tokens []AccessToken
  55. lastEight := token[len(token)-8:]
  56. err := x.Table(&AccessToken{}).Where("token_last_eight = ?", lastEight).Find(&tokens)
  57. if err != nil {
  58. return nil, err
  59. } else if len(tokens) == 0 {
  60. return nil, ErrAccessTokenNotExist{token}
  61. }
  62. for _, t := range tokens {
  63. tempHash := hashToken(token, t.TokenSalt)
  64. if subtle.ConstantTimeCompare([]byte(t.TokenHash), []byte(tempHash)) == 1 {
  65. return &t, nil
  66. }
  67. }
  68. return nil, ErrAccessTokenNotExist{token}
  69. }
  70. // AccessTokenByNameExists checks if a token name has been used already by a user.
  71. func AccessTokenByNameExists(token *AccessToken) (bool, error) {
  72. return x.Table("access_token").Where("name = ?", token.Name).And("uid = ?", token.UID).Exist()
  73. }
  74. // ListAccessTokensOptions contain filter options
  75. type ListAccessTokensOptions struct {
  76. ListOptions
  77. Name string
  78. UserID int64
  79. }
  80. // ListAccessTokens returns a list of access tokens belongs to given user.
  81. func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
  82. sess := x.Where("uid=?", opts.UserID)
  83. if len(opts.Name) != 0 {
  84. sess = sess.Where("name=?", opts.Name)
  85. }
  86. sess = sess.Desc("id")
  87. if opts.Page != 0 {
  88. sess = opts.setSessionPagination(sess)
  89. tokens := make([]*AccessToken, 0, opts.PageSize)
  90. return tokens, sess.Find(&tokens)
  91. }
  92. tokens := make([]*AccessToken, 0, 5)
  93. return tokens, sess.Find(&tokens)
  94. }
  95. // UpdateAccessToken updates information of access token.
  96. func UpdateAccessToken(t *AccessToken) error {
  97. _, err := x.ID(t.ID).AllCols().Update(t)
  98. return err
  99. }
  100. // DeleteAccessTokenByID deletes access token by given ID.
  101. func DeleteAccessTokenByID(id, userID int64) error {
  102. cnt, err := x.ID(id).Delete(&AccessToken{
  103. UID: userID,
  104. })
  105. if err != nil {
  106. return err
  107. } else if cnt != 1 {
  108. return ErrAccessTokenNotExist{}
  109. }
  110. return nil
  111. }