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.

key.go 1.9KB

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