Skip to content

Commit 8413325

Browse files
Merge pull request #397 from Chhekur/chhekur-add-bearer-auth
2 parents 4ed46d0 + 63d5e6d commit 8413325

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

jira.go

+35
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,41 @@ func (t *BasicAuthTransport) transport() http.RoundTripper {
382382
return http.DefaultTransport
383383
}
384384

385+
// BearerAuthTransport is a http.RoundTripper that authenticates all requests
386+
// using Jira's bearer (oauth 2.0 (3lo)) based authentication.
387+
type BearerAuthTransport struct {
388+
Token string
389+
390+
// Transport is the underlying HTTP transport to use when making requests.
391+
// It will default to http.DefaultTransport if nil.
392+
Transport http.RoundTripper
393+
}
394+
395+
// RoundTrip implements the RoundTripper interface. We just add the
396+
// bearer token and return the RoundTripper for this transport type.
397+
func (t *BearerAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
398+
req2 := cloneRequest(req) // per RoundTripper contract
399+
400+
req2.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.Token))
401+
return t.transport().RoundTrip(req2)
402+
}
403+
404+
// Client returns an *http.Client that makes requests that are authenticated
405+
// using HTTP Basic Authentication. This is a nice little bit of sugar
406+
// so we can just get the client instead of creating the client in the calling code.
407+
// If it's necessary to send more information on client init, the calling code can
408+
// always skip this and set the transport itself.
409+
func (t *BearerAuthTransport) Client() *http.Client {
410+
return &http.Client{Transport: t}
411+
}
412+
413+
func (t *BearerAuthTransport) transport() http.RoundTripper {
414+
if t.Transport != nil {
415+
return t.Transport
416+
}
417+
return http.DefaultTransport
418+
}
419+
385420
// PATAuthTransport is an http.RoundTripper that authenticates all requests
386421
// using the Personal Access Token specified.
387422
// See here for more info: https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html

0 commit comments

Comments
 (0)