Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: example showing how to handle user identity #54

Merged
merged 2 commits into from Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,5 +1,6 @@
examples/*
!examples/simple
!examples/identity
!examples/bubbletea
examples/bubbletea/bubbletea
examples/bubbletea/.ssh
Expand All @@ -13,4 +14,4 @@ examples/git/.repos
coverage.txt

# MacOS specific
.DS_Store
.DS_Store
67 changes: 67 additions & 0 deletions examples/identity/main.go
@@ -0,0 +1,67 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/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(
logging.Middleware(),
func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
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!")
}
h(s)
}
},
),
)
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)
}
}