| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package aws |
| 6 |
|
| 7 |
import ( |
| 8 |
"net/http" |
| 9 |
|
| 10 |
"github.com/smartystreets/go-aws-auth" |
| 11 |
) |
| 12 |
|
| 13 |
// NewV4SigningClient returns an *http.Client that will sign all requests with AWS V4 Signing. |
| 14 |
func NewV4SigningClient(credentials awsauth.Credentials) *http.Client { |
| 15 |
return &http.Client{ |
| 16 |
Transport: V4Transport{ |
| 17 |
HTTPClient: http.DefaultClient, |
| 18 |
Credentials: credentials, |
| 19 |
}, |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// V4Transport is a RoundTripper that will sign requests with AWS V4 Signing |
| 24 |
type V4Transport struct { |
| 25 |
HTTPClient *http.Client |
| 26 |
Credentials awsauth.Credentials |
| 27 |
} |
| 28 |
|
| 29 |
// RoundTrip uses the underlying RoundTripper transport, but signs request first with AWS V4 Signing |
| 30 |
func (st V4Transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 31 |
// Instead of directly modifying the request then calling http.DefaultTransport, |
| 32 |
// instead restart the request with the HTTPClient.Do function, |
| 33 |
// because the HTTPClient includes safeguards around not forwarding the |
| 34 |
// signed Authorization header to untrusted domains. |
| 35 |
return st.HTTPClient.Do(awsauth.Sign4(req, st.Credentials)) |
| 36 |
} |