Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

plumbing/transport/http: add callback based basic auth #419

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions plumbing/transport/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ func (a *BasicAuth) String() string {
return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked)
}

// BasicAuthCallback represent a HTTP basic auth with a password callback
type BasicAuthCallback struct {
username string
callback func() (password string)
}

// NewBasicAuthCallback returns a basicAuthCallback based on the callback function
func NewBasicAuthCallback(username string, callback func() (password string)) *BasicAuthCallback {
return &BasicAuthCallback{username, callback}
}

func (a *BasicAuthCallback) setAuth(r *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is completely untested, can you cover it?

if a == nil || a.callback == nil {
return
}

r.SetBasicAuth(a.username, a.callback())
}

// Name is name of the auth
func (a *BasicAuthCallback) Name() string {
return "http-basic-auth-callback"
}

func (a *BasicAuthCallback) String() string {
return fmt.Sprintf("%s - %s", a.Name(), a.username)
}

// Err is a dedicated error to return errors based on status code
type Err struct {
Response *http.Response
Expand Down
7 changes: 7 additions & 0 deletions plumbing/transport/http/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func (s *ClientSuite) TestNewBasicAuth(c *C) {
c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")
}

func (s *ClientSuite) TestNewBasicAuthCallback(c *C) {
a := NewBasicAuthCallback("foo", func() string { return "qux" })

c.Assert(a.Name(), Equals, "http-basic-auth-callback")
c.Assert(a.String(), Equals, "http-basic-auth-callback - foo")
}

func (s *ClientSuite) TestNewErrOK(c *C) {
res := &http.Response{StatusCode: http.StatusOK}
err := NewErr(res)
Expand Down