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