BearerTokenPolicy
A PipelinePolicy that uses an AuthenticationTokenProvider to authenticate requests.
using System.ClientModel.Internal;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace System.ClientModel.Primitives
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class BearerTokenPolicy : AuthenticationPolicy
{
private readonly AuthenticationTokenProvider _tokenProvider;
[System.Runtime.CompilerServices.Nullable(2)]
private readonly GetTokenOptions _flowContext;
public BearerTokenPolicy(AuthenticationTokenProvider tokenProvider, IEnumerable<IReadOnlyDictionary<string, object>> contexts)
{
_tokenProvider = tokenProvider;
_flowContext = GetOptionsFromContexts(contexts, tokenProvider);
}
public BearerTokenPolicy(AuthenticationTokenProvider tokenProvider, string scope)
{
_tokenProvider = tokenProvider;
_flowContext = new GetTokenOptions(new Dictionary<string, object> {
["scopes"] = new ReadOnlyMemory<string>(new string[1] {
scope
})
});
}
public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
ProcessAsync(message, pipeline, currentIndex, false).EnsureCompleted();
}
public override ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
return ProcessAsync(message, pipeline, currentIndex, true);
}
[AsyncStateMachine(typeof(<ProcessAsync>d__6))]
private ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex, bool async)
{
<ProcessAsync>d__6 stateMachine = default(<ProcessAsync>d__6);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.message = message;
stateMachine.pipeline = pipeline;
stateMachine.currentIndex = currentIndex;
stateMachine.async = async;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
internal static GetTokenOptions GetOptionsFromContexts(IEnumerable<IReadOnlyDictionary<string, object>> contexts, AuthenticationTokenProvider tokenProvider)
{
foreach (IReadOnlyDictionary<string, object> context in contexts) {
GetTokenOptions getTokenOptions = tokenProvider.CreateTokenOptions(context);
if (getTokenOptions != null)
return getTokenOptions;
}
return null;
}
}
}