fix: improve private key parsing by handling escaped newlines and whitespace in credentials #3
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/sftp"
|
"github.com/pkg/sftp"
|
||||||
@@ -32,8 +33,16 @@ func (e *SFTPExplorer) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var authMethod ssh.AuthMethod
|
var authMethod ssh.AuthMethod
|
||||||
signer, err := ssh.ParsePrivateKey([]byte(e.secret))
|
|
||||||
if err == nil {
|
secretStr := strings.TrimSpace(e.secret)
|
||||||
|
// Handle case where newlines might be escaped by mistake
|
||||||
|
secretStr = strings.ReplaceAll(secretStr, "\\n", "\n")
|
||||||
|
|
||||||
|
if strings.Contains(secretStr, "-----BEGIN") {
|
||||||
|
signer, err := ssh.ParsePrivateKey([]byte(secretStr))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse private key: %w", err)
|
||||||
|
}
|
||||||
authMethod = ssh.PublicKeys(signer)
|
authMethod = ssh.PublicKeys(signer)
|
||||||
} else {
|
} else {
|
||||||
authMethod = ssh.Password(e.secret)
|
authMethod = ssh.Password(e.secret)
|
||||||
|
|||||||
Reference in New Issue
Block a user