MCPcopy Index your code
hub / github.com/gogs/gogs / addKey

Function addKey

internal/database/ssh_key.go:371–397  ·  view source on GitHub ↗
(e Engine, key *PublicKey)

Source from the content-addressed store, hash-verified

369}
370
371func addKey(e Engine, key *PublicKey) (err error) {
372 // Calculate fingerprint.
373 tmpPath := strings.ReplaceAll(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/")
374 _ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
375 if err = os.WriteFile(tmpPath, []byte(key.Content), 0o644); err != nil {
376 return err
377 }
378
379 stdout, stderr, err := process.Exec("AddPublicKey", conf.SSH.KeygenPath, "-lf", tmpPath)
380 if err != nil {
381 return errors.Newf("fail to parse public key: %s - %s", err, stderr)
382 } else if len(stdout) < 2 {
383 return errors.New("not enough output for calculating fingerprint: " + stdout)
384 }
385 key.Fingerprint = strings.Split(stdout, " ")[1]
386
387 // Save SSH key.
388 if _, err = e.Insert(key); err != nil {
389 return err
390 }
391
392 // Don't need to rewrite this file if builtin SSH server is enabled.
393 if conf.SSH.StartBuiltinServer {
394 return nil
395 }
396 return appendAuthorizedKeysToFile(key)
397}
398
399// AddPublicKey adds new public key to database and authorized_keys file.
400func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) {

Callers 2

AddPublicKeyFunction · 0.85
AddDeployKeyFunction · 0.85

Calls 5

ExecFunction · 0.92
ReplaceAllMethod · 0.80
SplitMethod · 0.80
InsertMethod · 0.80

Tested by

no test coverage detected