]> source.dussan.org Git - gitea.git/commitdiff
Add generate fingerprint of ssh key
authorUnknown <joe2010xtmf@163.com>
Sun, 16 Mar 2014 10:16:03 +0000 (06:16 -0400)
committerUnknown <joe2010xtmf@163.com>
Sun, 16 Mar 2014 10:16:03 +0000 (06:16 -0400)
models/publickey.go
templates/user/publickey.tmpl

index 6aed7c57418b900873b0e03952d2ee596ce189d2..bd0b7ee937a1a930836fb87231ba0ed199860c73 100644 (file)
@@ -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
        }
index 60d2c2464a65d7c1ed2e90803251e1fcb10087e9..b671f63fdbd74e7b853eecafcae6c9b61b724620 100644 (file)
@@ -21,7 +21,7 @@
                 <li class="list-group-item"><span class="name">SSH Key's name</span></li>{{range .Keys}}
                 <li class="list-group-item">
                     <span class="name">{{.Name}}</span>
-                    <span class="print">(print code)</span>
+                    <span class="print">({{.Fingerprint}})</span>
                     <a href="#" class="btn btn-link btn-danger right delete" rel="{{.Id}}" data-del="{{.Id}}">Delete</a>
                 </li>{{end}}
                 <li class="list-group-item">