summaryrefslogtreecommitdiffstats
path: root/models/publickey.go
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-03-16 06:16:03 -0400
committerUnknown <joe2010xtmf@163.com>2014-03-16 06:16:03 -0400
commitca956d5cec34fd99d88494af3cafb214606fe27b (patch)
tree6a3ed4c6e86105e3cbf8878f21d272bdcb67ecc7 /models/publickey.go
parentc83657307e4ba5d414389c0e012ecf789053f763 (diff)
downloadgitea-ca956d5cec34fd99d88494af3cafb214606fe27b.tar.gz
gitea-ca956d5cec34fd99d88494af3cafb214606fe27b.zip
Add generate fingerprint of ssh key
Diffstat (limited to 'models/publickey.go')
-rw-r--r--models/publickey.go46
1 files changed, 33 insertions, 13 deletions
diff --git a/models/publickey.go b/models/publickey.go
index 6aed7c5741..bd0b7ee937 100644
--- a/models/publickey.go
+++ b/models/publickey.go
@@ -11,6 +11,7 @@ import (
"io"
"os"
"os/exec"
+ "path"
"path/filepath"
"strings"
"sync"
@@ -57,29 +58,48 @@ func init() {
}
type PublicKey struct {
- Id int64
- OwnerId int64 `xorm:"index"`
- Name string `xorm:"unique not null"`
- Content string `xorm:"text not null"`
- Created time.Time `xorm:"created"`
- Updated time.Time `xorm:"updated"`
+ Id int64
+ OwnerId int64 `xorm:"index"`
+ Name string `xorm:"unique not null"`
+ Fingerprint string
+ Content string `xorm:"text not null"`
+ Created time.Time `xorm:"created"`
+ Updated time.Time `xorm:"updated"`
}
func GenAuthorizedKey(keyId int64, key string) string {
return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
}
-func AddPublicKey(key *PublicKey) error {
- _, err := orm.Insert(key)
+func AddPublicKey(key *PublicKey) (err error) {
+ // Calculate fingerprint.
+ tmpPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
+ "id_rsa.pub")
+ os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
+ f, err := os.Create(tmpPath)
if err != nil {
+ return
+ }
+ if _, err = f.WriteString(key.Content); err != nil {
return err
}
-
- err = SaveAuthorizedKeyFile(key)
+ f.Close()
+ stdout, _, err := com.ExecCmd("ssh-keygen", "-l", "-f", tmpPath)
if err != nil {
- _, err2 := orm.Delete(key)
- if err2 != nil {
- // TODO: log the error
+ return err
+ } else if len(stdout) < 2 {
+ return errors.New("Not enough output for calculating fingerprint")
+ }
+ key.Fingerprint = strings.Split(stdout, " ")[1]
+
+ // Save SSH key.
+ if _, err = orm.Insert(key); err != nil {
+ return err
+ }
+
+ if err = SaveAuthorizedKeyFile(key); err != nil {
+ if _, err2 := orm.Delete(key); err2 != nil {
+ return err2
}
return err
}