gitea源码

key.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/private"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "code.gitea.io/gitea/services/context"
  10. )
  11. // UpdatePublicKeyInRepo update public key and deploy key updates
  12. func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
  13. keyID := ctx.PathParamInt64("id")
  14. repoID := ctx.PathParamInt64("repoid")
  15. if err := asymkey_model.UpdatePublicKeyUpdated(ctx, 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(ctx, 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. }