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.

94 lines
2.8 KiB

  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/keys/:keyid", GetDeployKey)
  75. m.Get("/repositories/:repoid/wiki/init", InitWiki)
  76. m.Post("/push/update", PushUpdate)
  77. m.Get("/protectedbranch/:pbid/:userid", CanUserPush)
  78. m.Get("/repo/:owner/:repo", GetRepositoryByOwnerAndName)
  79. m.Get("/branch/:id/*", GetProtectedBranchBy)
  80. m.Get("/repository/:rid", GetRepository)
  81. m.Get("/active-pull-request", GetActivePullRequest)
  82. }, CheckInternalToken)
  83. }