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.

63 lines
1.8 KiB

  1. // Copyright 2018 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. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "gitea.com/macaron/macaron"
  11. )
  12. // UpdatePublicKeyInRepo update public key and deploy key updates
  13. func UpdatePublicKeyInRepo(ctx *macaron.Context) {
  14. keyID := ctx.ParamsInt64(":id")
  15. repoID := ctx.ParamsInt64(":repoid")
  16. if err := models.UpdatePublicKeyUpdated(keyID); err != nil {
  17. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  18. "err": err.Error(),
  19. })
  20. return
  21. }
  22. deployKey, err := models.GetDeployKeyByRepo(keyID, repoID)
  23. if err != nil {
  24. if models.IsErrDeployKeyNotExist(err) {
  25. ctx.PlainText(200, []byte("success"))
  26. return
  27. }
  28. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  29. "err": err.Error(),
  30. })
  31. return
  32. }
  33. deployKey.UpdatedUnix = timeutil.TimeStampNow()
  34. if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
  35. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  36. "err": err.Error(),
  37. })
  38. return
  39. }
  40. ctx.PlainText(http.StatusOK, []byte("success"))
  41. }
  42. // AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
  43. // and returns public key found.
  44. func AuthorizedPublicKeyByContent(ctx *macaron.Context) {
  45. content := ctx.Query("content")
  46. publicKey, err := models.SearchPublicKeyByContent(content)
  47. if err != nil {
  48. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  49. "err": err.Error(),
  50. })
  51. return
  52. }
  53. ctx.PlainText(http.StatusOK, []byte(publicKey.AuthorizedString()))
  54. }