From d1a49977b089afefc40172711d02eb795d2234de Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 17 Dec 2019 01:49:07 +0000 Subject: AuthorizedKeysCommand should not query db directly (#9371) * AuthorizedKeysCommand should not query db directly * Update routers/private/internal.go * Fix import order --- modules/private/key.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'modules/private') diff --git a/modules/private/key.go b/modules/private/key.go index ebc28eb871..40e1c492f8 100644 --- a/modules/private/key.go +++ b/modules/private/key.go @@ -6,6 +6,8 @@ package private import ( "fmt" + "io/ioutil" + "net/http" "code.gitea.io/gitea/modules/setting" ) @@ -27,3 +29,26 @@ func UpdatePublicKeyInRepo(keyID, repoID int64) error { } return nil } + +// AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part) +// and returns public key found. +func AuthorizedPublicKeyByContent(content string) (string, error) { + // Ask for running deliver hook and test pull request tasks. + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/authorized_keys") + req := newInternalRequest(reqURL, "POST") + req.Param("content", content) + resp, err := req.Response() + if err != nil { + return "", err + } + + defer resp.Body.Close() + + // All 2XX status codes are accepted and others will return an error + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err) + } + bs, err := ioutil.ReadAll(resp.Body) + + return string(bs), err +} -- cgit v1.2.3