From 5bed3e8fab965738f3f55bb01d15cd4748c26066 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Wed, 31 Aug 2016 23:42:36 +0200 Subject: clients: default Auth methods --- clients/http/common.go | 7 +++++++ clients/http/git_upload_pack.go | 31 +++++++++++++++++++++++++++++-- clients/http/git_upload_pack_test.go | 21 ++++++++++++++++----- 3 files changed, 52 insertions(+), 7 deletions(-) (limited to 'clients/http') diff --git a/clients/http/common.go b/clients/http/common.go index 703208b..483308a 100644 --- a/clients/http/common.go +++ b/clients/http/common.go @@ -9,15 +9,18 @@ import ( "gopkg.in/src-d/go-git.v4/core" ) +// HTTPAuthMethod concrete implementation of common.AuthMethod for HTTP services type HTTPAuthMethod interface { common.AuthMethod setAuth(r *http.Request) } +// BasicAuth represent a HTTP basic auth type BasicAuth struct { username, password string } +// NewBasicAuth returns a BasicAuth base on the given user and password func NewBasicAuth(username, password string) *BasicAuth { return &BasicAuth{username, password} } @@ -26,6 +29,7 @@ func (a *BasicAuth) setAuth(r *http.Request) { r.SetBasicAuth(a.username, a.password) } +// Name name of the auth func (a *BasicAuth) Name() string { return "http-basic-auth" } @@ -39,10 +43,12 @@ func (a *BasicAuth) String() string { return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked) } +// HTTPError a dedicated error to return errors bases on status codes type HTTPError struct { Response *http.Response } +// NewHTTPError returns a new HTTPError based on a http response func NewHTTPError(r *http.Response) error { if r.StatusCode >= 200 && r.StatusCode < 300 { return nil @@ -59,6 +65,7 @@ func NewHTTPError(r *http.Response) error { return core.NewUnexpectedError(err) } +// StatusCode returns the status code of the response func (e *HTTPError) StatusCode() int { return e.Response.StatusCode } diff --git a/clients/http/git_upload_pack.go b/clients/http/git_upload_pack.go index bca4534..888d279 100644 --- a/clients/http/git_upload_pack.go +++ b/clients/http/git_upload_pack.go @@ -12,24 +12,47 @@ import ( "gopkg.in/src-d/go-git.v4/formats/pktline" ) +// GitUploadPackService git-upoad-pack service over HTTP type GitUploadPackService struct { client *http.Client endpoint common.Endpoint auth HTTPAuthMethod } +// NewGitUploadPackService connects to a git-upload-pack service over HTTP, the +// auth is extracted from the URL, or can be provided using the SetAuth method func NewGitUploadPackService(endpoint common.Endpoint) common.GitUploadPackService { - return &GitUploadPackService{ + s := &GitUploadPackService{ client: http.DefaultClient, endpoint: endpoint, } + + s.setBasicAuthFromEndpoint() + return s } +// Connect has not any effect, is here just for meet the interface func (s *GitUploadPackService) Connect() error { return nil } -func (s *GitUploadPackService) ConnectWithAuth(auth common.AuthMethod) error { +func (s *GitUploadPackService) setBasicAuthFromEndpoint() { + info := s.endpoint.User + if info == nil { + return + } + + p, ok := info.Password() + if !ok { + return + } + + u := info.Username() + s.auth = NewBasicAuth(u, p) +} + +// SetAuth sets the AuthMethod +func (s *GitUploadPackService) SetAuth(auth common.AuthMethod) error { httpAuth, ok := auth.(HTTPAuthMethod) if !ok { return common.ErrInvalidAuthMethod @@ -39,6 +62,7 @@ func (s *GitUploadPackService) ConnectWithAuth(auth common.AuthMethod) error { return nil } +// Info returns the references info and capabilities from the service func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) { url := fmt.Sprintf( "%s/info/refs?service=%s", @@ -56,6 +80,7 @@ func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) { return i, i.Decode(pktline.NewDecoder(res.Body)) } +// Fetch request and returns a reader to a packfile func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error) { url := fmt.Sprintf( "%s/%s", @@ -98,6 +123,7 @@ func (s *GitUploadPackService) discardResponseInfo(r io.Reader) error { return nil } + func (s *GitUploadPackService) doRequest(method, url string, content *strings.Reader) (*http.Response, error) { var body io.Reader if content != nil { @@ -145,6 +171,7 @@ func (s *GitUploadPackService) applyAuthToRequest(req *http.Request) { s.auth.setAuth(req) } +// Disconnect do nothing func (s *GitUploadPackService) Disconnect() (err error) { return nil } diff --git a/clients/http/git_upload_pack_test.go b/clients/http/git_upload_pack_test.go index e344a49..579419f 100644 --- a/clients/http/git_upload_pack_test.go +++ b/clients/http/git_upload_pack_test.go @@ -16,19 +16,30 @@ var _ = Suite(&RemoteSuite{}) func (s *RemoteSuite) SetUpSuite(c *C) { var err error - s.Endpoint, err = common.NewEndpoint("https://github.com/tyba/git-fixture") + s.Endpoint, err = common.NewEndpoint("https://github.com/git-fixtures/basic") c.Assert(err, IsNil) } +func (s *RemoteSuite) TestNewGitUploadPackServiceAuth(c *C) { + e, err := common.NewEndpoint("https://foo:bar@github.com/git-fixtures/basic") + c.Assert(err, IsNil) + + r := NewGitUploadPackService(e) + auth := r.(*GitUploadPackService).auth + + c.Assert(auth.String(), Equals, "http-basic-auth - foo:*******") +} + func (s *RemoteSuite) TestConnect(c *C) { r := NewGitUploadPackService(s.Endpoint) c.Assert(r.Connect(), IsNil) } -func (s *RemoteSuite) TestConnectWithAuth(c *C) { +func (s *RemoteSuite) TestSetAuth(c *C) { auth := &BasicAuth{} r := NewGitUploadPackService(s.Endpoint) - c.Assert(r.ConnectWithAuth(auth), IsNil) + r.SetAuth(auth) + c.Assert(auth, Equals, r.(*GitUploadPackService).auth) } type mockAuth struct{} @@ -36,9 +47,9 @@ type mockAuth struct{} func (*mockAuth) Name() string { return "" } func (*mockAuth) String() string { return "" } -func (s *RemoteSuite) TestConnectWithAuthWrongType(c *C) { +func (s *RemoteSuite) TestSetAuthWrongType(c *C) { r := NewGitUploadPackService(s.Endpoint) - c.Assert(r.ConnectWithAuth(&mockAuth{}), Equals, common.ErrInvalidAuthMethod) + c.Assert(r.SetAuth(&mockAuth{}), Equals, common.ErrInvalidAuthMethod) } func (s *RemoteSuite) TestInfoEmpty(c *C) { -- cgit