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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "net/http"
  6. asymkey_model "code.gitea.io/gitea/models/asymkey"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/private"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. )
  11. // UpdatePublicKeyInRepo update public key and deploy key updates
  12. func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
  13. keyID := ctx.ParamsInt64(":id")
  14. repoID := ctx.ParamsInt64(":repoid")
  15. if err := asymkey_model.UpdatePublicKeyUpdated(keyID); err != nil {
  16. ctx.JSON(http.StatusInternalServerError, private.Response{
  17. Err: err.Error(),
  18. })
  19. return
  20. }
  21. deployKey, err := asymkey_model.GetDeployKeyByRepo(ctx, keyID, repoID)
  22. if err != nil {
  23. if asymkey_model.IsErrDeployKeyNotExist(err) {
  24. ctx.PlainText(http.StatusOK, "success")
  25. return
  26. }
  27. ctx.JSON(http.StatusInternalServerError, private.Response{
  28. Err: err.Error(),
  29. })
  30. return
  31. }
  32. deployKey.UpdatedUnix = timeutil.TimeStampNow()
  33. if err = asymkey_model.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
  34. ctx.JSON(http.StatusInternalServerError, private.Response{
  35. Err: err.Error(),
  36. })
  37. return
  38. }
  39. ctx.PlainText(http.StatusOK, "success")
  40. }
  41. // AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
  42. // and returns public key found.
  43. func AuthorizedPublicKeyByContent(ctx *context.PrivateContext) {
  44. content := ctx.FormString("content")
  45. publicKey, err := asymkey_model.SearchPublicKeyByContent(ctx, content)
  46. if err != nil {
  47. ctx.JSON(http.StatusInternalServerError, private.Response{
  48. Err: err.Error(),
  49. })
  50. return
  51. }
  52. ctx.PlainText(http.StatusOK, publicKey.AuthorizedString())
  53. }