Skip to content

Commit a258d1b

Browse files
authored
Merge pull request #164 from ovska/fix-nonawaited-task
Fix unawaited task in UpdateClientAssertion event
2 parents 09e31bc + 8ced2ac commit a258d1b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/OAuth2IntrospectionHandler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private static async Task<TokenIntrospectionResponse> LoadClaimsForToken(
178178
OAuth2IntrospectionOptions options)
179179
{
180180
var introspectionClient = await options.IntrospectionClient.Value.ConfigureAwait(false);
181-
using var request = CreateTokenIntrospectionRequest(token, context, scheme, events, options);
181+
using var request = await CreateTokenIntrospectionRequest(token, context, scheme, events, options);
182182

183183
var requestSendingContext = new SendingRequestContext(context, scheme, options)
184184
{
@@ -190,7 +190,7 @@ private static async Task<TokenIntrospectionResponse> LoadClaimsForToken(
190190
return await introspectionClient.IntrospectTokenAsync(request).ConfigureAwait(false);
191191
}
192192

193-
private static TokenIntrospectionRequest CreateTokenIntrospectionRequest(
193+
private static async ValueTask<TokenIntrospectionRequest> CreateTokenIntrospectionRequest(
194194
string token,
195195
HttpContext context,
196196
AuthenticationScheme scheme,
@@ -199,7 +199,9 @@ private static TokenIntrospectionRequest CreateTokenIntrospectionRequest(
199199
{
200200
if (options.ClientSecret == null && options.ClientAssertionExpirationTime <= DateTime.UtcNow)
201201
{
202-
lock (options.AssertionUpdateLockObj)
202+
await options.AssertionUpdateLock.WaitAsync();
203+
204+
try
203205
{
204206
if (options.ClientAssertionExpirationTime <= DateTime.UtcNow)
205207
{
@@ -208,13 +210,17 @@ private static TokenIntrospectionRequest CreateTokenIntrospectionRequest(
208210
ClientAssertion = options.ClientAssertion ?? new ClientAssertion()
209211
};
210212

211-
events.UpdateClientAssertion(updateClientAssertionContext);
213+
await events.UpdateClientAssertion(updateClientAssertionContext);
212214

213215
options.ClientAssertion = updateClientAssertionContext.ClientAssertion;
214216
options.ClientAssertionExpirationTime =
215217
updateClientAssertionContext.ClientAssertionExpirationTime;
216218
}
217219
}
220+
finally
221+
{
222+
options.AssertionUpdateLock.Release();
223+
}
218224
}
219225

220226
return new TokenIntrospectionRequest

src/OAuth2IntrospectionOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Http;
88
using System;
99
using System.Net.Http;
10+
using System.Threading;
1011

1112
namespace IdentityModel.AspNetCore.OAuth2Introspection
1213
{
@@ -45,7 +46,7 @@ public OAuth2IntrospectionOptions()
4546
/// </summary>
4647
public string ClientSecret { get; set; }
4748

48-
internal object AssertionUpdateLockObj = new object();
49+
internal readonly SemaphoreSlim AssertionUpdateLock = new SemaphoreSlim(1, 1);
4950

5051
internal ClientAssertion ClientAssertion { get; set; }
5152

0 commit comments

Comments
 (0)