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

Commit bf61908

Browse files
author
Eric Billingsley
committed
plumbing/transport: http, Adds token authentication support [Fixes #858]
Signed-off-by: Eric Billingsley <ebilling@babrains.com>
1 parent b235700 commit bf61908

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

Diff for: _examples/branch/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828

2929
// Create a new plumbing.HashReference object with the name of the branch
3030
// and the hash from the HEAD. The reference name should be a full reference
31-
// name and now a abbreviated one, as is used on the git cli.
31+
// name and not an abbreviated one, as is used on the git cli.
3232
//
3333
// For tags we should use `refs/tags/%s` instead of `refs/heads/%s` used
3434
// for branches.

Diff for: plumbing/transport/http/common.go

+25
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ func (a *BasicAuth) String() string {
201201
return fmt.Sprintf("%s - %s:%s", a.Name(), a.Username, masked)
202202
}
203203

204+
// TokenAuth implements the go-git http.AuthMethod and transport.AuthMethod interfaces
205+
type TokenAuth struct {
206+
Token string
207+
}
208+
209+
func (a *TokenAuth) setAuth(r *http.Request) {
210+
if a == nil {
211+
return
212+
}
213+
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
214+
}
215+
216+
// Name is name of the auth
217+
func (a *TokenAuth) Name() string {
218+
return "http-token-auth"
219+
}
220+
221+
func (a *TokenAuth) String() string {
222+
masked := "*******"
223+
if a.Token == "" {
224+
masked = "<empty>"
225+
}
226+
return fmt.Sprintf("%s - %s", a.Name(), masked)
227+
}
228+
204229
// Err is a dedicated error to return errors based on status code
205230
type Err struct {
206231
Response *http.Response

Diff for: plumbing/transport/http/common_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ func (s *ClientSuite) TestNewBasicAuth(c *C) {
5454
c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")
5555
}
5656

57+
func (s *ClientSuite) TestNewTokenAuth(c *C) {
58+
a := &TokenAuth{"OAUTH-TOKEN-TEXT"}
59+
60+
c.Assert(a.Name(), Equals, "http-token-auth")
61+
c.Assert(a.String(), Equals, "http-token-auth - *******")
62+
63+
// Check header is set correctly
64+
req, err := http.NewRequest("GET", "https://github.com/git-fixtures/basic", nil)
65+
c.Assert(err, Equals, nil)
66+
a.setAuth(req)
67+
c.Assert(req.Header.Get("Authorization"), Equals, "Bearer OAUTH-TOKEN-TEXT")
68+
}
69+
5770
func (s *ClientSuite) TestNewErrOK(c *C) {
5871
res := &http.Response{StatusCode: http.StatusOK}
5972
err := NewErr(res)

0 commit comments

Comments
 (0)