MCPcopy Create free account
hub / github.com/gogs/gogs / CheckPublicKeyString

Function CheckPublicKeyString

internal/database/ssh_key.go:273–318  ·  view source on GitHub ↗

CheckPublicKeyString checks if the given public key string is recognized by SSH. It returns the actual public key line on success.

(content string)

Source from the content-addressed store, hash-verified

271// CheckPublicKeyString checks if the given public key string is recognized by SSH.
272// It returns the actual public key line on success.
273func CheckPublicKeyString(content string) (_ string, err error) {
274 if conf.SSH.Disabled {
275 return "", errors.New("SSH is disabled")
276 }
277
278 content, err = parseKeyString(content)
279 if err != nil {
280 return "", err
281 }
282
283 content = strings.TrimRight(content, "\n\r")
284 if strings.ContainsAny(content, "\n\r") {
285 return "", errors.New("only a single line with a single key please")
286 }
287
288 // Remove any unnecessary whitespace
289 content = strings.TrimSpace(content)
290
291 if !conf.SSH.MinimumKeySizeCheck {
292 return content, nil
293 }
294
295 var (
296 fnName string
297 keyType string
298 length int
299 )
300 if conf.SSH.StartBuiltinServer {
301 fnName = "SSHNativeParsePublicKey"
302 keyType, length, err = SSHNativeParsePublicKey(content)
303 } else {
304 fnName = "SSHKeygenParsePublicKey"
305 keyType, length, err = SSHKeygenParsePublicKey(content, conf.SSH.KeyTestPath, conf.SSH.KeygenPath)
306 }
307 if err != nil {
308 return "", errors.Newf("%s: %v", fnName, err)
309 }
310 log.Trace("Key info [native: %v]: %s-%d", conf.SSH.StartBuiltinServer, keyType, length)
311
312 if minLen, found := conf.SSH.MinimumKeySizes[keyType]; found && length >= minLen {
313 return content, nil
314 } else if found && length < minLen {
315 return "", errors.Newf("key length is not enough: got %d, needs %d", length, minLen)
316 }
317 return "", errors.Newf("key type is not allowed: %s", keyType)
318}
319
320// appendAuthorizedKeysToFile appends new SSH keys' content to authorized_keys file.
321func appendAuthorizedKeysToFile(keys ...*PublicKey) error {

Callers 4

SettingsSSHKeysPostFunction · 0.92
SettingsDeployKeysPostFunction · 0.92
CreateUserPublicKeyFunction · 0.92
CreateDeployKeyFunction · 0.92

Calls 3

parseKeyStringFunction · 0.85
SSHNativeParsePublicKeyFunction · 0.85
SSHKeygenParsePublicKeyFunction · 0.85

Tested by

no test coverage detected