|
| 1 | +package ssh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "golang.org/x/crypto/ssh" |
| 7 | + "gopkg.in/src-d/go-git.v2/clients/common" |
| 8 | +) |
| 9 | + |
| 10 | +// AuthMethod is the interface all auth methods for the ssh client |
| 11 | +// must implement. The clientConfig method returns the ssh client |
| 12 | +// configuration needed to establish an ssh connection. |
| 13 | +type AuthMethod interface { |
| 14 | + common.AuthMethod |
| 15 | + clientConfig() *ssh.ClientConfig |
| 16 | +} |
| 17 | + |
| 18 | +// The names of the AuthMethod implementations. To be returned by the |
| 19 | +// Name() method. Most git servers only allow PublicKeysName and |
| 20 | +// PublicKeysCallbackName. |
| 21 | +const ( |
| 22 | + KeyboardInteractiveName = "ssh-keyboard-interactive" |
| 23 | + PasswordName = "ssh-password" |
| 24 | + PasswordCallbackName = "ssh-password-callback" |
| 25 | + PublicKeysName = "ssh-public-keys" |
| 26 | + PublicKeysCallbackName = "ssh-public-key-callback" |
| 27 | +) |
| 28 | + |
| 29 | +// KeyboardInteractive implements AuthMethod by using a |
| 30 | +// prompt/response sequence controlled by the server. |
| 31 | +type KeyboardInteractive struct { |
| 32 | + User string |
| 33 | + Challenge ssh.KeyboardInteractiveChallenge |
| 34 | +} |
| 35 | + |
| 36 | +func (a *KeyboardInteractive) Name() string { |
| 37 | + return KeyboardInteractiveName |
| 38 | +} |
| 39 | + |
| 40 | +func (a *KeyboardInteractive) String() string { |
| 41 | + return fmt.Sprintf("user: %s, name: %s", a.User, a.Name()) |
| 42 | +} |
| 43 | + |
| 44 | +func (a *KeyboardInteractive) clientConfig() *ssh.ClientConfig { |
| 45 | + return &ssh.ClientConfig{ |
| 46 | + User: a.User, |
| 47 | + Auth: []ssh.AuthMethod{ssh.KeyboardInteractiveChallenge(a.Challenge)}, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Password implements AuthMethod by using the given password. |
| 52 | +type Password struct { |
| 53 | + User string |
| 54 | + Pass string |
| 55 | +} |
| 56 | + |
| 57 | +func (a *Password) Name() string { |
| 58 | + return PasswordName |
| 59 | +} |
| 60 | + |
| 61 | +func (a *Password) String() string { |
| 62 | + return fmt.Sprintf("user: %s, name: %s", a.User, a.Name()) |
| 63 | +} |
| 64 | + |
| 65 | +func (a *Password) clientConfig() *ssh.ClientConfig { |
| 66 | + return &ssh.ClientConfig{ |
| 67 | + User: a.User, |
| 68 | + Auth: []ssh.AuthMethod{ssh.Password(a.Pass)}, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// PasswordCallback implements AuthMethod by using a callback |
| 73 | +// to fetch the password. |
| 74 | +type PasswordCallback struct { |
| 75 | + User string |
| 76 | + Callback func() (pass string, err error) |
| 77 | +} |
| 78 | + |
| 79 | +func (a *PasswordCallback) Name() string { |
| 80 | + return PasswordCallbackName |
| 81 | +} |
| 82 | + |
| 83 | +func (a *PasswordCallback) String() string { |
| 84 | + return fmt.Sprintf("user: %s, name: %s", a.User, a.Name()) |
| 85 | +} |
| 86 | + |
| 87 | +func (a *PasswordCallback) clientConfig() *ssh.ClientConfig { |
| 88 | + return &ssh.ClientConfig{ |
| 89 | + User: a.User, |
| 90 | + Auth: []ssh.AuthMethod{ssh.PasswordCallback(a.Callback)}, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// PublicKeys implements AuthMethod by using the given |
| 95 | +// key pairs. |
| 96 | +type PublicKeys struct { |
| 97 | + User string |
| 98 | + Signer ssh.Signer |
| 99 | +} |
| 100 | + |
| 101 | +func (a *PublicKeys) Name() string { |
| 102 | + return PublicKeysName |
| 103 | +} |
| 104 | + |
| 105 | +func (a *PublicKeys) String() string { |
| 106 | + return fmt.Sprintf("user: %s, name: %s", a.User, a.Name()) |
| 107 | +} |
| 108 | + |
| 109 | +func (a *PublicKeys) clientConfig() *ssh.ClientConfig { |
| 110 | + return &ssh.ClientConfig{ |
| 111 | + User: a.User, |
| 112 | + Auth: []ssh.AuthMethod{ssh.PublicKeys(a.Signer)}, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// PublicKeysCallback implements AuthMethod by asking a |
| 117 | +// ssh.agent.Agent to act as a signer. |
| 118 | +type PublicKeysCallback struct { |
| 119 | + User string |
| 120 | + Callback func() (signers []ssh.Signer, err error) |
| 121 | +} |
| 122 | + |
| 123 | +func (a *PublicKeysCallback) Name() string { |
| 124 | + return PublicKeysCallbackName |
| 125 | +} |
| 126 | + |
| 127 | +func (a *PublicKeysCallback) String() string { |
| 128 | + return fmt.Sprintf("user: %s, name: %s", a.User, a.Name()) |
| 129 | +} |
| 130 | + |
| 131 | +func (a *PublicKeysCallback) clientConfig() *ssh.ClientConfig { |
| 132 | + return &ssh.ClientConfig{ |
| 133 | + User: a.User, |
| 134 | + Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(a.Callback)}, |
| 135 | + } |
| 136 | +} |
0 commit comments