Skip to content

Commit ca9d468

Browse files
pr comments
1 parent e4754a8 commit ca9d468

9 files changed

+23
-29
lines changed

src/client/Microsoft.Identity.Client/Internal/Requests/ManagedIdentityAuthRequest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,12 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
105105
cachedAccessTokenItem,
106106
() =>
107107
{
108-
// Use a linked token source, in case the original cts is disposed
108+
// Use a linked token source, in case the original cancellation token source is disposed before this background task completes.
109109
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
110110
return GetAccessTokenAsync(tokenSource.Token, logger);
111-
},
112-
logger,
113-
ServiceBundle,
114-
AuthenticationRequestParameters.RequestContext.ApiEvent,
115-
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
116-
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
111+
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent,
112+
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
113+
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
117114
}
118115
}
119116
catch (MsalServiceException e)
@@ -154,9 +151,12 @@ private async Task<AuthenticationResult> GetAccessTokenAsync(
154151

155152
try
156153
{
157-
// Bypass cache and send request to token endpoint, when
158-
// 1. Force refresh is requested, or
159-
// 2. If the access token needs to be refreshed proactively.
154+
// While holding the semaphore, decide whether to bypass the cache.
155+
// Re-check because another thread may have filled the cache while we waited.
156+
// Bypass when:
157+
// 1) ForceRefresh is requested
158+
// 2) Proactive refresh is in effect
159+
// 3) Claims are present (revocation flow)
160160
if (_managedIdentityParameters.ForceRefresh ||
161161
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo == CacheRefreshReason.ProactivelyRefreshed ||
162162
!string.IsNullOrEmpty(_managedIdentityParameters.Claims))

src/client/Microsoft.Identity.Client/ManagedIdentity/AbstractManagedIdentity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public virtual async Task<ManagedIdentityResponse> AuthenticateAsync(
5555
// Convert the scopes to a resource string.
5656
string resource = parameters.Resource;
5757

58-
ManagedIdentityRequest request = CreateRequest(resource, parameters);
58+
ManagedIdentityRequest request = CreateRequest(resource);
5959

6060
// Automatically add claims / capabilities if this MI source supports them
6161
if (_sourceType.SupportsClaimsAndCapabilities())
@@ -149,7 +149,7 @@ protected virtual Task<ManagedIdentityResponse> HandleResponseAsync(
149149
throw exception;
150150
}
151151

152-
protected abstract ManagedIdentityRequest CreateRequest(string resource, AcquireTokenForManagedIdentityParameters parameters);
152+
protected abstract ManagedIdentityRequest CreateRequest(string resource);
153153

154154
protected ManagedIdentityResponse GetSuccessfulResponse(HttpResponse response)
155155
{

src/client/Microsoft.Identity.Client/ManagedIdentity/AppServiceManagedIdentitySource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ private static bool TryValidateEnvVars(string msiEndpoint, ILoggerAdapter logger
6666
return true;
6767
}
6868

69-
protected override ManagedIdentityRequest CreateRequest(string resource,
70-
AcquireTokenForManagedIdentityParameters parameters)
69+
protected override ManagedIdentityRequest CreateRequest(string resource)
7170
{
7271
ManagedIdentityRequest request = new(System.Net.Http.HttpMethod.Get, _endpoint);
7372

src/client/Microsoft.Identity.Client/ManagedIdentity/AzureArcManagedIdentitySource.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ private AzureArcManagedIdentitySource(Uri endpoint, RequestContext requestContex
7979
}
8080
}
8181

82-
protected override ManagedIdentityRequest CreateRequest(string resource,
83-
AcquireTokenForManagedIdentityParameters parameters)
82+
protected override ManagedIdentityRequest CreateRequest(string resource)
8483
{
8584
ManagedIdentityRequest request = new ManagedIdentityRequest(System.Net.Http.HttpMethod.Get, _endpoint);
8685

@@ -120,7 +119,7 @@ protected override async Task<ManagedIdentityResponse> HandleResponseAsync(
120119

121120
var authHeaderValue = "Basic " + File.ReadAllText(splitChallenge[1]);
122121

123-
ManagedIdentityRequest request = CreateRequest(parameters.Resource, parameters);
122+
ManagedIdentityRequest request = CreateRequest(parameters.Resource);
124123

125124
_requestContext.Logger.Verbose(() => "[Managed Identity] Adding authorization header to the request.");
126125
request.Headers.Add("Authorization", authHeaderValue);

src/client/Microsoft.Identity.Client/ManagedIdentity/CloudShellManagedIdentitySource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ private CloudShellManagedIdentitySource(Uri endpoint, RequestContext requestCont
7474
}
7575
}
7676

77-
protected override ManagedIdentityRequest CreateRequest(string resource,
78-
AcquireTokenForManagedIdentityParameters parameters)
77+
protected override ManagedIdentityRequest CreateRequest(string resource)
7978
{
8079
ManagedIdentityRequest request = new ManagedIdentityRequest(HttpMethod.Post, _endpoint);
8180

src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsManagedIdentitySource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ internal ImdsManagedIdentitySource(RequestContext requestContext) :
5555
requestContext.Logger.Verbose(() => "[Managed Identity] Creating IMDS managed identity source. Endpoint URI: " + _imdsEndpoint);
5656
}
5757

58-
protected override ManagedIdentityRequest CreateRequest(string resource,
59-
AcquireTokenForManagedIdentityParameters parameters)
58+
protected override ManagedIdentityRequest CreateRequest(string resource)
6059
{
6160
ManagedIdentityRequest request = new(HttpMethod.Get, _imdsEndpoint);
6261

src/client/Microsoft.Identity.Client/ManagedIdentity/MachineLearningManagedIdentitySource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ private static bool TryValidateEnvVars(string msiEndpoint, ILoggerAdapter logger
6464
return true;
6565
}
6666

67-
protected override ManagedIdentityRequest CreateRequest(string resource,
68-
AcquireTokenForManagedIdentityParameters parameters)
67+
protected override ManagedIdentityRequest CreateRequest(string resource)
6968
{
7069
ManagedIdentityRequest request = new(System.Net.Http.HttpMethod.Get, _endpoint);
7170

src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentitySourceExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace Microsoft.Identity.Client.ManagedIdentity
88
internal static class ManagedIdentitySourceExtensions
99
{
1010
private static readonly HashSet<ManagedIdentitySource> s_supportsClaimsAndCaps =
11-
[
12-
// add other sources here as they light up
13-
ManagedIdentitySource.ServiceFabric,
14-
];
11+
[
12+
// add other sources here as they light up
13+
ManagedIdentitySource.ServiceFabric,
14+
];
1515

1616
internal static bool SupportsClaimsAndCapabilities(
1717
this ManagedIdentitySource source) => s_supportsClaimsAndCaps.Contains(source);

src/client/Microsoft.Identity.Client/ManagedIdentity/ServiceFabricManagedIdentitySource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ private ServiceFabricManagedIdentitySource(RequestContext requestContext, Uri en
7575
}
7676
}
7777

78-
protected override ManagedIdentityRequest CreateRequest(string resource,
79-
AcquireTokenForManagedIdentityParameters parameters)
78+
protected override ManagedIdentityRequest CreateRequest(string resource)
8079
{
8180
ManagedIdentityRequest request = new ManagedIdentityRequest(HttpMethod.Get, _endpoint);
8281

0 commit comments

Comments
 (0)