forked from forgejo/forgejo
AuthorizedKeysCommand should not query db directly (#9371)
* AuthorizedKeysCommand should not query db directly * Update routers/private/internal.go * Fix import order
This commit is contained in:
parent
1707f59966
commit
d1a49977b0
6 changed files with 93 additions and 50 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue