/* Copyright 2025 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using Google.Apis.Http; using Google.Apis.Util; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Google.Apis.Auth.OAuth2; /// /// External account credentials that rely on user-provided code to obtain the third-party token. /// /// /// This credential type does not validate the credential configuration. A security /// risk occurs when a credential configuration configured with malicious urls /// is used. /// You should validate credential configurations provided by untrusted sources. /// See /// Security requirements when using credential configurations from an external source /// for more details. /// public sealed class ProgrammaticExternalAccountCredential : ExternalAccountCredential, IGoogleCredential { /// /// Subject token provider which is passed to a as the means /// to obtain the third-party or external credentials token to include on the STS token exchange request. /// public interface ISubjectTokenProvider { /// /// Obtains the subject token for the third-party credentials that will be inclded on the STS token exchange request. /// The returned value will be included on the STS token exchange request as is. /// This method will only be called when no Google access token has been obtained or it has (or is about to) expired, /// /// /// The credential calling this subject token provider. This may be useful to use state stored in the credential for /// obtaining the subject token, but should be ignored otherwise. Will not be null. /// /// /// The cancellation token for the operation. /// Task GetSubjectTokenAsync(ProgrammaticExternalAccountCredential caller, CancellationToken taskCancellationToken); } /// /// Initializer for . /// new public class Initializer : ExternalAccountCredential.Initializer { /// /// Builds an initializer for . /// /// /// The STS token exchange endpoint. Must not be null or empty. /// /// /// The STS audience which contains the resource name for the workload identity pool and the provider identifier in that pool. /// Must not be null or empty. /// /// /// The STS subject token type based on the OAuth 2.0 token exchange specification. /// Must not be null or empty. /// /// /// The provider of the third-party token that's included on the STS token exchange request. /// Must not be null. /// public Initializer(string tokenUrl, string audience, string subjectTokenType, ISubjectTokenProvider subjectTokenProvider) : base( tokenUrl.ThrowIfNullOrEmpty(nameof(tokenUrl)), audience.ThrowIfNullOrEmpty(nameof(audience)), subjectTokenType.ThrowIfNullOrEmpty(nameof(subjectTokenType))) => SubjectTokenProvider = subjectTokenProvider.ThrowIfNull(nameof(subjectTokenProvider)); /// /// Builds and initializer for from the given initializer. /// public Initializer(Initializer other) : base(other.ThrowIfNull(nameof(other))) => SubjectTokenProvider = other.SubjectTokenProvider; /// /// Builds and initializer for from the given credential. /// public Initializer(ProgrammaticExternalAccountCredential credential) : base(credential.ThrowIfNull(nameof(credential))) => SubjectTokenProvider = credential.SubjectTokenProvider; /// /// The provider of the third-party token that's included on the STS token exchange request. /// Will not be null. /// public ISubjectTokenProvider SubjectTokenProvider { get; } } /// /// The provider of the third-party token that's included on the STS token exchange request. /// Will not be null. /// public ISubjectTokenProvider SubjectTokenProvider { get; } /// bool IGoogleCredential.HasExplicitScopes => HasExplicitScopes; /// bool IGoogleCredential.SupportsExplicitScopes => SupportsExplicitScopes; /// /// Builds a instace from the given initializer. /// public ProgrammaticExternalAccountCredential(Initializer initializer) : base(initializer.ThrowIfNull(nameof(initializer))) => SubjectTokenProvider = initializer.SubjectTokenProvider; /// protected override Task GetSubjectTokenAsyncImpl(CancellationToken taskCancellationToken) => SubjectTokenProvider.GetSubjectTokenAsync(this, taskCancellationToken); /// private protected override GoogleCredential WithoutImpersonationConfigurationImpl() => ServiceAccountImpersonationUrl is null ? new GoogleCredential(this) : new GoogleCredential(new ProgrammaticExternalAccountCredential(new Initializer(this) { ServiceAccountImpersonationUrl = null })); /// public GoogleCredential ToGoogleCredential() => new GoogleCredential(this); /// IGoogleCredential IGoogleCredential.WithQuotaProject(string quotaProject) => new ProgrammaticExternalAccountCredential(new Initializer(this) { QuotaProject = quotaProject }); /// Task IGoogleCredential.GetUniverseDomainAsync(CancellationToken cancellationToken) => Task.FromResult(UniverseDomain); /// string IGoogleCredential.GetUniverseDomain() => UniverseDomain; /// IGoogleCredential IGoogleCredential.MaybeWithScopes(IEnumerable scopes) => new ProgrammaticExternalAccountCredential(new Initializer(this) { Scopes = scopes }); /// IGoogleCredential IGoogleCredential.WithUserForDomainWideDelegation(string user) => WithUserForDomainWideDelegation(user); /// IGoogleCredential IGoogleCredential.WithHttpClientFactory(IHttpClientFactory httpClientFactory) => new ProgrammaticExternalAccountCredential(new Initializer(this) { HttpClientFactory = httpClientFactory }); /// IGoogleCredential IGoogleCredential.WithUniverseDomain(string universeDomain) => new ProgrammaticExternalAccountCredential(new Initializer(this) { UniverseDomain = universeDomain }); }