Skip to content

Commit

Permalink
introduce the abstraction for tokencredential type and keycredential …
Browse files Browse the repository at this point in the history
…type (microsoft#5231)

Fixes microsoft#5235
Fixes microsoft#4410

The ClientProviderTests class contains both general client provider
features (such as service operations, query parameters, api-versions)
and auth specific features.
My PR is trying to separate them - therefore some test cases are moved
to a new class.
  • Loading branch information
ArcturusZhang authored and Mingzhe Huang (from Dev Box) committed Dec 17, 2024
1 parent e6df27f commit 9454da5
Show file tree
Hide file tree
Showing 22 changed files with 819 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ internal static class ScmKnownParameters
public static readonly ParameterProvider Data = new("data", FormattableStringHelpers.Empty, typeof(BinaryData));
public static ParameterProvider ClientOptions(CSharpType clientOptionsType)
=> new("options", $"The options for configuring the client.", clientOptionsType.WithNullable(true), initializationValue: New.Instance(clientOptionsType.WithNullable(true)));
public static readonly ParameterProvider KeyAuth = new("keyCredential", $"The token credential to copy", ClientModelPlugin.Instance.TypeFactory.KeyCredentialType);
public static readonly ParameterProvider MatchConditionsParameter = new("matchConditions", $"The content to send as the request conditions of the request.", ClientModelPlugin.Instance.TypeFactory.MatchConditionsType, DefaultOf(ClientModelPlugin.Instance.TypeFactory.MatchConditionsType));
public static readonly ParameterProvider OptionalRequestOptions = new(
ClientModelPlugin.Instance.TypeFactory.HttpRequestOptionsApi.ParameterName,
$"The request options, which can override default behaviors of the client pipeline on a per-call basis.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public abstract record ClientPipelineApi : ScopedApi, IClientPipelineApi
public abstract CSharpType ClientPipelineType { get; }
public abstract CSharpType ClientPipelineOptionsType { get; }
public abstract CSharpType PipelinePolicyType { get; }
public abstract CSharpType? KeyCredentialType { get; }
public abstract CSharpType? TokenCredentialType { get; }

protected ClientPipelineApi(Type type, ValueExpression original) : base(type, original)
{
Expand All @@ -26,7 +28,8 @@ protected ClientPipelineApi(Type type, ValueExpression original) : base(type, or

public abstract ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies);

public abstract ValueExpression AuthorizationPolicy(params ValueExpression[] arguments);
public abstract ValueExpression KeyAuthorizationPolicy(ValueExpression credential, ValueExpression headerName, ValueExpression? keyPrefix = null);
public abstract ValueExpression TokenAuthorizationPolicy(ValueExpression credential, ValueExpression scopes);
public abstract ClientPipelineApi FromExpression(ValueExpression expression);
public abstract ClientPipelineApi ToExpression();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public interface IClientPipelineApi : IExpressionApi<ClientPipelineApi>
CSharpType ClientPipelineType { get; }
CSharpType ClientPipelineOptionsType { get; }
CSharpType PipelinePolicyType { get; }

CSharpType? KeyCredentialType { get; }
CSharpType? TokenCredentialType { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Statements;
using Microsoft.Generator.CSharp.Snippets;
using Microsoft.Generator.CSharp.Statements;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

namespace Microsoft.Generator.CSharp.ClientModel.Providers
Expand All @@ -25,6 +27,10 @@ public ClientPipelineProvider(ValueExpression original) : base(typeof(ClientPipe

public override CSharpType PipelinePolicyType => typeof(PipelinePolicy);

public override CSharpType KeyCredentialType => typeof(ApiKeyCredential);

public override CSharpType? TokenCredentialType => null; // Scm library does not support token credentials yet.

public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies)
=> Static<ClientPipeline>().Invoke(nameof(ClientPipeline.Create), [options, New.Array(ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType), perRetryPolicies, New.Array(ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType)]).As<ClientPipeline>();

Expand All @@ -34,8 +40,18 @@ public override ValueExpression CreateMessage(HttpRequestOptionsApi requestOptio
public override ClientPipelineApi FromExpression(ValueExpression expression)
=> new ClientPipelineProvider(expression);

public override ValueExpression AuthorizationPolicy(params ValueExpression[] arguments)
=> Static<ApiKeyAuthenticationPolicy>().Invoke(nameof(ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy), arguments).As<ApiKeyAuthenticationPolicy>();
public override ValueExpression KeyAuthorizationPolicy(ValueExpression credential, ValueExpression headerName, ValueExpression? keyPrefix = null)
{
ValueExpression[] arguments = keyPrefix == null ? [credential, headerName] : [credential, headerName, keyPrefix];
return Static<ApiKeyAuthenticationPolicy>().Invoke(nameof(ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy), arguments).As<ApiKeyAuthenticationPolicy>();
}

public override ValueExpression TokenAuthorizationPolicy(ValueExpression credential, ValueExpression scopes)
{
// Scm library does not support token credentials yet. The throw here is intentional.
// For a plugin that supports token credentials, they could override this implementation as well as the above TokenCredentialType property.
throw new NotImplementedException();
}

public override ClientPipelineApi ToExpression() => this;

Expand Down
Loading

0 comments on commit 9454da5

Please sign in to comment.