Browse Source

Add generate fingerprint of ssh key

pull/31/head
Unknown 11 years ago
parent
commit
ca956d5cec
  1. 34
      models/publickey.go
  2. 2
      templates/user/publickey.tmpl

34
models/publickey.go

@ -11,6 +11,7 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -60,6 +61,7 @@ type PublicKey struct {
Id int64 Id int64
OwnerId int64 `xorm:"index"` OwnerId int64 `xorm:"index"`
Name string `xorm:"unique not null"` Name string `xorm:"unique not null"`
Fingerprint string
Content string `xorm:"text not null"` Content string `xorm:"text not null"`
Created time.Time `xorm:"created"` Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"` Updated time.Time `xorm:"updated"`
@ -69,17 +71,35 @@ func GenAuthorizedKey(keyId int64, key string) string {
return fmt.Sprintf(tmplPublicKey, appPath, keyId, key) return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
} }
func AddPublicKey(key *PublicKey) error { func AddPublicKey(key *PublicKey) (err error) {
_, err := orm.Insert(key) // 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 { if err != nil {
return
}
if _, err = f.WriteString(key.Content); err != nil {
return err return err
} }
f.Close()
err = SaveAuthorizedKeyFile(key) stdout, _, err := com.ExecCmd("ssh-keygen", "-l", "-f", tmpPath)
if err != nil { if err != nil {
_, err2 := orm.Delete(key) return err
if err2 != nil { } else if len(stdout) < 2 {
// TODO: log the error 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 return err
} }

2
templates/user/publickey.tmpl

@ -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">SSH Key's name</span></li>{{range .Keys}}
<li class="list-group-item"> <li class="list-group-item">
<span class="name">{{.Name}}</span> <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> <a href="#" class="btn btn-link btn-danger right delete" rel="{{.Id}}" data-del="{{.Id}}">Delete</a>
</li>{{end}} </li>{{end}}
<li class="list-group-item"> <li class="list-group-item">

Loading…
Cancel
Save