Andrew Gerrand | 038cb4a | 2015-08-27 10:42:02 +1000 | [diff] [blame] | 1 | // Copyright 2014 The Go Authors. All rights reserved. |
Burcu Dogan | abc4bcd | 2014-05-17 17:26:57 +0200 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
Burcu Dogan | d7c8bcd | 2014-05-13 21:06:46 +0300 | [diff] [blame] | 4 | |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 5 | package oauth2 |
| 6 | |
| 7 | import ( |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 8 | "errors" |
Tim Cooper | 858c2ad | 2018-06-28 19:10:32 -0300 | [diff] [blame] | 9 | "log" |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 10 | "net/http" |
| 11 | "sync" |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 12 | ) |
| 13 | |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 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. |
Burcu Dogan | 0ae3d4e | 2014-10-27 17:35:35 -0700 | [diff] [blame] | 17 | // |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 18 | // Transport is a low-level mechanism. Most code will use the |
| 19 | // higher-level Config.Client method instead. |
Burcu Dogan | ee77246 | 2014-08-13 22:22:35 -0700 | [diff] [blame] | 20 | type Transport struct { |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 21 | // Source supplies the token to add to outgoing requests' |
| 22 | // Authorization headers. |
| 23 | Source TokenSource |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 24 | |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 25 | // Base is the base RoundTripper used to make HTTP requests. |
| 26 | // If nil, http.DefaultTransport is used. |
| 27 | Base http.RoundTripper |
Burcu Dogan | 2af52e7 | 2014-05-10 09:41:39 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Burcu Dogan | ee77246 | 2014-08-13 22:22:35 -0700 | [diff] [blame] | 30 | // RoundTrip authorizes and authenticates the request with an |
Tim Cooper | ec22f46 | 2018-05-28 17:23:04 -0300 | [diff] [blame] | 31 | // access token from Transport's Source. |
Burcu Dogan | b846388 | 2014-11-24 17:07:50 -0800 | [diff] [blame] | 32 | func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { |
Tim Cooper | 30b72df | 2018-05-28 18:26:48 -0300 | [diff] [blame] | 33 | reqBodyClosed := false |
| 34 | if req.Body != nil { |
| 35 | defer func() { |
| 36 | if !reqBodyClosed { |
| 37 | req.Body.Close() |
| 38 | } |
| 39 | }() |
| 40 | } |
| 41 | |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 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 |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 50 | req2 := cloneRequest(req) // per RoundTripper contract |
| 51 | token.SetAuthHeader(req2) |
Tim Cooper | 30b72df | 2018-05-28 18:26:48 -0300 | [diff] [blame] | 52 | |
Tim Cooper | 858c2ad | 2018-06-28 19:10:32 -0300 | [diff] [blame] | 53 | // req.Body is assumed to be closed by the base RoundTripper. |
Tim Cooper | 30b72df | 2018-05-28 18:26:48 -0300 | [diff] [blame] | 54 | reqBodyClosed = true |
Tim Cooper | 858c2ad | 2018-06-28 19:10:32 -0300 | [diff] [blame] | 55 | return t.base().RoundTrip(req2) |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Tim Cooper | 858c2ad | 2018-06-28 19:10:32 -0300 | [diff] [blame] | 58 | var 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. |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 64 | func (t *Transport) CancelRequest(req *http.Request) { |
Tim Cooper | 858c2ad | 2018-06-28 19:10:32 -0300 | [diff] [blame] | 65 | cancelOnce.Do(func() { |
| 66 | log.Printf("deprecated: golang.org/x/oauth2: Transport.CancelRequest no longer does anything; use contexts") |
| 67 | }) |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 70 | func (t *Transport) base() http.RoundTripper { |
| 71 | if t.Base != nil { |
| 72 | return t.Base |
| 73 | } |
| 74 | return http.DefaultTransport |
| 75 | } |
| 76 | |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 77 | // cloneRequest returns a clone of the provided *http.Request. |
| 78 | // The clone is a shallow copy of the struct and its Header map. |
| 79 | func 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 |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 84 | r2.Header = make(http.Header, len(r.Header)) |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 85 | for k, s := range r.Header { |
Brad Fitzpatrick | a568078 | 2014-12-10 10:17:33 +1100 | [diff] [blame] | 86 | r2.Header[k] = append([]string(nil), s...) |
Burcu Dogan | c32deba | 2014-05-05 23:54:23 +0200 | [diff] [blame] | 87 | } |
| 88 | return r2 |
| 89 | } |