diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2015-11-05 19:08:20 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2015-11-05 20:55:46 +0100 |
commit | 6be0bc1233b1a31e749354f1d54104b49d5bf19b (patch) | |
tree | a9fd259944a42322050247ed6b5393150f2c4cb8 /clients/http | |
parent | a80d56b21142096f458758405736f3d567c175f5 (diff) | |
download | go-git-6be0bc1233b1a31e749354f1d54104b49d5bf19b.tar.gz |
clients: new AuthMethod and ConnectWithAuth
Diffstat (limited to 'clients/http')
-rw-r--r-- | clients/http/common.go | 33 | ||||
-rw-r--r-- | clients/http/git_upload_pack.go | 23 | ||||
-rw-r--r-- | clients/http/git_upload_pack_test.go | 17 |
3 files changed, 73 insertions, 0 deletions
diff --git a/clients/http/common.go b/clients/http/common.go index 15338a8..fc3a236 100644 --- a/clients/http/common.go +++ b/clients/http/common.go @@ -1,6 +1,7 @@ package http import ( + "errors" "fmt" "net/http" @@ -8,6 +9,38 @@ import ( "gopkg.in/src-d/go-git.v2/core" ) +var InvalidAuthMethodErr = errors.New("invalid http auth method: a http.HTTPAuthMethod should be provided.") + +type HTTPAuthMethod interface { + common.AuthMethod + setAuth(r *http.Request) +} + +type BasicAuth struct { + username, password string +} + +func NewBasicAuth(username, password string) *BasicAuth { + return &BasicAuth{username, password} +} + +func (a *BasicAuth) setAuth(r *http.Request) { + r.SetBasicAuth(a.username, a.password) +} + +func (a *BasicAuth) Name() string { + return "http-basic-auth" +} + +func (a *BasicAuth) String() string { + masked := "*******" + if a.password == "" { + masked = "<empty>" + } + + return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked) +} + type HTTPError struct { Response *http.Response } diff --git a/clients/http/git_upload_pack.go b/clients/http/git_upload_pack.go index 6960be1..217e4aa 100644 --- a/clients/http/git_upload_pack.go +++ b/clients/http/git_upload_pack.go @@ -15,6 +15,7 @@ type GitUploadPackService struct { Client *http.Client endpoint common.Endpoint + auth HTTPAuthMethod } func NewGitUploadPackService() *GitUploadPackService { @@ -29,6 +30,18 @@ func (s *GitUploadPackService) Connect(url common.Endpoint) error { return nil } +func (s *GitUploadPackService) ConnectWithAuth(url common.Endpoint, auth common.AuthMethod) error { + httpAuth, ok := auth.(HTTPAuthMethod) + if !ok { + return InvalidAuthMethodErr + } + + s.endpoint = url + s.auth = httpAuth + + return nil +} + func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) { url := fmt.Sprintf("%s/info/refs?service=%s", s.endpoint, common.GitUploadPackServiceName) res, err := s.doRequest("GET", url, nil) @@ -69,8 +82,10 @@ func (s *GitUploadPackService) doRequest(method, url string, content *strings.Re } s.applyHeadersToRequest(req, content) + s.applyAuthToRequest(req) res, err := s.Client.Do(req) + if err != nil { return nil, core.NewUnexpectedError(err) } @@ -94,3 +109,11 @@ func (s *GitUploadPackService) applyHeadersToRequest(req *http.Request, content req.Header.Add("Content-Length", string(content.Len())) } } + +func (s *GitUploadPackService) applyAuthToRequest(req *http.Request) { + if s.auth == nil { + return + } + + s.auth.setAuth(req) +} diff --git a/clients/http/git_upload_pack_test.go b/clients/http/git_upload_pack_test.go index b870259..02ed37c 100644 --- a/clients/http/git_upload_pack_test.go +++ b/clients/http/git_upload_pack_test.go @@ -19,6 +19,23 @@ func (s *SuiteRemote) TestConnect(c *C) { c.Assert(r.Connect(RepositoryFixture), IsNil) } +func (s *SuiteRemote) TestConnectWithAuth(c *C) { + auth := &BasicAuth{} + r := NewGitUploadPackService() + c.Assert(r.ConnectWithAuth(RepositoryFixture, auth), IsNil) + c.Assert(r.auth, Equals, auth) +} + +type mockAuth struct{} + +func (*mockAuth) Name() string { return "" } +func (*mockAuth) String() string { return "" } + +func (s *SuiteRemote) TestConnectWithAuthWrongType(c *C) { + r := NewGitUploadPackService() + c.Assert(r.ConnectWithAuth(RepositoryFixture, &mockAuth{}), Equals, InvalidAuthMethodErr) +} + func (s *SuiteRemote) TestDefaultBranch(c *C) { r := NewGitUploadPackService() c.Assert(r.Connect(RepositoryFixture), IsNil) |