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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // UpdatePublicKeyInRepo update public key and if necessary deploy key updates
  12. func UpdatePublicKeyInRepo(ctx context.Context, keyID, repoID int64) error {
  13. // Ask for running deliver hook and test pull request tasks.
  14. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update/%d", keyID, repoID)
  15. resp, err := newInternalRequest(ctx, reqURL, "POST").Response()
  16. if err != nil {
  17. return err
  18. }
  19. defer resp.Body.Close()
  20. // All 2XX status codes are accepted and others will return an error
  21. if resp.StatusCode/100 != 2 {
  22. return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
  23. }
  24. return nil
  25. }
  26. // AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
  27. // and returns public key found.
  28. func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string, error) {
  29. // Ask for running deliver hook and test pull request tasks.
  30. reqURL := setting.LocalURL + "api/internal/ssh/authorized_keys"
  31. req := newInternalRequest(ctx, reqURL, "POST")
  32. req.Param("content", content)
  33. resp, err := req.Response()
  34. if err != nil {
  35. return "", err
  36. }
  37. defer resp.Body.Close()
  38. // All 2XX status codes are accepted and others will return an error
  39. if resp.StatusCode != http.StatusOK {
  40. return "", fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
  41. }
  42. bs, err := io.ReadAll(resp.Body)
  43. return string(bs), err
  44. }