| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package auth implements a wrapper around golang.org/x/oauth2. | 5 // Package auth implements a wrapper around golang.org/x/oauth2. |
| 6 // | 6 // |
| 7 // Its main improvement is the on-disk cache for OAuth tokens, which is | 7 // Its main improvement is the on-disk cache for OAuth tokens, which is |
| 8 // especially important for 3-legged interactive OAuth flows: its usage | 8 // especially important for 3-legged interactive OAuth flows: its usage |
| 9 // eliminates annoying login prompts each time a program is used (because the | 9 // eliminates annoying login prompts each time a program is used (because the |
| 10 // refresh token can now be reused). The cache also allows to reduce unnecessary | 10 // refresh token can now be reused). The cache also allows to reduce unnecessary |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 // Invoked by Authenticator if AutoSelectMethod is passed as Method in Options. | 338 // Invoked by Authenticator if AutoSelectMethod is passed as Method in Options. |
| 339 // It picks the first applicable method in this order: | 339 // It picks the first applicable method in this order: |
| 340 // * ServiceAccountMethod (if the service account private key is configured). | 340 // * ServiceAccountMethod (if the service account private key is configured). |
| 341 // * LUCIContextMethod (if running inside LUCI_CONTEXT with an auth server). | 341 // * LUCIContextMethod (if running inside LUCI_CONTEXT with an auth server). |
| 342 // * GCEMetadataMethod (if running on GCE and GCEAllowAsDefault is true). | 342 // * GCEMetadataMethod (if running on GCE and GCEAllowAsDefault is true). |
| 343 // * UserCredentialsMethod (if no other method applies). | 343 // * UserCredentialsMethod (if no other method applies). |
| 344 // | 344 // |
| 345 // Beware: it may do relatively heavy calls on first usage (to detect GCE | 345 // Beware: it may do relatively heavy calls on first usage (to detect GCE |
| 346 // environment). Fast after that. | 346 // environment). Fast after that. |
| 347 func SelectBestMethod(ctx context.Context, opts Options) Method { | 347 func SelectBestMethod(ctx context.Context, opts Options) Method { |
| 348 » switch { | 348 » // Asked to use JSON private key. |
| 349 » case opts.ServiceAccountJSONPath != "" || len(opts.ServiceAccountJSON) !
= 0: | 349 » if opts.ServiceAccountJSONPath != "" || len(opts.ServiceAccountJSON) !=
0 { |
| 350 if opts.ServiceAccountJSONPath == GCEServiceAccount { | 350 if opts.ServiceAccountJSONPath == GCEServiceAccount { |
| 351 return GCEMetadataMethod | 351 return GCEMetadataMethod |
| 352 } | 352 } |
| 353 return ServiceAccountMethod | 353 return ServiceAccountMethod |
| 354 » case lucictx.GetLocalAuth(ctx) != nil: | 354 » } |
| 355 |
| 356 » // Have a local auth server and an account we are allowed to pick by def
ault. |
| 357 » // If no default account is given, don't automatically pick up this meth
od. |
| 358 » if la := lucictx.GetLocalAuth(ctx); la != nil && la.CanUseByDefault() { |
| 355 return LUCIContextMethod | 359 return LUCIContextMethod |
| 356 » case opts.GCEAllowAsDefault && metadata.OnGCE(): | 360 » } |
| 361 |
| 362 » // Running on GCE and callers are fine with automagically picking up GCE |
| 363 » // metadata server. |
| 364 » if opts.GCEAllowAsDefault && metadata.OnGCE() { |
| 357 return GCEMetadataMethod | 365 return GCEMetadataMethod |
| 358 default: | |
| 359 return UserCredentialsMethod | |
| 360 } | 366 } |
| 367 |
| 368 return UserCredentialsMethod |
| 361 } | 369 } |
| 362 | 370 |
| 363 // AllowsArbitraryScopes returns true if given authenticator options allow | 371 // AllowsArbitraryScopes returns true if given authenticator options allow |
| 364 // generating tokens for arbitrary set of scopes. | 372 // generating tokens for arbitrary set of scopes. |
| 365 // | 373 // |
| 366 // For example, using a private key to sign assertions allows to mint tokens | 374 // For example, using a private key to sign assertions allows to mint tokens |
| 367 // for any set of scopes (since there's no restriction on what scopes we can | 375 // for any set of scopes (since there's no restriction on what scopes we can |
| 368 // put into JWT to be signed). | 376 // put into JWT to be signed). |
| 369 // | 377 // |
| 370 // On other hand, using e.g GCE metadata server restricts us to use only scopes | 378 // On other hand, using e.g GCE metadata server restricts us to use only scopes |
| (...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1265 func makeIAMTokenProvider(ctx context.Context, opts *Options) (internal.TokenPro
vider, error) { | 1273 func makeIAMTokenProvider(ctx context.Context, opts *Options) (internal.TokenPro
vider, error) { |
| 1266 if opts.testingIAMTokenProvider != nil { | 1274 if opts.testingIAMTokenProvider != nil { |
| 1267 return opts.testingIAMTokenProvider, nil | 1275 return opts.testingIAMTokenProvider, nil |
| 1268 } | 1276 } |
| 1269 return internal.NewIAMTokenProvider( | 1277 return internal.NewIAMTokenProvider( |
| 1270 ctx, | 1278 ctx, |
| 1271 opts.ActAsServiceAccount, | 1279 opts.ActAsServiceAccount, |
| 1272 opts.Scopes, | 1280 opts.Scopes, |
| 1273 opts.Transport) | 1281 opts.Transport) |
| 1274 } | 1282 } |
| OLD | NEW |