2015-08-27 03:42:02 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-05-17 19:26:57 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-05-13 22:06:46 +04:00
|
|
|
|
2014-05-06 01:54:23 +04:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
2014-12-10 02:17:33 +03:00
|
|
|
"errors"
|
2018-06-29 01:10:32 +03:00
|
|
|
"log"
|
2014-05-06 01:54:23 +04:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-12-10 02:17:33 +03:00
|
|
|
// Transport is an http.RoundTripper that makes OAuth 2.0 HTTP requests,
|
|
|
|
// wrapping a base RoundTripper and adding an Authorization header
|
|
|
|
// with a token from the supplied Sources.
|
2014-10-28 03:35:35 +03:00
|
|
|
//
|
2014-12-10 02:17:33 +03:00
|
|
|
// Transport is a low-level mechanism. Most code will use the
|
|
|
|
// higher-level Config.Client method instead.
|
2014-08-14 09:22:35 +04:00
|
|
|
type Transport struct {
|
2014-12-10 02:17:33 +03:00
|
|
|
// Source supplies the token to add to outgoing requests'
|
|
|
|
// Authorization headers.
|
|
|
|
Source TokenSource
|
2014-05-06 01:54:23 +04:00
|
|
|
|
2014-12-10 02:17:33 +03:00
|
|
|
// Base is the base RoundTripper used to make HTTP requests.
|
|
|
|
// If nil, http.DefaultTransport is used.
|
|
|
|
Base http.RoundTripper
|
2014-05-10 11:41:39 +04:00
|
|
|
}
|
|
|
|
|
2014-08-14 09:22:35 +04:00
|
|
|
// RoundTrip authorizes and authenticates the request with an
|
2018-05-28 23:23:04 +03:00
|
|
|
// access token from Transport's Source.
|
2014-11-25 04:07:50 +03:00
|
|
|
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
2018-05-29 00:26:48 +03:00
|
|
|
reqBodyClosed := false
|
|
|
|
if req.Body != nil {
|
|
|
|
defer func() {
|
|
|
|
if !reqBodyClosed {
|
|
|
|
req.Body.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:17:33 +03:00
|
|
|
if t.Source == nil {
|
|
|
|
return nil, errors.New("oauth2: Transport's Source is nil")
|
|
|
|
}
|
|
|
|
token, err := t.Source.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req2 := cloneRequest(req) // per RoundTripper contract
|
|
|
|
token.SetAuthHeader(req2)
|
2018-05-29 00:26:48 +03:00
|
|
|
|
2018-06-29 01:10:32 +03:00
|
|
|
// req.Body is assumed to be closed by the base RoundTripper.
|
2018-05-29 00:26:48 +03:00
|
|
|
reqBodyClosed = true
|
2018-06-29 01:10:32 +03:00
|
|
|
return t.base().RoundTrip(req2)
|
2014-12-10 02:17:33 +03:00
|
|
|
}
|
2014-05-06 01:54:23 +04:00
|
|
|
|
2018-06-29 01:10:32 +03:00
|
|
|
var cancelOnce sync.Once
|
|
|
|
|
|
|
|
// CancelRequest does nothing. It used to be a legacy cancellation mechanism
|
|
|
|
// but now only it only logs on first use to warn that it's deprecated.
|
|
|
|
//
|
|
|
|
// Deprecated: use contexts for cancellation instead.
|
2014-12-10 02:17:33 +03:00
|
|
|
func (t *Transport) CancelRequest(req *http.Request) {
|
2018-06-29 01:10:32 +03:00
|
|
|
cancelOnce.Do(func() {
|
|
|
|
log.Printf("deprecated: golang.org/x/oauth2: Transport.CancelRequest no longer does anything; use contexts")
|
|
|
|
})
|
2014-05-06 01:54:23 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 02:17:33 +03:00
|
|
|
func (t *Transport) base() http.RoundTripper {
|
|
|
|
if t.Base != nil {
|
|
|
|
return t.Base
|
|
|
|
}
|
|
|
|
return http.DefaultTransport
|
2014-05-06 01:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cloneRequest returns a clone of the provided *http.Request.
|
|
|
|
// The clone is a shallow copy of the struct and its Header map.
|
|
|
|
func cloneRequest(r *http.Request) *http.Request {
|
|
|
|
// shallow copy of the struct
|
|
|
|
r2 := new(http.Request)
|
|
|
|
*r2 = *r
|
|
|
|
// deep copy of the Header
|
2014-12-10 02:17:33 +03:00
|
|
|
r2.Header = make(http.Header, len(r.Header))
|
2014-05-06 01:54:23 +04:00
|
|
|
for k, s := range r.Header {
|
2014-12-10 02:17:33 +03:00
|
|
|
r2.Header[k] = append([]string(nil), s...)
|
2014-05-06 01:54:23 +04:00
|
|
|
}
|
|
|
|
return r2
|
|
|
|
}
|