Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "net/http"
  6. auth_model "code.gitea.io/gitea/models/auth"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/models/unit"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // RequireRepoAdmin returns a middleware for requiring repository admin permission
  12. func RequireRepoAdmin() func(ctx *Context) {
  13. return func(ctx *Context) {
  14. if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
  15. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  16. return
  17. }
  18. }
  19. }
  20. // RequireRepoWriter returns a middleware for requiring repository write to the specify unitType
  21. func RequireRepoWriter(unitType unit.Type) func(ctx *Context) {
  22. return func(ctx *Context) {
  23. if !ctx.Repo.CanWrite(unitType) {
  24. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  25. return
  26. }
  27. }
  28. }
  29. // CanEnableEditor checks if the user is allowed to write to the branch of the repo
  30. func CanEnableEditor() func(ctx *Context) {
  31. return func(ctx *Context) {
  32. if !ctx.Repo.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
  33. ctx.NotFound("CanWriteToBranch denies permission", nil)
  34. return
  35. }
  36. }
  37. }
  38. // RequireRepoWriterOr returns a middleware for requiring repository write to one of the unit permission
  39. func RequireRepoWriterOr(unitTypes ...unit.Type) func(ctx *Context) {
  40. return func(ctx *Context) {
  41. for _, unitType := range unitTypes {
  42. if ctx.Repo.CanWrite(unitType) {
  43. return
  44. }
  45. }
  46. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  47. }
  48. }
  49. // RequireRepoReader returns a middleware for requiring repository read to the specify unitType
  50. func RequireRepoReader(unitType unit.Type) func(ctx *Context) {
  51. return func(ctx *Context) {
  52. if !ctx.Repo.CanRead(unitType) {
  53. if log.IsTrace() {
  54. if ctx.IsSigned {
  55. log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+
  56. "User in Repo has Permissions: %-+v",
  57. ctx.Doer,
  58. unitType,
  59. ctx.Repo.Repository,
  60. ctx.Repo.Permission)
  61. } else {
  62. log.Trace("Permission Denied: Anonymous user cannot read %-v in Repo %-v\n"+
  63. "Anonymous user in Repo has Permissions: %-+v",
  64. unitType,
  65. ctx.Repo.Repository,
  66. ctx.Repo.Permission)
  67. }
  68. }
  69. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  70. return
  71. }
  72. }
  73. }
  74. // RequireRepoReaderOr returns a middleware for requiring repository write to one of the unit permission
  75. func RequireRepoReaderOr(unitTypes ...unit.Type) func(ctx *Context) {
  76. return func(ctx *Context) {
  77. for _, unitType := range unitTypes {
  78. if ctx.Repo.CanRead(unitType) {
  79. return
  80. }
  81. }
  82. if log.IsTrace() {
  83. var format string
  84. var args []any
  85. if ctx.IsSigned {
  86. format = "Permission Denied: User %-v cannot read ["
  87. args = append(args, ctx.Doer)
  88. } else {
  89. format = "Permission Denied: Anonymous user cannot read ["
  90. }
  91. for _, unit := range unitTypes {
  92. format += "%-v, "
  93. args = append(args, unit)
  94. }
  95. format = format[:len(format)-2] + "] in Repo %-v\n" +
  96. "User in Repo has Permissions: %-+v"
  97. args = append(args, ctx.Repo.Repository, ctx.Repo.Permission)
  98. log.Trace(format, args...)
  99. }
  100. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  101. }
  102. }
  103. // CheckRepoScopedToken check whether personal access token has repo scope
  104. func CheckRepoScopedToken(ctx *Context, repo *repo_model.Repository, level auth_model.AccessTokenScopeLevel) {
  105. if !ctx.IsBasicAuth || ctx.Data["IsApiToken"] != true {
  106. return
  107. }
  108. scope, ok := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope)
  109. if ok { // it's a personal access token but not oauth2 token
  110. var scopeMatched bool
  111. requiredScopes := auth_model.GetRequiredScopes(level, auth_model.AccessTokenScopeCategoryRepository)
  112. // check if scope only applies to public resources
  113. publicOnly, err := scope.PublicOnly()
  114. if err != nil {
  115. ctx.ServerError("HasScope", err)
  116. return
  117. }
  118. if publicOnly && repo.IsPrivate {
  119. ctx.Error(http.StatusForbidden)
  120. return
  121. }
  122. scopeMatched, err = scope.HasScope(requiredScopes...)
  123. if err != nil {
  124. ctx.ServerError("HasScope", err)
  125. return
  126. }
  127. if !scopeMatched {
  128. ctx.Error(http.StatusForbidden)
  129. return
  130. }
  131. }
  132. }