Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cross platform session token cache global locking #470

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CredentialProvider.Microsoft/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static async Task<int> Main(string[] args)
{
IRequestHandlers requestHandlers = new RequestHandlerCollection
{
{ MessageMethod.GetAuthenticationCredentials, new GetAuthenticationCredentialsRequestHandler(multiLogger, credentialProviders) },
{ MessageMethod.GetAuthenticationCredentials, new GetAuthenticationCredentialsRequestHandler(multiLogger, credentialProviders, tokenSource.Token) },
{ MessageMethod.GetOperationClaims, new GetOperationClaimsRequestHandler(multiLogger, credentialProviders) },
{ MessageMethod.Initialize, new InitializeRequestHandler(multiLogger) },
{ MessageMethod.SetLogLevel, new SetLogLevelRequestHandler(multiLogger) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public GetAuthenticationCredentialsRequestHandler(ILogger logger, IReadOnlyColle
this.cache = cache;
}

public GetAuthenticationCredentialsRequestHandler(ILogger logger, IReadOnlyCollection<ICredentialProvider> credentialProviders)
public GetAuthenticationCredentialsRequestHandler(ILogger logger, IReadOnlyCollection<ICredentialProvider> credentialProviders, CancellationToken cancellationToken)
: this(logger, credentialProviders, null)
{
this.cache = GetSessionTokenCache(logger, CancellationToken);
this.cache = GetSessionTokenCache(logger, cancellationToken);
}

public override async Task<GetAuthenticationCredentialsResponse> HandleRequestAsync(GetAuthenticationCredentialsRequest request)
Expand Down
3 changes: 3 additions & 0 deletions CredentialProvider.Microsoft/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,7 @@ Provide MSAL Cache Location
<data name="SessionTokenCacheWriteFail" xml:space="preserve">
<value>Unable to write token to credential cache. Exception: {0}, Message: {1}</value>
</data>
<data name="SessionTokenCacheCancelMessage" xml:space="preserve">
<value>Canceling SessionToken cache operation.</value>
</data>
</root>
47 changes: 18 additions & 29 deletions CredentialProvider.Microsoft/Util/SessionTokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,13 @@ private Dictionary<string, string> Cache
// We couldn't get the mutex on our first acquisition attempt. Log this so the user knows what we're
// waiting on.
logger.Verbose(Resources.SessionTokenCacheMutexMiss);

int index = WaitHandle.WaitAny(new WaitHandle[] { mutex, this.cancellationToken.WaitHandle }, -1);

if (index == 1)
{
logger.Verbose(Resources.CancelMessage);
return new Dictionary<string, string>();
}
else if (index == WaitHandle.WaitTimeout)
while (!mutex.WaitOne(100))
{
logger.Verbose(Resources.SessionTokenCacheMutexFail);
return new Dictionary<string, string>();
if (this.cancellationToken.IsCancellationRequested)
{
logger.Verbose(Resources.SessionTokenCacheCancelMessage);
return new Dictionary<string, string>();
}
}
}
}
Expand Down Expand Up @@ -95,16 +90,13 @@ public string this[Uri key]
// We couldn't get the mutex on our first acquisition attempt. Log this so the user knows what we're
// waiting on.
logger.Verbose(Resources.SessionTokenCacheMutexMiss);

int index = WaitHandle.WaitAny(new WaitHandle[] { mutex, this.cancellationToken.WaitHandle }, -1);

if (index == 1)
{
logger.Verbose(Resources.CancelMessage);
}
else if (index == WaitHandle.WaitTimeout)
while (!mutex.WaitOne(100))
{
logger.Verbose(Resources.SessionTokenCacheMutexFail);
if (this.cancellationToken.IsCancellationRequested)
{
logger.Verbose(Resources.SessionTokenCacheCancelMessage);
return;
}
}
}
}
Expand Down Expand Up @@ -170,16 +162,13 @@ public void Remove(Uri key)
// We couldn't get the mutex on our first acquisition attempt. Log this so the user knows what we're
// waiting on.
logger.Verbose(Resources.SessionTokenCacheMutexMiss);

int index = WaitHandle.WaitAny(new WaitHandle[] { mutex, this.cancellationToken.WaitHandle }, -1);

if (index == 1)
while (!mutex.WaitOne(100))
{
logger.Verbose(Resources.CancelMessage);
}
else if (index == WaitHandle.WaitTimeout)
{
logger.Verbose(Resources.SessionTokenCacheMutexFail);
if (this.cancellationToken.IsCancellationRequested)
{
logger.Verbose(Resources.SessionTokenCacheCancelMessage);
return;
}
}
}
}
Expand Down