summaryrefslogtreecommitdiffstats
path: root/models/publickey.go
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-03-17 14:03:58 -0400
committerUnknown <joe2010xtmf@163.com>2014-03-17 14:03:58 -0400
commite51afe4621cfbc8380994c818981f42a097e3ec5 (patch)
tree74e26a6c8fa60ab82f9f1e791693cd178adc5a10 /models/publickey.go
parent9d3b003add6bee6cb23cbac5d32f6fb3d4fd50cb (diff)
downloadgitea-e51afe4621cfbc8380994c818981f42a097e3ec5.tar.gz
gitea-e51afe4621cfbc8380994c818981f42a097e3ec5.zip
Add postgres support, clean code, code review
Diffstat (limited to 'models/publickey.go')
-rw-r--r--models/publickey.go40
1 files changed, 25 insertions, 15 deletions
diff --git a/models/publickey.go b/models/publickey.go
index ee6bd53101..021d690fb6 100644
--- a/models/publickey.go
+++ b/models/publickey.go
@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
+ "io/ioutil"
"os"
"os/exec"
"path"
@@ -20,16 +21,19 @@ import (
"github.com/Unknwon/com"
)
+const (
+ // "### autogenerated by gitgos, DO NOT EDIT\n"
+ TPL_PUBLICK_KEY = `command="%s serv key-%d",no-port-forwarding, no-X11-forwarding,no-agent-forwarding,no-pty %s`
+)
+
var (
sshOpLocker = sync.Mutex{}
- //publicKeyRootPath string
+
sshPath string
appPath string
- // "### autogenerated by gitgos, DO NOT EDIT\n"
- tmplPublicKey = "command=\"%s serv key-%d\",no-port-forwarding," +
- "no-X11-forwarding,no-agent-forwarding,no-pty %s\n"
)
+// exePath returns the executable path.
func exePath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
@@ -38,6 +42,7 @@ func exePath() (string, error) {
return filepath.Abs(file)
}
+// homeDir returns the home directory of current user.
func homeDir() string {
home, err := com.HomeDir()
if err != nil {
@@ -48,15 +53,22 @@ func homeDir() string {
func init() {
var err error
+
appPath, err = exePath()
if err != nil {
- println(err.Error())
+ fmt.Printf("publickey.init(fail to get app path): %v\n", err)
os.Exit(2)
}
+ // Determine and create .ssh path.
sshPath = filepath.Join(homeDir(), ".ssh")
+ if err = os.MkdirAll(sshPath, os.ModePerm); err != nil {
+ fmt.Printf("publickey.init(fail to create sshPath(%s)): %v\n", sshPath, err)
+ os.Exit(2)
+ }
}
+// PublicKey represents a SSH key of user.
type PublicKey struct {
Id int64
OwnerId int64 `xorm:"index"`
@@ -71,10 +83,12 @@ var (
ErrKeyAlreadyExist = errors.New("Public key already exist")
)
+// GenAuthorizedKey returns formatted public key string.
func GenAuthorizedKey(keyId int64, key string) string {
- return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
+ return fmt.Sprintf(TPL_PUBLICK_KEY+"\n", appPath, keyId, key)
}
+// AddPublicKey adds new public key to database and SSH key file.
func AddPublicKey(key *PublicKey) (err error) {
// Check if public key name has been used.
has, err := orm.Get(key)
@@ -88,14 +102,9 @@ func AddPublicKey(key *PublicKey) (err error) {
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 {
+ if err = ioutil.WriteFile(tmpPath, []byte(key.Content), os.ModePerm); err != nil {
return err
}
- f.Close()
stdout, _, err := com.ExecCmd("ssh-keygen", "-l", "-f", tmpPath)
if err != nil {
return err
@@ -108,7 +117,6 @@ func AddPublicKey(key *PublicKey) (err error) {
if _, err = orm.Insert(key); err != nil {
return err
}
-
if err = SaveAuthorizedKeyFile(key); err != nil {
if _, err2 := orm.Delete(key); err2 != nil {
return err2
@@ -121,6 +129,7 @@ func AddPublicKey(key *PublicKey) (err error) {
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
func DeletePublicKey(key *PublicKey) (err error) {
+ // Delete SSH key in database.
has, err := orm.Id(key.Id).Get(key)
if err != nil {
return err
@@ -131,6 +140,7 @@ func DeletePublicKey(key *PublicKey) (err error) {
return err
}
+ // Delete SSH key in SSH key file.
sshOpLocker.Lock()
defer sshOpLocker.Unlock()
@@ -182,16 +192,17 @@ func DeletePublicKey(key *PublicKey) (err error) {
if err = os.Remove(p); err != nil {
return err
}
-
return os.Rename(tmpP, p)
}
+// ListPublicKey returns a list of public keys that user has.
func ListPublicKey(userId int64) ([]PublicKey, error) {
keys := make([]PublicKey, 0)
err := orm.Find(&keys, &PublicKey{OwnerId: userId})
return keys, err
}
+// SaveAuthorizedKeyFile writes SSH key content to SSH key file.
func SaveAuthorizedKeyFile(key *PublicKey) error {
sshOpLocker.Lock()
defer sshOpLocker.Unlock()
@@ -203,7 +214,6 @@ func SaveAuthorizedKeyFile(key *PublicKey) error {
}
defer f.Close()
- //os.Chmod(p, 0600)
_, err = f.WriteString(GenAuthorizedKey(key.Id, key.Content))
return err
}