forked from melbahja/goph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
58 lines (40 loc) 路 996 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2020 Mohammed El Bahja. All rights reserved.
// Use of this source code is governed by a MIT license.
package goph
import (
"io/ioutil"
"golang.org/x/crypto/ssh"
)
type Auth []ssh.AuthMethod
// Get auth method from raw password.
func Password(pass string) Auth {
return Auth{
ssh.Password(pass),
}
}
// Get auth method from private key with or without passphrase.
func Key(prvFile string, passphrase string) Auth {
signer, err := GetSigner(prvFile, passphrase)
if err != nil {
panic(err)
}
return Auth{
ssh.PublicKeys(signer),
}
}
// Get private key signer.
func GetSigner(prvFile string, passphrase string) (ssh.Signer, error) {
var (
err error
signer ssh.Signer
)
privateKey, err := ioutil.ReadFile(prvFile)
if err != nil {
return nil, err
} else if passphrase != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(privateKey, []byte(passphrase))
} else {
signer, err = ssh.ParsePrivateKey(privateKey)
}
return signer, err
}