@@ -20,9 +20,11 @@ func cacheOptions(cfg *Config) []auth.Option {
2020}
2121
2222// serviceToServiceVisitor returns a visitor that sets the Authorization header
23- // to the token from the auth token source and the provided secondary header to
24- // the token from the secondary token source.
25- func serviceToServiceVisitor (primary , secondary oauth2.TokenSource , secondaryHeader string , opts ... auth.Option ) func (r * http.Request ) error {
23+ // to the token from the primary token source and the provided secondary header
24+ // to the token from the secondary token source. If secondaryOptional is true,
25+ // a failure to get the secondary token logs a warning and skips the header
26+ // instead of returning an error.
27+ func serviceToServiceVisitor (primary , secondary oauth2.TokenSource , secondaryHeader string , secondaryOptional bool , opts ... auth.Option ) func (r * http.Request ) error {
2628 refreshableAuth := auth .NewCachedTokenSource (authconv .AuthTokenSource (primary ), opts ... )
2729 refreshableSecondary := auth .NewCachedTokenSource (authconv .AuthTokenSource (secondary ), opts ... )
2830 return func (r * http.Request ) error {
@@ -34,36 +36,17 @@ func serviceToServiceVisitor(primary, secondary oauth2.TokenSource, secondaryHea
3436
3537 cloud , err := refreshableSecondary .Token (context .Background ())
3638 if err != nil {
39+ if secondaryOptional {
40+ logger .Warnf (r .Context (), "Failed to get secondary token for %s header: %v. Skipping." , secondaryHeader , err )
41+ return nil
42+ }
3743 return fmt .Errorf ("cloud token: %w" , err )
3844 }
3945 r .Header .Set (secondaryHeader , cloud .AccessToken )
4046 return nil
4147 }
4248}
4349
44- // serviceToServiceVisitorWithFallback is like serviceToServiceVisitor but
45- // logs a warning and skips the secondary header when the secondary token
46- // source fails, instead of returning an error.
47- func serviceToServiceVisitorWithFallback (primary , secondary oauth2.TokenSource , secondaryHeader string , opts ... auth.Option ) func (r * http.Request ) error {
48- refreshableAuth := auth .NewCachedTokenSource (authconv .AuthTokenSource (primary ), opts ... )
49- refreshableSecondary := auth .NewCachedTokenSource (authconv .AuthTokenSource (secondary ), opts ... )
50- return func (r * http.Request ) error {
51- inner , err := refreshableAuth .Token (context .Background ())
52- if err != nil {
53- return fmt .Errorf ("inner token: %w" , err )
54- }
55- inner .SetAuthHeader (r )
56-
57- cloud , err := refreshableSecondary .Token (context .Background ())
58- if err != nil {
59- logger .Warnf (r .Context (), "Failed to get secondary token for %s header: %v. Skipping." , secondaryHeader , err )
60- return nil
61- }
62- r .Header .Set (secondaryHeader , cloud .AccessToken )
63- return nil
64- }
65- }
66-
6750// The same as serviceToServiceVisitor, but without a secondary token source.
6851func refreshableVisitor (inner oauth2.TokenSource , opts ... auth.Option ) func (r * http.Request ) error {
6952 return refreshableAuthVisitor (authconv .AuthTokenSource (inner ), opts ... )
0 commit comments