From 9979564c357fac6bd1329c662bccf1853be9691c Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 13 Jun 2022 15:12:50 -0300 Subject: [PATCH 1/2] docs: example showing how to handle user identity An simple example showing how to handle user identity. Signed-off-by: Carlos A Becker --- .gitignore | 3 +- examples/identity/main.go | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/identity/main.go diff --git a/.gitignore b/.gitignore index cbac575..c12c120 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ examples/* !examples/simple +!examples/identity !examples/bubbletea examples/bubbletea/bubbletea examples/bubbletea/.ssh @@ -13,4 +14,4 @@ examples/git/.repos coverage.txt # MacOS specific -.DS_Store \ No newline at end of file +.DS_Store diff --git a/examples/identity/main.go b/examples/identity/main.go new file mode 100644 index 0000000..3ae2739 --- /dev/null +++ b/examples/identity/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + "os/signal" + "strings" + "syscall" + "time" + + "github.com/charmbracelet/wish" + "github.com/charmbracelet/wish/logging" + "github.com/gliderlabs/ssh" + gossh "golang.org/x/crypto/ssh" +) + +const ( + host = "localhost" + port = 23234 +) + +func main() { + s, err := wish.NewServer( + wish.WithAddress(fmt.Sprintf("%s:%d", host, port)), + wish.WithHostKeyPath(".ssh/term_info_ed25519"), + wish.WithPublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool { + return true + }), + wish.WithMiddleware( + func(h ssh.Handler) ssh.Handler { + return func(s ssh.Session) { + switch strings.TrimSpace(string(gossh.MarshalAuthorizedKey(s.PublicKey()))) { + case "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILxWe2rXKoiO6W14LYPVfJKzRfJ1f3Jhzxrgjc/D4tU7": + wish.Println(s, "Hey Carlos!") + default: + wish.Println(s, "Hey, I don't know who you are!") + } + h(s) + } + }, + logging.Middleware(), + ), + ) + if err != nil { + log.Fatalln(err) + } + + done := make(chan os.Signal, 1) + signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + log.Printf("Starting SSH server on %s:%d", host, port) + go func() { + if err = s.ListenAndServe(); err != nil { + log.Fatalln(err) + } + }() + + <-done + log.Println("Stopping SSH server") + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer func() { cancel() }() + if err := s.Shutdown(ctx); err != nil { + log.Fatalln(err) + } +} From 8227f27c2299d481e7d91d35fb422e6b919d884b Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 13 Jun 2022 15:38:34 -0400 Subject: [PATCH 2/2] fix: use ssh.KeysEqual --- examples/identity/main.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/identity/main.go b/examples/identity/main.go index 3ae2739..13c9f17 100644 --- a/examples/identity/main.go +++ b/examples/identity/main.go @@ -6,14 +6,12 @@ import ( "log" "os" "os/signal" - "strings" "syscall" "time" "github.com/charmbracelet/wish" "github.com/charmbracelet/wish/logging" "github.com/gliderlabs/ssh" - gossh "golang.org/x/crypto/ssh" ) const ( @@ -29,10 +27,14 @@ func main() { return true }), wish.WithMiddleware( + logging.Middleware(), func(h ssh.Handler) ssh.Handler { return func(s ssh.Session) { - switch strings.TrimSpace(string(gossh.MarshalAuthorizedKey(s.PublicKey()))) { - case "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILxWe2rXKoiO6W14LYPVfJKzRfJ1f3Jhzxrgjc/D4tU7": + carlos, _, _, _, _ := ssh.ParseAuthorizedKey( + []byte("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILxWe2rXKoiO6W14LYPVfJKzRfJ1f3Jhzxrgjc/D4tU7"), + ) + switch { + case ssh.KeysEqual(s.PublicKey(), carlos): wish.Println(s, "Hey Carlos!") default: wish.Println(s, "Hey, I don't know who you are!") @@ -40,7 +42,6 @@ func main() { h(s) } }, - logging.Middleware(), ), ) if err != nil {