Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go

go:
- 1.3
- 1.4
- 1.5

Expand Down
136 changes: 136 additions & 0 deletions clients/ssh/auth_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package ssh

import (
"fmt"

"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v2/clients/common"
)

// AuthMethod is the interface all auth methods for the ssh client
// must implement. The clientConfig method returns the ssh client
// configuration needed to establish an ssh connection.
type AuthMethod interface {
common.AuthMethod
clientConfig() *ssh.ClientConfig
}

// The names of the AuthMethod implementations. To be returned by the
// Name() method. Most git servers only allow PublicKeysName and
// PublicKeysCallbackName.
const (
KeyboardInteractiveName = "ssh-keyboard-interactive"
PasswordName = "ssh-password"
PasswordCallbackName = "ssh-password-callback"
PublicKeysName = "ssh-public-keys"
PublicKeysCallbackName = "ssh-public-key-callback"
)

// KeyboardInteractive implements AuthMethod by using a
// prompt/response sequence controlled by the server.
type KeyboardInteractive struct {
User string
Challenge ssh.KeyboardInteractiveChallenge
}

func (a *KeyboardInteractive) Name() string {
return KeyboardInteractiveName
}

func (a *KeyboardInteractive) String() string {
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
}

func (a *KeyboardInteractive) clientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: a.User,
Auth: []ssh.AuthMethod{ssh.KeyboardInteractiveChallenge(a.Challenge)},
}
}

// Password implements AuthMethod by using the given password.
type Password struct {
User string
Pass string
}

func (a *Password) Name() string {
return PasswordName
}

func (a *Password) String() string {
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
}

func (a *Password) clientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: a.User,
Auth: []ssh.AuthMethod{ssh.Password(a.Pass)},
}
}

// PasswordCallback implements AuthMethod by using a callback
// to fetch the password.
type PasswordCallback struct {
User string
Callback func() (pass string, err error)
}

func (a *PasswordCallback) Name() string {
return PasswordCallbackName
}

func (a *PasswordCallback) String() string {
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
}

func (a *PasswordCallback) clientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: a.User,
Auth: []ssh.AuthMethod{ssh.PasswordCallback(a.Callback)},
}
}

// PublicKeys implements AuthMethod by using the given
// key pairs.
type PublicKeys struct {
User string
Signer ssh.Signer
}

func (a *PublicKeys) Name() string {
return PublicKeysName
}

func (a *PublicKeys) String() string {
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
}

func (a *PublicKeys) clientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: a.User,
Auth: []ssh.AuthMethod{ssh.PublicKeys(a.Signer)},
}
}

// PublicKeysCallback implements AuthMethod by asking a
// ssh.agent.Agent to act as a signer.
type PublicKeysCallback struct {
User string
Callback func() (signers []ssh.Signer, err error)
}

func (a *PublicKeysCallback) Name() string {
return PublicKeysCallbackName
}

func (a *PublicKeysCallback) String() string {
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
}

func (a *PublicKeysCallback) clientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: a.User,
Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(a.Callback)},
}
}
94 changes: 94 additions & 0 deletions clients/ssh/auth_method_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ssh

import (
"fmt"
"testing"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type SuiteCommon struct{}

var _ = Suite(&SuiteCommon{})

func (s *SuiteRemote) TestKeyboardInteractiveName(c *C) {
a := &KeyboardInteractive{
User: "test",
Challenge: nil,
}
c.Assert(a.Name(), Equals, KeyboardInteractiveName)
}

func (s *SuiteRemote) TestKeyboardInteractiveString(c *C) {
a := &KeyboardInteractive{
User: "test",
Challenge: nil,
}
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", KeyboardInteractiveName))
}

func (s *SuiteRemote) TestPasswordName(c *C) {
a := &Password{
User: "test",
Pass: "",
}
c.Assert(a.Name(), Equals, PasswordName)
}

func (s *SuiteRemote) TestPasswordString(c *C) {
a := &Password{
User: "test",
Pass: "",
}
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordName))
}

func (s *SuiteRemote) TestPasswordCallbackName(c *C) {
a := &PasswordCallback{
User: "test",
Callback: nil,
}
c.Assert(a.Name(), Equals, PasswordCallbackName)
}

func (s *SuiteRemote) TestPasswordCallbackString(c *C) {
a := &PasswordCallback{
User: "test",
Callback: nil,
}
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordCallbackName))
}

func (s *SuiteRemote) TestPublicKeysName(c *C) {
a := &PublicKeys{
User: "test",
Signer: nil,
}
c.Assert(a.Name(), Equals, PublicKeysName)
}

func (s *SuiteRemote) TestPublicKeysString(c *C) {
a := &PublicKeys{
User: "test",
Signer: nil,
}
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysName))
}

func (s *SuiteRemote) TestPublicKeysCallbackName(c *C) {
a := &PublicKeysCallback{
User: "test",
Callback: nil,
}
c.Assert(a.Name(), Equals, PublicKeysCallbackName)
}

func (s *SuiteRemote) TestPublicKeysCallbackString(c *C) {
a := &PublicKeysCallback{
User: "test",
Callback: nil,
}
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysCallbackName))
}
Loading