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.

internal.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2017 The Gitea 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 private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
  5. package private
  6. import (
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/setting"
  10. macaron "gopkg.in/macaron.v1"
  11. )
  12. // CheckInternalToken check internal token is set
  13. func CheckInternalToken(ctx *macaron.Context) {
  14. tokens := ctx.Req.Header.Get("Authorization")
  15. fields := strings.Fields(tokens)
  16. if len(fields) != 2 || fields[0] != "Bearer" || fields[1] != setting.InternalToken {
  17. ctx.Error(403)
  18. }
  19. }
  20. //GetRepositoryByOwnerAndName chainload to models.GetRepositoryByOwnerAndName
  21. func GetRepositoryByOwnerAndName(ctx *macaron.Context) {
  22. //TODO use repo.Get(ctx *context.APIContext) ?
  23. ownerName := ctx.Params(":owner")
  24. repoName := ctx.Params(":repo")
  25. repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
  26. if err != nil {
  27. ctx.JSON(500, map[string]interface{}{
  28. "err": err.Error(),
  29. })
  30. return
  31. }
  32. ctx.JSON(200, repo)
  33. }
  34. //CheckUnitUser chainload to models.CheckUnitUser
  35. func CheckUnitUser(ctx *macaron.Context) {
  36. repoID := ctx.ParamsInt64(":repoid")
  37. userID := ctx.ParamsInt64(":userid")
  38. repo, err := models.GetRepositoryByID(repoID)
  39. if err != nil {
  40. ctx.JSON(500, map[string]interface{}{
  41. "err": err.Error(),
  42. })
  43. return
  44. }
  45. var user *models.User
  46. if userID > 0 {
  47. user, err = models.GetUserByID(userID)
  48. if err != nil {
  49. ctx.JSON(500, map[string]interface{}{
  50. "err": err.Error(),
  51. })
  52. return
  53. }
  54. }
  55. perm, err := models.GetUserRepoPermission(repo, user)
  56. if err != nil {
  57. ctx.JSON(500, map[string]interface{}{
  58. "err": err.Error(),
  59. })
  60. return
  61. }
  62. ctx.JSON(200, perm.UnitAccessMode(models.UnitType(ctx.QueryInt("unitType"))))
  63. }
  64. // RegisterRoutes registers all internal APIs routes to web application.
  65. // These APIs will be invoked by internal commands for example `gitea serv` and etc.
  66. func RegisterRoutes(m *macaron.Macaron) {
  67. m.Group("/", func() {
  68. m.Get("/ssh/:id", GetPublicKeyByID)
  69. m.Get("/ssh/:id/user", GetUserByKeyID)
  70. m.Post("/ssh/:id/update", UpdatePublicKey)
  71. m.Post("/repositories/:repoid/keys/:keyid/update", UpdateDeployKey)
  72. m.Get("/repositories/:repoid/user/:userid/checkunituser", CheckUnitUser)
  73. m.Get("/repositories/:repoid/has-keys/:keyid", HasDeployKey)
  74. m.Get("/repositories/:repoid/wiki/init", InitWiki)
  75. m.Post("/push/update", PushUpdate)
  76. m.Get("/protectedbranch/:pbid/:userid", CanUserPush)
  77. m.Get("/repo/:owner/:repo", GetRepositoryByOwnerAndName)
  78. m.Get("/branch/:id/*", GetProtectedBranchBy)
  79. m.Get("/repository/:rid", GetRepository)
  80. m.Get("/active-pull-request", GetActivePullRequest)
  81. }, CheckInternalToken)
  82. }