From 6be0bc1233b1a31e749354f1d54104b49d5bf19b Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Thu, 5 Nov 2015 19:08:20 +0100 Subject: clients: new AuthMethod and ConnectWithAuth --- clients/http/common.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'clients/http/common.go') 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 = "" + } + + return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked) +} + type HTTPError struct { Response *http.Response } -- cgit