Skip to content

Commit f3dbb79

Browse files
committed
add support for reading public SSH keys from the user's .ssh directory
1 parent f984a5e commit f3dbb79

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

cmd/up.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@ func NewCmdUp() *cobra.Command {
139139
if flags.sshAddr != "" {
140140
server := ssh.Server{
141141
PublicKeyHandler: func(ctx ssh.Context, key ssh.PublicKey) bool {
142+
sshDir := filepath.Join(os.Getenv("HOME"), ".ssh")
143+
entries, err := os.ReadDir(sshDir)
144+
if err != nil && !errors.Is(err, os.ErrNotExist) {
145+
fmt.Fprintf(os.Stderr, "failed to read ssh directory: %v\n", err)
146+
return false
147+
}
148+
149+
for _, entry := range entries {
150+
if filepath.Ext(entry.Name()) != ".pub" {
151+
continue
152+
}
153+
154+
pubKeyBytes, err := os.ReadFile(filepath.Join(sshDir, entry.Name()))
155+
if err != nil {
156+
continue
157+
}
158+
159+
pubKey, _, _, _, err := gossh.ParseAuthorizedKey(pubKeyBytes)
160+
if err != nil {
161+
continue
162+
}
163+
164+
if ssh.KeysEqual(pubKey, key) {
165+
return true
166+
}
167+
}
168+
142169
for _, authorizedKeysPath := range []string{
143170
filepath.Join(os.Getenv("HOME"), ".ssh", "authorized_keys"),
144171
filepath.Join(k.String("dir"), ".smallweb", "authorized_keys"),

0 commit comments

Comments
 (0)