mirror of
https://github.com/moul/sshportal.git
synced 2025-03-01 01:45:21 +08:00
Handle auth by key
This commit is contained in:
parent
729060f397
commit
35e62e562b
3 changed files with 9 additions and 5 deletions
|
@ -29,7 +29,7 @@ func NewSSHKey(keyType string, length uint) (*SSHKey, error) {
|
||||||
|
|
||||||
// convert priv key to x509 format
|
// convert priv key to x509 format
|
||||||
var pemKey = &pem.Block{
|
var pemKey = &pem.Block{
|
||||||
Type: "PRIVATE KEY",
|
Type: "RSA PRIVATE KEY",
|
||||||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||||
}
|
}
|
||||||
buf := bytes.NewBufferString("")
|
buf := bytes.NewBufferString("")
|
||||||
|
|
2
db.go
2
db.go
|
@ -95,7 +95,7 @@ func dbDemo(db *gorm.DB) error {
|
||||||
|
|
||||||
func RemoteHostFromSession(s ssh.Session, db *gorm.DB) (*Host, error) {
|
func RemoteHostFromSession(s ssh.Session, db *gorm.DB) (*Host, error) {
|
||||||
var host Host
|
var host Host
|
||||||
db.Where("name = ?", s.User()).Find(&host)
|
db.Preload("SSHKey").Where("name = ?", s.User()).Find(&host)
|
||||||
if host.Name == "" {
|
if host.Name == "" {
|
||||||
// FIXME: add available hosts
|
// FIXME: add available hosts
|
||||||
return nil, fmt.Errorf("No such target: %q", s.User())
|
return nil, fmt.Errorf("No such target: %q", s.User())
|
||||||
|
|
10
proxy.go
10
proxy.go
|
@ -82,12 +82,16 @@ func (host *Host) ClientConfig(_ ssh.Session) (*gossh.ClientConfig, error) {
|
||||||
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
||||||
Auth: []gossh.AuthMethod{},
|
Auth: []gossh.AuthMethod{},
|
||||||
}
|
}
|
||||||
|
if host.SSHKey != nil {
|
||||||
|
signer, err := gossh.ParsePrivateKey([]byte(host.SSHKey.PrivKey))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Auth = append(config.Auth, gossh.PublicKeys(signer))
|
||||||
|
}
|
||||||
if host.Password != "" {
|
if host.Password != "" {
|
||||||
config.Auth = append(config.Auth, gossh.Password(host.Password))
|
config.Auth = append(config.Auth, gossh.Password(host.Password))
|
||||||
}
|
}
|
||||||
if host.SSHKey != nil {
|
|
||||||
return nil, fmt.Errorf("auth by priv key is not yet implemented")
|
|
||||||
}
|
|
||||||
if len(config.Auth) == 0 {
|
if len(config.Auth) == 0 {
|
||||||
return nil, fmt.Errorf("no valid authentication method for host %q", host.Name)
|
return nil, fmt.Errorf("no valid authentication method for host %q", host.Name)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue