diff --git a/_example/public_key/public_key.go b/_example/public_key/public_key.go index 08cdf68..215292e 100644 --- a/_example/public_key/public_key.go +++ b/_example/public_key/public_key.go @@ -1,33 +1,25 @@ package main import ( + "fmt" "io" "log" - b64 "encoding/base64" - "github.com/gliderlabs/ssh" + gossh "golang.org/x/crypto/ssh" ) func main() { - ssh.Handle(func(s ssh.Session) { - user := s.User() - keyType := s.PublicKey().Type() - - publicKeyString := keyType + " " + b64.StdEncoding.EncodeToString(s.PublicKey().Marshal()) - - io.WriteString(s, "Hello "+user+"\n\n") - io.WriteString(s, "your public key:\n") - io.WriteString(s, publicKeyString+"\n\n") + authorizedKey := gossh.MarshalAuthorizedKey(s.PublicKey()) + io.WriteString(s, fmt.Sprintf("public key used by %s:\n", s.User())) + s.Write(authorizedKey) }) - publicKeyHandler := ssh.PublicKeyAuth(func(user string, key ssh.PublicKey) bool { - // allow all keys - // use ssh.KeysEqual() to compare agains know keys - return true + publicKeyOption := ssh.PublicKeyAuth(func(user string, key ssh.PublicKey) bool { + return true // allow all keys, or use ssh.KeysEqual() to compare against known keys }) - log.Println("starting ssh server on port: 2222") - log.Fatal(ssh.ListenAndServe(":2222", nil, publicKeyHandler)) + log.Println("starting ssh server on port 2222...") + log.Fatal(ssh.ListenAndServe(":2222", nil, publicKeyOption)) } diff --git a/_example/simple/simple.go b/_example/simple/simple.go index d57c2e7..d2bcff1 100644 --- a/_example/simple/simple.go +++ b/_example/simple/simple.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "log" @@ -8,12 +9,10 @@ import ( ) func main() { - ssh.Handle(func(s ssh.Session) { - user := s.User() - io.WriteString(s, "Hello "+user+"\n") + io.WriteString(s, fmt.Sprintf("Hello %s\n", s.User())) }) - log.Println("starting ssh server on port: 2222") + log.Println("starting ssh server on port 2222...") log.Fatal(ssh.ListenAndServe(":2222", nil)) } diff --git a/server.go b/server.go index 0697b8d..9ea48ec 100644 --- a/server.go +++ b/server.go @@ -59,6 +59,8 @@ func (srv *Server) makeConfig() (*gossh.ServerConfig, error) { if ok := srv.PublicKeyHandler(conn.User(), key); !ok { return perms, fmt.Errorf("permission denied") } + // no other way to pass the key from + // auth handler to session handler perms.Extensions = map[string]string{ "_publickey": string(key.Marshal()), }