From fb960db6afa5fa84e60556f0c7d240b4af165a8d Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 16 Mar 2014 06:25:16 -0400 Subject: Add check if public key name has been used --- models/publickey.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'models/publickey.go') diff --git a/models/publickey.go b/models/publickey.go index bd0b7ee937..ee6bd53101 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -67,11 +67,23 @@ type PublicKey struct { Updated time.Time `xorm:"updated"` } +var ( + ErrKeyAlreadyExist = errors.New("Public key already exist") +) + func GenAuthorizedKey(keyId int64, key string) string { return fmt.Sprintf(tmplPublicKey, appPath, keyId, key) } func AddPublicKey(key *PublicKey) (err error) { + // Check if public key name has been used. + has, err := orm.Get(key) + if err != nil { + return err + } else if has { + return ErrKeyAlreadyExist + } + // Calculate fingerprint. tmpPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub") -- cgit v1.2.3