summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-03-16 06:25:16 -0400
committerUnknown <joe2010xtmf@163.com>2014-03-16 06:25:16 -0400
commitfb960db6afa5fa84e60556f0c7d240b4af165a8d (patch)
tree879dfeca18f62975b580ab93fdfabbb093b058ff
parentca956d5cec34fd99d88494af3cafb214606fe27b (diff)
downloadgitea-fb960db6afa5fa84e60556f0c7d240b4af165a8d.tar.gz
gitea-fb960db6afa5fa84e60556f0c7d240b4af165a8d.zip
Add check if public key name has been used
-rw-r--r--models/publickey.go12
-rw-r--r--routers/user/setting.go4
2 files changed, 16 insertions, 0 deletions
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")
diff --git a/routers/user/setting.go b/routers/user/setting.go
index cd12bb6296..91e992b18e 100644
--- a/routers/user/setting.go
+++ b/routers/user/setting.go
@@ -128,6 +128,10 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
}
if err := models.AddPublicKey(k); err != nil {
+ if err.Error() == models.ErrKeyAlreadyExist.Error() {
+ ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
+ return
+ }
ctx.Handle(200, "ssh.AddPublicKey", err)
return
} else {