@@ -382,6 +382,41 @@ func (t *BasicAuthTransport) transport() http.RoundTripper {
382
382
return http .DefaultTransport
383
383
}
384
384
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
+
385
420
// PATAuthTransport is an http.RoundTripper that authenticates all requests
386
421
// using the Personal Access Token specified.
387
422
// See here for more info: https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html
0 commit comments