Skip to content

Commit

Permalink
use _keyBytes to check if key has been initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
terencefan committed Dec 10, 2024
1 parent b0c7c96 commit 4b50e80
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private set
{
if (value)
{
LastException = new Exception("The access key has expired.");
LastException = null;
}
_updateAt = DateTime.UtcNow;
_isAuthorized = value;
Expand All @@ -73,7 +73,7 @@ private set

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

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

internal string GetAccessKeyUrl { get; }

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

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

private static string GetExceptionMessage(Exception exception)
private static string GetExceptionMessage(Exception? exception, bool initialized)
{
return exception switch
{
AzureSignalRUnauthorizedException => AzureSignalRUnauthorizedException.ErrorMessageMicrosoftEntra,
_ => exception.Message,
_ => exception?.Message ?? (initialized ? "The access key has expired." : "The access key has not been initialized."),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public async Task TestUpdateAccessKeyFailedThrowsNotAuthorizedException(AzureSig

var (kid, accessKey) = ("foo", DefaultSigningKey);
key.UpdateAccessKey(kid, accessKey);
Assert.Contains("has expired", key.LastException.Message);
Assert.Null(key.LastException);
}

[Theory]
Expand Down

0 comments on commit 4b50e80

Please sign in to comment.