Skip to content

Commit 6898597

Browse files
authored
feat(auth): add httptransport package (#8567)
This package is the equivalent of google.golang.org/api/transport/http for the new auth library. The biggest different is the main function does not return an endpoint so we will want to add a helper in gax or somewhere for this. Also, there are is an extra helper to add a TokenProvider to a http.Client which should elevate some user needs for extra extensibility of a client. They can simply customize away and user the helper to add auth.
1 parent e4a54cd commit 6898597

10 files changed

Lines changed: 1108 additions & 61 deletions

File tree

auth/detect/internal/impersonate/impersonate.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424

2525
"cloud.google.com/go/auth"
2626
"cloud.google.com/go/auth/internal"
27-
"cloud.google.com/go/auth/internal/transport"
2827
)
2928

3029
const (
3130
defaultTokenLifetime = "3600s"
31+
authHeaderKey = "Authorization"
3232
)
3333

3434
// generateAccesstokenReq is used for service account impersonation
@@ -93,7 +93,7 @@ func (tp *Options) Token(ctx context.Context) (*auth.Token, error) {
9393
return nil, fmt.Errorf("detect: unable to create impersonation request: %w", err)
9494
}
9595
req.Header.Set("Content-Type", "application/json")
96-
if err := transport.SetAuthHeader(ctx, tp.Tp, req); err != nil {
96+
if err := setAuthHeader(ctx, tp.Tp, req); err != nil {
9797
return nil, err
9898
}
9999
resp, err := tp.Client.Do(req)
@@ -123,3 +123,16 @@ func (tp *Options) Token(ctx context.Context) (*auth.Token, error) {
123123
Type: internal.TokenTypeBearer,
124124
}, nil
125125
}
126+
127+
func setAuthHeader(ctx context.Context, tp auth.TokenProvider, r *http.Request) error {
128+
t, err := tp.Token(ctx)
129+
if err != nil {
130+
return err
131+
}
132+
typ := t.Type
133+
if typ == "" {
134+
typ = internal.TokenTypeBearer
135+
}
136+
r.Header.Set(authHeaderKey, typ+" "+t.Value)
137+
return nil
138+
}

auth/go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ go 1.19
55
require (
66
cloud.google.com/go/compute/metadata v0.2.3
77
github.com/google/go-cmp v0.5.9
8+
go.opencensus.io v0.24.0
9+
golang.org/x/net v0.14.0
810
)
911

10-
require cloud.google.com/go/compute v1.14.0 // indirect
12+
require (
13+
cloud.google.com/go/compute v1.19.1 // indirect
14+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
15+
golang.org/x/text v0.12.0 // indirect
16+
)

auth/go.sum

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,103 @@
1-
cloud.google.com/go/compute v1.14.0 h1:hfm2+FfxVmnRlh6LpB7cg1ZNU+5edAHmW679JePztk0=
2-
cloud.google.com/go/compute v1.14.0/go.mod h1:YfLtxrj9sU4Yxv+sXzZkyPjEyPBZfXHUvjxega5vAdo=
1+
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
cloud.google.com/go/compute v1.19.1 h1:am86mquDUgjGNWxiGn+5PGLbmgiWXlE/yNWpIpNvuXY=
3+
cloud.google.com/go/compute v1.19.1/go.mod h1:6ylj3a05WF8leseCdIf77NK0g1ey+nj5IKd5/kvShxE=
34
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
45
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
6+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
7+
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
8+
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
9+
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
13+
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
14+
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
15+
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
16+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
17+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
18+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
19+
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
20+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
21+
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
22+
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
23+
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
24+
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
25+
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
26+
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
27+
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
28+
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
29+
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
30+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
31+
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
32+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
33+
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
34+
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
535
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
636
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
37+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
38+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
39+
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
40+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
41+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
42+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
43+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
44+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
45+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
46+
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
47+
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
48+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
49+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
50+
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
51+
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
52+
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
53+
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
54+
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
55+
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
56+
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
57+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
58+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
59+
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
60+
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
61+
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
62+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
63+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
64+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
65+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
66+
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
67+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
68+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
69+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
70+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
71+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
72+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
73+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
74+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
75+
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
76+
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
77+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
78+
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
79+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
80+
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
81+
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
82+
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
83+
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
84+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
85+
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
86+
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
87+
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
88+
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
89+
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
90+
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
91+
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
92+
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
93+
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
94+
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
95+
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
96+
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
97+
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
98+
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
99+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
100+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
101+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
102+
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
103+
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package httptransport
16+
17+
import (
18+
"crypto/tls"
19+
"errors"
20+
"fmt"
21+
"net/http"
22+
23+
"cloud.google.com/go/auth"
24+
"cloud.google.com/go/auth/detect"
25+
"cloud.google.com/go/auth/internal"
26+
"cloud.google.com/go/auth/internal/transport"
27+
)
28+
29+
// ClientCertProvider is a function that returns a TLS client certificate to be
30+
// used when opening TLS connections. It follows the same semantics as
31+
// [crypto/tls.Config.GetClientCertificate].
32+
type ClientCertProvider = func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
33+
34+
// Options used to configure a [net/http.Client] from [NewClient].
35+
type Options struct {
36+
// DisableTelemetry disables default telemetry (OpenCensus). An example
37+
// reason to do so would be to bind custom telemetry that overrides the
38+
// defaults.
39+
DisableTelemetry bool
40+
// DisableAuthentication specifies that no authentication should be used. It
41+
// is suitable only for testing and for accessing public resources, like
42+
// public Google Cloud Storage buckets.
43+
DisableAuthentication bool
44+
// Headers are extra HTTP headers that will be appended to every outgoing
45+
// request.
46+
Headers http.Header
47+
// Endpoint overrides the default endpoint to be used for a service.
48+
Endpoint string
49+
// APIKey specifies an API key to be used as the basis for authentication.
50+
// If set DetectOpts are ignored.
51+
APIKey string
52+
// TokenProvider specifies the provider used to add Authorization header to
53+
// all requests. If set DetectOpts are ignored.
54+
TokenProvider auth.TokenProvider
55+
// ClientCertProvider is a function that returns a TLS client certificate to
56+
// be used when opening TLS connections. It follows the same semantics as
57+
// crypto/tls.Config.GetClientCertificate.
58+
ClientCertProvider ClientCertProvider
59+
// DetectOpts configures settings for detect Application Default
60+
// Credentials.
61+
DetectOpts *detect.Options
62+
63+
// InternalOptions are NOT meant to be set directly by consumers of this
64+
// package, they should only be set by generated client code.
65+
InternalOptions *InternalOptions
66+
}
67+
68+
func (o *Options) validate() error {
69+
if o == nil {
70+
return errors.New("httptransport: opts required to be non-nil")
71+
}
72+
hasCreds := o.APIKey != "" ||
73+
o.TokenProvider != nil ||
74+
(o.DetectOpts != nil && len(o.DetectOpts.CredentialsJSON) > 0) ||
75+
(o.DetectOpts != nil && o.DetectOpts.CredentialsFile != "")
76+
if o.DisableAuthentication && hasCreds {
77+
return errors.New("httptransport: DisableAuthentication is incompatible with options that set or detect credentials")
78+
}
79+
return nil
80+
}
81+
82+
// client returns the client a user set for the detect options or nil if one was
83+
// not set.
84+
func (o *Options) client() *http.Client {
85+
if o.DetectOpts != nil && o.DetectOpts.Client != nil {
86+
return o.DetectOpts.Client
87+
}
88+
return nil
89+
}
90+
91+
func (o *Options) resolveDetectOptions() *detect.Options {
92+
io := o.InternalOptions
93+
// soft-clone these so we are not updating a ref the user holds and may reuse
94+
do := transport.CloneDetectOptions(o.DetectOpts)
95+
96+
// If scoped JWTs are enabled user provided an aud, allow self-signed JWT.
97+
if (io != nil && io.EnableJWTWithScope) || do.Audience != "" {
98+
do.UseSelfSignedJWT = true
99+
}
100+
// Only default scopes if user did not also set an audience.
101+
if len(do.Scopes) == 0 && do.Audience == "" && io != nil && len(io.DefaultScopes) > 0 {
102+
do.Scopes = make([]string, len(io.DefaultScopes))
103+
copy(do.Scopes, io.DefaultScopes)
104+
}
105+
if len(do.Scopes) == 0 && do.Audience == "" && io != nil {
106+
do.Audience = o.InternalOptions.DefaultAudience
107+
}
108+
return do
109+
}
110+
111+
// InternalOptions are only meant to be set by generated client code. These are
112+
// not meant to be set directly by consumers of this package. Configuration in
113+
// this type is considered EXPERIMENTAL and may be removed at any time in the
114+
// future without warning.
115+
type InternalOptions struct {
116+
// EnableJWTWithScope specifies if scope can be used with self-signed JWT.
117+
EnableJWTWithScope bool
118+
// DefaultAudience specifies a default audience to be used as the audience
119+
// field ("aud") for the JWT token authentication.
120+
DefaultAudience string
121+
// DefaultEndpoint specifies the default endpoint.
122+
DefaultEndpoint string
123+
// DefaultMTLSEndpoint specifies the default mTLS endpoint.
124+
DefaultMTLSEndpoint string
125+
// DefaultScopes specifies the default OAuth2 scopes to be used for a
126+
// service.
127+
DefaultScopes []string
128+
}
129+
130+
// AddAuthorizationMiddleware adds a middleware to the provided client's
131+
// transport that sets the Authorization header with the value produced by the
132+
// provided [cloud.google.com/go/auth.TokenProvider]. An error is returned only
133+
// if client or tp is nil.
134+
func AddAuthorizationMiddleware(client *http.Client, tp auth.TokenProvider) error {
135+
if client == nil || tp == nil {
136+
return fmt.Errorf("httptransport: client and tp must not be nil")
137+
}
138+
base := client.Transport
139+
if base == nil {
140+
base = http.DefaultTransport.(*http.Transport).Clone()
141+
}
142+
client.Transport = &authTransport{
143+
provider: auth.NewCachedTokenProvider(tp, nil),
144+
base: base,
145+
}
146+
return nil
147+
}
148+
149+
// NewClient returns a [net/http.Client] that can be used to communicate with a
150+
// Google cloud service, configured with the provided [Options]. It
151+
// automatically appends Authorization headers to all outgoing requests.
152+
func NewClient(opts *Options) (*http.Client, error) {
153+
if err := opts.validate(); err != nil {
154+
return nil, err
155+
}
156+
// TODO(codyoss): re-add in a future PR
157+
158+
// tOpts := &transport.Options{
159+
// Endpoint: opts.Endpoint,
160+
// ClientCertProvider: opts.ClientCertProvider,
161+
// Client: opts.client(),
162+
// }
163+
// if io := opts.InternalOptions; io != nil {
164+
// tOpts.DefaultEndpoint = io.DefaultEndpoint
165+
// tOpts.DefaultMTLSEndpoint = io.DefaultMTLSEndpoint
166+
// }
167+
// clientCertProvider, dialTLSContext, err := transport.GetHTTPTransportConfig(tOpts)
168+
// if err != nil {
169+
// return nil, err
170+
// }
171+
trans, err := newTransport(defaultBaseTransport(nil), opts)
172+
if err != nil {
173+
return nil, err
174+
}
175+
return &http.Client{
176+
Transport: trans,
177+
}, nil
178+
}
179+
180+
// SetAuthHeader uses the provided token to set the Authorization header on a
181+
// request. If the token.Type is empty, the type is assumed to be Bearer.
182+
func SetAuthHeader(token *auth.Token, req *http.Request) {
183+
typ := token.Type
184+
if typ == "" {
185+
typ = internal.TokenTypeBearer
186+
}
187+
req.Header.Set("Authorization", typ+" "+token.Value)
188+
}

0 commit comments

Comments
 (0)