blob: 90657915fbcf0ea51e8d97858432d793060de436 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001// Copyright 2014 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package oauth2
6
7import (
8 "errors"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +00009 "log"
Zack Williamse940c7a2019-08-21 14:25:39 -070010 "net/http"
11 "sync"
12)
13
14// Transport is an http.RoundTripper that makes OAuth 2.0 HTTP requests,
15// wrapping a base RoundTripper and adding an Authorization header
16// with a token from the supplied Sources.
17//
18// Transport is a low-level mechanism. Most code will use the
19// higher-level Config.Client method instead.
20type Transport struct {
21 // Source supplies the token to add to outgoing requests'
22 // Authorization headers.
23 Source TokenSource
24
25 // Base is the base RoundTripper used to make HTTP requests.
26 // If nil, http.DefaultTransport is used.
27 Base http.RoundTripper
Zack Williamse940c7a2019-08-21 14:25:39 -070028}
29
30// RoundTrip authorizes and authenticates the request with an
31// access token from Transport's Source.
32func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
33 reqBodyClosed := false
34 if req.Body != nil {
35 defer func() {
36 if !reqBodyClosed {
37 req.Body.Close()
38 }
39 }()
40 }
41
42 if t.Source == nil {
43 return nil, errors.New("oauth2: Transport's Source is nil")
44 }
45 token, err := t.Source.Token()
46 if err != nil {
47 return nil, err
48 }
49
50 req2 := cloneRequest(req) // per RoundTripper contract
51 token.SetAuthHeader(req2)
Zack Williamse940c7a2019-08-21 14:25:39 -070052
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000053 // req.Body is assumed to be closed by the base RoundTripper.
Zack Williamse940c7a2019-08-21 14:25:39 -070054 reqBodyClosed = true
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000055 return t.base().RoundTrip(req2)
Zack Williamse940c7a2019-08-21 14:25:39 -070056}
57
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000058var cancelOnce sync.Once
59
60// CancelRequest does nothing. It used to be a legacy cancellation mechanism
61// but now only it only logs on first use to warn that it's deprecated.
62//
63// Deprecated: use contexts for cancellation instead.
Zack Williamse940c7a2019-08-21 14:25:39 -070064func (t *Transport) CancelRequest(req *http.Request) {
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000065 cancelOnce.Do(func() {
66 log.Printf("deprecated: golang.org/x/oauth2: Transport.CancelRequest no longer does anything; use contexts")
67 })
Zack Williamse940c7a2019-08-21 14:25:39 -070068}
69
70func (t *Transport) base() http.RoundTripper {
71 if t.Base != nil {
72 return t.Base
73 }
74 return http.DefaultTransport
75}
76
Zack Williamse940c7a2019-08-21 14:25:39 -070077// cloneRequest returns a clone of the provided *http.Request.
78// The clone is a shallow copy of the struct and its Header map.
79func cloneRequest(r *http.Request) *http.Request {
80 // shallow copy of the struct
81 r2 := new(http.Request)
82 *r2 = *r
83 // deep copy of the Header
84 r2.Header = make(http.Header, len(r.Header))
85 for k, s := range r.Header {
86 r2.Header[k] = append([]string(nil), s...)
87 }
88 return r2
89}