Skip to content

Commit 33c4c33

Browse files
committed
use _keyBytes to check if key has been initialized
1 parent b0c7c96 commit 33c4c33

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/Microsoft.Azure.SignalR.Common/Auth/MicrosoftEntra/MicrosoftEntraAccessKey.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private set
6060
{
6161
if (value)
6262
{
63-
LastException = new Exception("The access key has expired.");
63+
LastException = null;
6464
}
6565
_updateAt = DateTime.UtcNow;
6666
_isAuthorized = value;
@@ -73,7 +73,7 @@ private set
7373

7474
public byte[] KeyBytes => _keyBytes ?? throw new ArgumentNullException(nameof(KeyBytes));
7575

76-
internal Exception LastException { get; private set; } = new Exception("The access key has not been initialized.");
76+
internal Exception? LastException { get; private set; }
7777

7878
internal string GetAccessKeyUrl { get; }
7979

@@ -135,7 +135,7 @@ public async Task<string> GenerateAccessTokenAsync(string audience,
135135
}
136136
return Available
137137
? AuthUtility.GenerateAccessToken(KeyBytes, Kid, audience, claims, lifetime, algorithm)
138-
: throw new AzureSignalRAccessTokenNotAuthorizedException(TokenCredential, GetExceptionMessage(LastException), LastException);
138+
: throw new AzureSignalRAccessTokenNotAuthorizedException(TokenCredential, GetExceptionMessage(LastException, _keyBytes != null), LastException);
139139
}
140140

141141
internal void UpdateAccessKey(string kid, string keyStr)
@@ -199,12 +199,12 @@ private async Task UpdateAccessKeyInternalAsync(TaskCompletionSource<bool> tcs)
199199
tcs.TrySetResult(false);
200200
}
201201

202-
private static string GetExceptionMessage(Exception exception)
202+
private static string GetExceptionMessage(Exception? exception, bool initialized)
203203
{
204204
return exception switch
205205
{
206206
AzureSignalRUnauthorizedException => AzureSignalRUnauthorizedException.ErrorMessageMicrosoftEntra,
207-
_ => exception.Message,
207+
_ => exception?.Message ?? (initialized ? "Access key has expired" : "The access key has not initialized"),
208208
};
209209
}
210210

test/Microsoft.Azure.SignalR.Common.Tests/Auth/MicrosoftEntraAccessKeyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public async Task TestUpdateAccessKeyFailedThrowsNotAuthorizedException(AzureSig
133133

134134
var (kid, accessKey) = ("foo", DefaultSigningKey);
135135
key.UpdateAccessKey(kid, accessKey);
136-
Assert.Contains("has expired", key.LastException.Message);
136+
Assert.Null(key.LastException);
137137
}
138138

139139
[Theory]

0 commit comments

Comments
 (0)