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.

47 lines
1.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. 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("/branch/:id/*", GetProtectedBranchBy)
  38. }, CheckInternalToken)
  39. }