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.

85 lines
2.4 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. "gitea.com/macaron/macaron"
  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.Post("/ssh/:id/update/:repoid", UpdatePublicKeyInRepo)
  69. m.Get("/hook/pre-receive/:owner/:repo", HookPreReceive)
  70. m.Get("/hook/post-receive/:owner/:repo", HookPostReceive)
  71. m.Get("/serv/none/:keyid", ServNoCommand)
  72. m.Get("/serv/command/:keyid/:owner/:repo", ServCommand)
  73. }, CheckInternalToken)
  74. }