Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

internal.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // UpdatePublicKey update publick key updates
  21. func UpdatePublicKey(ctx *macaron.Context) {
  22. keyID := ctx.ParamsInt64(":id")
  23. if err := models.UpdatePublicKeyUpdated(keyID); err != nil {
  24. ctx.JSON(500, map[string]interface{}{
  25. "err": err.Error(),
  26. })
  27. return
  28. }
  29. ctx.PlainText(200, []byte("success"))
  30. }
  31. // RegisterRoutes registers all internal APIs routes to web application.
  32. // These APIs will be invoked by internal commands for example `gitea serv` and etc.
  33. func RegisterRoutes(m *macaron.Macaron) {
  34. m.Group("/", func() {
  35. m.Post("/ssh/:id/update", UpdatePublicKey)
  36. m.Post("/push/update", PushUpdate)
  37. m.Get("/protectedbranch/:pbid/:userid", CanUserPush)
  38. m.Get("/branch/:id/*", GetProtectedBranchBy)
  39. }, CheckInternalToken)
  40. }