-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
idtoken.go
230 lines (206 loc) · 6.84 KB
/
idtoken.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2020 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package idtoken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strings"
"cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/impersonate"
"google.golang.org/api/internal"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
htransport "google.golang.org/api/transport/http"
)
// ClientOption is aliased so relevant options are easily found in the docs.
// ClientOption is for configuring a Google API client or transport.
type ClientOption = option.ClientOption
type credentialsType int
const (
unknownCredType credentialsType = iota
serviceAccount
impersonatedServiceAccount
externalAccount
)
// NewClient creates a HTTP Client that automatically adds an ID token to each
// request via an Authorization header. The token will have the audience
// provided and be configured with the supplied options. The parameter audience
// may not be empty.
func NewClient(ctx context.Context, audience string, opts ...ClientOption) (*http.Client, error) {
var ds internal.DialSettings
for _, opt := range opts {
opt.Apply(&ds)
}
if err := ds.Validate(); err != nil {
return nil, err
}
if ds.NoAuth {
return nil, fmt.Errorf("idtoken: option.WithoutAuthentication not supported")
}
if ds.APIKey != "" {
return nil, fmt.Errorf("idtoken: option.WithAPIKey not supported")
}
if ds.TokenSource != nil {
return nil, fmt.Errorf("idtoken: option.WithTokenSource not supported")
}
ts, err := NewTokenSource(ctx, audience, opts...)
if err != nil {
return nil, err
}
// Skip DialSettings validation so added TokenSource will not conflict with user
// provided credentials.
opts = append(opts, option.WithTokenSource(ts), internaloption.SkipDialSettingsValidation())
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
httpTransport.MaxIdleConnsPerHost = 100
t, err := htransport.NewTransport(ctx, httpTransport, opts...)
if err != nil {
return nil, err
}
return &http.Client{Transport: t}, nil
}
// NewTokenSource creates a TokenSource that returns ID tokens with the audience
// provided and configured with the supplied options. The parameter audience may
// not be empty.
func NewTokenSource(ctx context.Context, audience string, opts ...ClientOption) (oauth2.TokenSource, error) {
if audience == "" {
return nil, fmt.Errorf("idtoken: must supply a non-empty audience")
}
var ds internal.DialSettings
for _, opt := range opts {
opt.Apply(&ds)
}
if err := ds.Validate(); err != nil {
return nil, err
}
if ds.TokenSource != nil {
return nil, fmt.Errorf("idtoken: option.WithTokenSource not supported")
}
if ds.ImpersonationConfig != nil {
return nil, fmt.Errorf("idtoken: option.WithImpersonatedCredentials not supported")
}
return newTokenSource(ctx, audience, &ds)
}
func newTokenSource(ctx context.Context, audience string, ds *internal.DialSettings) (oauth2.TokenSource, error) {
creds, err := internal.Creds(ctx, ds)
if err != nil {
return nil, err
}
if len(creds.JSON) > 0 {
return tokenSourceFromBytes(ctx, creds.JSON, audience, ds)
}
// If internal.Creds did not return a response with JSON fallback to the
// metadata service as the creds.TokenSource is not an ID token.
if metadata.OnGCE() {
return computeTokenSource(audience, ds)
}
return nil, fmt.Errorf("idtoken: couldn't find any credentials")
}
func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds *internal.DialSettings) (oauth2.TokenSource, error) {
allowedType, err := getAllowedType(data)
if err != nil {
return nil, err
}
switch allowedType {
case serviceAccount:
cfg, err := google.JWTConfigFromJSON(data, ds.GetScopes()...)
if err != nil {
return nil, err
}
customClaims := ds.CustomClaims
if customClaims == nil {
customClaims = make(map[string]interface{})
}
customClaims["target_audience"] = audience
cfg.PrivateClaims = customClaims
cfg.UseIDToken = true
ts := cfg.TokenSource(ctx)
tok, err := ts.Token()
if err != nil {
return nil, err
}
return oauth2.ReuseTokenSource(tok, ts), nil
case impersonatedServiceAccount, externalAccount:
type url struct {
ServiceAccountImpersonationURL string `json:"service_account_impersonation_url"`
}
var accountURL *url
if err := json.Unmarshal(data, &accountURL); err != nil {
return nil, err
}
account := filepath.Base(accountURL.ServiceAccountImpersonationURL)
account = strings.Split(account, ":")[0]
config := impersonate.IDTokenConfig{
Audience: audience,
TargetPrincipal: account,
IncludeEmail: true,
}
ts, err := impersonate.IDTokenSource(ctx, config, option.WithCredentialsJSON(data))
if err != nil {
return nil, err
}
return ts, nil
default:
return nil, fmt.Errorf("idtoken: unsupported credentials type")
}
}
// getAllowedType returns the credentials type of type credentialsType, and an error.
// allowed types are "service_account" and "impersonated_service_account"
func getAllowedType(data []byte) (credentialsType, error) {
var t credentialsType
if len(data) == 0 {
return t, fmt.Errorf("idtoken: credential provided is 0 bytes")
}
var f struct {
Type string `json:"type"`
}
if err := json.Unmarshal(data, &f); err != nil {
return t, err
}
t = parseCredType(f.Type)
return t, nil
}
func parseCredType(typeString string) credentialsType {
switch typeString {
case "service_account":
return serviceAccount
case "impersonated_service_account":
return impersonatedServiceAccount
case "external_account":
return externalAccount
default:
return unknownCredType
}
}
// WithCustomClaims optionally specifies custom private claims for an ID token.
func WithCustomClaims(customClaims map[string]interface{}) ClientOption {
return withCustomClaims(customClaims)
}
type withCustomClaims map[string]interface{}
func (w withCustomClaims) Apply(o *internal.DialSettings) {
o.CustomClaims = w
}
// WithCredentialsFile returns a ClientOption that authenticates
// API calls with the given service account or refresh token JSON
// credentials file.
func WithCredentialsFile(filename string) ClientOption {
return option.WithCredentialsFile(filename)
}
// WithCredentialsJSON returns a ClientOption that authenticates
// API calls with the given service account or refresh token JSON
// credentials.
func WithCredentialsJSON(p []byte) ClientOption {
return option.WithCredentialsJSON(p)
}
// WithHTTPClient returns a ClientOption that specifies the HTTP client to use
// as the basis of communications. This option may only be used with services
// that support HTTP as their communication transport. When used, the
// WithHTTPClient option takes precedent over all other supplied options.
func WithHTTPClient(client *http.Client) ClientOption {
return option.WithHTTPClient(client)
}