| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using Azure.Core; |
| | 9 | | using Azure.Core.Diagnostics; |
| | 10 | | using Azure.Core.Pipeline; |
| | 11 | | using Microsoft.Identity.Client; |
| | 12 | |
|
| | 13 | | namespace Azure.Identity |
| | 14 | | { |
| | 15 | | internal class CredentialPipeline |
| | 16 | | { |
| 4 | 17 | | private static readonly Lazy<CredentialPipeline> s_singleton = new Lazy<CredentialPipeline>(() => new Credential |
| | 18 | |
|
| | 19 | | private readonly IScopeHandler _defaultScopeHandler; |
| | 20 | | private IScopeHandler _groupScopeHandler; |
| | 21 | |
|
| 194 | 22 | | private CredentialPipeline(TokenCredentialOptions options) |
| | 23 | | { |
| 194 | 24 | | AuthorityHost = options.AuthorityHost; |
| | 25 | |
|
| 194 | 26 | | HttpPipeline = HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), Array.Empty<HttpPipelin |
| | 27 | |
|
| 194 | 28 | | Diagnostics = new ClientDiagnostics(options); |
| | 29 | |
|
| 194 | 30 | | _defaultScopeHandler = new ScopeHandler(Diagnostics); |
| 194 | 31 | | } |
| | 32 | |
|
| | 33 | | public static CredentialPipeline GetInstance(TokenCredentialOptions options) |
| | 34 | | { |
| 1870 | 35 | | return options is null ? s_singleton.Value : new CredentialPipeline(options); |
| | 36 | | } |
| | 37 | |
|
| 160 | 38 | | public Uri AuthorityHost { get; } |
| | 39 | |
|
| 184 | 40 | | public HttpPipeline HttpPipeline { get; } |
| | 41 | |
|
| 934 | 42 | | public ClientDiagnostics Diagnostics { get; } |
| | 43 | |
|
| | 44 | | public IConfidentialClientApplication CreateMsalConfidentialClient(string tenantId, string clientId, string clie |
| | 45 | | { |
| 0 | 46 | | return ConfidentialClientApplicationBuilder.Create(clientId).WithHttpClientFactory(new HttpPipelineClientFac |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public CredentialDiagnosticScope StartGetTokenScope(string fullyQualifiedMethod, TokenRequestContext context) |
| | 50 | | { |
| 658 | 51 | | IScopeHandler scopeHandler = _groupScopeHandler ?? _defaultScopeHandler; |
| | 52 | |
|
| 658 | 53 | | CredentialDiagnosticScope scope = new CredentialDiagnosticScope(fullyQualifiedMethod, context, scopeHandler) |
| 654 | 54 | | scope.Start(); |
| 654 | 55 | | return scope; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public CredentialDiagnosticScope StartGetTokenScopeGroup(string fullyQualifiedMethod, TokenRequestContext contex |
| | 59 | | { |
| 692 | 60 | | var scopeHandler = new ScopeGroupHandler(this, fullyQualifiedMethod); |
| | 61 | |
|
| 692 | 62 | | CredentialDiagnosticScope scope = new CredentialDiagnosticScope(fullyQualifiedMethod, context, scopeHandler) |
| 692 | 63 | | scope.Start(); |
| 692 | 64 | | return scope; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private class CredentialResponseClassifier : ResponseClassifier |
| | 68 | | { |
| | 69 | | public override bool IsRetriableResponse(HttpMessage message) |
| | 70 | | { |
| 28 | 71 | | return base.IsRetriableResponse(message) || message.Response.Status == 404; |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | private class ScopeHandler : IScopeHandler |
| | 76 | | { |
| | 77 | | private readonly ClientDiagnostics _diagnostics; |
| | 78 | |
|
| 194 | 79 | | public ScopeHandler(ClientDiagnostics diagnostics) |
| | 80 | | { |
| 194 | 81 | | _diagnostics = diagnostics; |
| 194 | 82 | | } |
| | 83 | |
|
| 552 | 84 | | public DiagnosticScope CreateScope(string name) => _diagnostics.CreateScope(name); |
| 552 | 85 | | public void Start(string name, in DiagnosticScope scope) => scope.Start(); |
| 552 | 86 | | public void Dispose(string name, in DiagnosticScope scope) => scope.Dispose(); |
| 294 | 87 | | public void Fail(string name, in DiagnosticScope scope, Exception exception) => scope.Failed(exception); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private class ScopeGroupHandler : IScopeHandler |
| | 91 | | { |
| | 92 | | private readonly CredentialPipeline _pipeline; |
| | 93 | | private readonly string _groupName; |
| | 94 | | private Dictionary<string, (DateTime StartDateTime, Exception Exception)> _childScopes; |
| | 95 | |
|
| 692 | 96 | | public ScopeGroupHandler(CredentialPipeline pipeline, string groupName) |
| | 97 | | { |
| 692 | 98 | | _pipeline = pipeline; |
| 692 | 99 | | _groupName = groupName; |
| 692 | 100 | | } |
| | 101 | |
|
| | 102 | | public DiagnosticScope CreateScope(string name) |
| | 103 | | { |
| 798 | 104 | | if (IsGroup(name)) |
| | 105 | | { |
| 692 | 106 | | _pipeline._groupScopeHandler = this; |
| 692 | 107 | | return _pipeline.Diagnostics.CreateScope(name); |
| | 108 | | } |
| | 109 | |
|
| 106 | 110 | | _childScopes ??= new Dictionary<string, (DateTime startDateTime, Exception exception)>(); |
| 106 | 111 | | _childScopes[name] = default; |
| 102 | 112 | | return default; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public void Start(string name, in DiagnosticScope scope) |
| | 116 | | { |
| 794 | 117 | | if (IsGroup(name)) |
| | 118 | | { |
| 692 | 119 | | scope.Start(); |
| | 120 | | } |
| | 121 | | else |
| | 122 | | { |
| 102 | 123 | | _childScopes[name] = (DateTime.UtcNow, default); |
| | 124 | | } |
| 102 | 125 | | } |
| | 126 | |
|
| | 127 | | public void Dispose(string name, in DiagnosticScope scope) |
| | 128 | | { |
| 794 | 129 | | if (!IsGroup(name)) |
| | 130 | | { |
| 102 | 131 | | return; |
| | 132 | | } |
| | 133 | |
|
| 692 | 134 | | if (_childScopes != null) |
| | 135 | | { |
| 136 | 136 | | var succeededScope = _childScopes.LastOrDefault(kvp => kvp.Value.Exception == default); |
| 42 | 137 | | if (succeededScope.Key != default) |
| | 138 | | { |
| 20 | 139 | | SucceedChildScope(succeededScope.Key, succeededScope.Value.StartDateTime); |
| | 140 | | } |
| | 141 | | } |
| | 142 | |
|
| 692 | 143 | | scope.Dispose(); |
| 692 | 144 | | _pipeline._groupScopeHandler = default; |
| 692 | 145 | | } |
| | 146 | |
|
| | 147 | | public void Fail(string name, in DiagnosticScope scope, Exception exception) |
| | 148 | | { |
| 624 | 149 | | if (_childScopes == default) |
| | 150 | | { |
| 542 | 151 | | scope.Failed(exception); |
| 542 | 152 | | return; |
| | 153 | | } |
| | 154 | |
|
| 82 | 155 | | if (IsGroup(name)) |
| | 156 | | { |
| 8 | 157 | | if (exception is OperationCanceledException) |
| | 158 | | { |
| 0 | 159 | | var canceledScope = _childScopes.Last(kvp => kvp.Value.Exception == exception); |
| 0 | 160 | | FailChildScope(canceledScope.Key, canceledScope.Value.StartDateTime, canceledScope.Value.Excepti |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 72 | 164 | | foreach (var childScope in _childScopes) |
| | 165 | | { |
| 28 | 166 | | FailChildScope(childScope.Key, childScope.Value.StartDateTime, childScope.Value.Exception); |
| | 167 | | } |
| | 168 | | } |
| | 169 | |
|
| 8 | 170 | | scope.Failed(exception); |
| | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 74 | 174 | | _childScopes[name] = (_childScopes[name].StartDateTime, exception); |
| | 175 | | } |
| 74 | 176 | | } |
| | 177 | |
|
| | 178 | | private void SucceedChildScope(string name, DateTime dateTime) |
| | 179 | | { |
| 20 | 180 | | using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope(name); |
| 20 | 181 | | scope.SetStartTime(dateTime); |
| 20 | 182 | | scope.Start(); |
| 40 | 183 | | } |
| | 184 | |
|
| | 185 | | private void FailChildScope(string name, DateTime dateTime, Exception exception) |
| | 186 | | { |
| 28 | 187 | | using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope(name); |
| 28 | 188 | | scope.SetStartTime(dateTime); |
| 28 | 189 | | scope.Start(); |
| 28 | 190 | | scope.Failed(exception); |
| 56 | 191 | | } |
| | 192 | |
|
| 2468 | 193 | | private bool IsGroup(string name) => string.Equals(name, _groupName, StringComparison.Ordinal); |
| | 194 | | } |
| | 195 | | } |
| | 196 | | } |