summaryrefslogtreecommitdiffstats
path: root/models/publickey.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-02-25 16:13:47 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-02-25 16:13:47 +0800
commitd42c194aadd349ebb2e87d64d764a370fb3f54dc (patch)
treeeabde3d7ea2a4f82cd737140ef2b59b30b086b37 /models/publickey.go
parentbbf53450046f067842bb3731fad46db14333ff4c (diff)
downloadgitea-d42c194aadd349ebb2e87d64d764a370fb3f54dc.tar.gz
gitea-d42c194aadd349ebb2e87d64d764a370fb3f54dc.zip
add publickey
Diffstat (limited to 'models/publickey.go')
-rw-r--r--models/publickey.go38
1 files changed, 30 insertions, 8 deletions
diff --git a/models/publickey.go b/models/publickey.go
index bc9fbb33bd..1e58ba0574 100644
--- a/models/publickey.go
+++ b/models/publickey.go
@@ -3,17 +3,37 @@ package models
import (
"fmt"
"os"
+ "os/exec"
"path/filepath"
"time"
)
var (
publicKeyRootPath string
+ sshPath string = "/Users/lunny/.ssh"
+ appPath string
tmplPublicKey = "### autogenerated by gitgos, DO NOT EDIT\n" +
- "command=\"gitosis-serve %s\",no-port-forwarding," +
- "no-X11-forwarding,no-agent-forwarding,no-pty %s"
+ "command=\"%s serv key-%d\",no-port-forwarding," +
+ "no-X11-forwarding,no-agent-forwarding,no-pty %s\n"
)
+func exePath() (string, error) {
+ file, err := exec.LookPath(os.Args[0])
+ if err != nil {
+ return "", err
+ }
+ return filepath.Abs(file)
+}
+
+func init() {
+ var err error
+ appPath, err = exePath()
+ if err != nil {
+ println(err.Error())
+ os.Exit(2)
+ }
+}
+
type PublicKey struct {
Id int64
OwnerId int64 `xorm:"index"`
@@ -23,8 +43,8 @@ type PublicKey struct {
Updated time.Time `xorm:"updated"`
}
-func GenAuthorizedKey(user, key string) string {
- return fmt.Sprintf(tmplPublicKey, user, key)
+func GenAuthorizedKey(keyId int64, key string) string {
+ return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
}
func AddPublicKey(key *PublicKey, user string) error {
@@ -33,7 +53,7 @@ func AddPublicKey(key *PublicKey, user string) error {
return err
}
- err = SaveAuthorizedKeyFile(user, key.Content)
+ err = SaveAuthorizedKeyFile(key)
if err != nil {
_, err2 := orm.Delete(key)
if err2 != nil {
@@ -45,11 +65,13 @@ func AddPublicKey(key *PublicKey, user string) error {
return nil
}
-func SaveAuthorizedKeyFile(user, key string) error {
- f, err := os.Create(filepath.Join(publicKeyRootPath, user+".pub"))
+func SaveAuthorizedKeyFile(key *PublicKey) error {
+ p := filepath.Join(sshPath, "authorized_keys")
+ f, err := os.Create(p)
if err != nil {
return err
}
- _, err = f.WriteString(GenAuthorizedKey(user, key))
+ os.Chmod(p, 0600)
+ _, err = f.WriteString(GenAuthorizedKey(key.Id, key.Content))
return err
}