-
Notifications
You must be signed in to change notification settings - Fork 546
feat: Makes ComputeCredential support scopes #2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,10 +68,12 @@ public class ComputeCredential : ServiceCredential, IOidcTokenProvider, IGoogleC | |
| public string OidcTokenUrl { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| bool IGoogleCredential.HasExplicitScopes => false; | ||
| bool IGoogleCredential.HasExplicitScopes => HasExplicitScopes; | ||
|
|
||
| /// <inheritdoc/> | ||
| bool IGoogleCredential.SupportsExplicitScopes => false; | ||
| bool IGoogleCredential.SupportsExplicitScopes => true; | ||
|
|
||
| internal string EffectiveTokenServerUrl { get; } | ||
|
|
||
| /// <summary> | ||
| /// An initializer class for the Compute credential. It uses <see cref="GoogleAuthConsts.ComputeTokenUrl"/> | ||
|
|
@@ -107,14 +109,38 @@ internal Initializer(ComputeCredential other) | |
| public ComputeCredential() : this(new Initializer()) { } | ||
|
|
||
| /// <summary>Constructs a new Compute credential instance.</summary> | ||
| public ComputeCredential(Initializer initializer) : base(initializer) => OidcTokenUrl = initializer.OidcTokenUrl; | ||
| public ComputeCredential(Initializer initializer) : base(initializer) | ||
| { | ||
| OidcTokenUrl = initializer.OidcTokenUrl; | ||
| if (HasExplicitScopes) | ||
| { | ||
| var uriBuilder = new UriBuilder(TokenServerUrl); | ||
| string scopesQuery = $"scopes={string.Join(",", Scopes)}"; | ||
|
|
||
| // As per https://docs.microsoft.com/en-us/dotnet/api/system.uribuilder.query?view=net-6.0#examples | ||
| if (uriBuilder.Query is null || uriBuilder.Query.Length <= 1) | ||
| { | ||
| uriBuilder.Query = scopesQuery; | ||
| } | ||
| else | ||
| { | ||
| uriBuilder.Query = $"{uriBuilder.Query.Substring(1)}&{scopesQuery}"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So setting the query you don't want the leading "?" (prior to .NET 5), but fetching it you receive it? That sucks so much.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's all very "weird", some tests failed and some tests didn't when I first just appended the scopes with the potential |
||
| } | ||
| EffectiveTokenServerUrl = uriBuilder.Uri.AbsoluteUri; | ||
| } | ||
| else | ||
| { | ||
| EffectiveTokenServerUrl = TokenServerUrl; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| IGoogleCredential IGoogleCredential.WithQuotaProject(string quotaProject) => | ||
| new ComputeCredential(new Initializer(this) { QuotaProject = quotaProject }); | ||
|
|
||
| /// <inheritdoc/> | ||
| IGoogleCredential IGoogleCredential.MaybeWithScopes(IEnumerable<string> scopes) => this; | ||
| IGoogleCredential IGoogleCredential.MaybeWithScopes(IEnumerable<string> scopes) => | ||
| new ComputeCredential(new Initializer(this) { Scopes = scopes }); | ||
|
|
||
| /// <inheritdoc/> | ||
| IGoogleCredential IGoogleCredential.WithUserForDomainWideDelegation(string user) => | ||
|
|
@@ -130,7 +156,7 @@ IGoogleCredential IGoogleCredential.WithHttpClientFactory(IHttpClientFactory htt | |
| public override async Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken) | ||
| { | ||
| // Create and send the HTTP request to compute server token URL. | ||
| var httpRequest = new HttpRequestMessage(HttpMethod.Get, TokenServerUrl); | ||
| var httpRequest = new HttpRequestMessage(HttpMethod.Get, EffectiveTokenServerUrl); | ||
| httpRequest.Headers.Add(MetadataFlavor, GoogleMetadataHeader); | ||
| var response = await HttpClient.SendAsync(httpRequest, taskCancellationToken).ConfigureAwait(false); | ||
| Token = await TokenResponse.FromHttpResponseAsync(response, Clock, Logger).ConfigureAwait(false); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.