forked from melbahja/goph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
80 lines (59 loc) · 1.53 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2020 Mohammed El Bahja. All rights reserved.
// Use of this source code is governed by a MIT license.
package goph
import (
"fmt"
"io/ioutil"
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
// Auth represents ssh auth methods.
type Auth []ssh.AuthMethod
// Password returns password auth method.
func Password(pass string) Auth {
return Auth{
ssh.Password(pass),
}
}
// Key returns auth method from private key with or without passphrase.
func Key(prvFile string, passphrase string) (Auth, error) {
signer, err := GetSigner(prvFile, passphrase)
if err != nil {
return nil, err
}
return Auth{
ssh.PublicKeys(signer),
}, nil
}
// HasAgent checks if ssh agent exists.
func HasAgent() bool {
return os.Getenv("SSH_AUTH_SOCK") != ""
}
// UseAgent auth via ssh agent, (Unix systems only)
func UseAgent() (Auth, error) {
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, fmt.Errorf("could not find ssh agent: %w", err)
}
return Auth{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
}, nil
}
// GetSigner returns ssh signer from private key file.
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
}