This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
fix null ref in tokenCache when UserInfo is absent #1607
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ | |
using Microsoft.Identity.Core; | ||
using Microsoft.Identity.Core.Cache; | ||
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Cache; | ||
using Test.ADAL.NET.Common.Mocks; | ||
using System; | ||
|
||
namespace Test.ADAL.NET.Unit | ||
{ | ||
|
@@ -186,11 +188,95 @@ public void ParallelStoreTest() | |
} | ||
|
||
[TestMethod] | ||
[Description("Test to ensure the token cache doesnt throw an exception when cleared")] | ||
[Description("Test to ensure the token cache doesn't throw an exception when cleared")] | ||
[TestCategory("AdalDotNet.Unit")] | ||
public void TokenCacheClearTest() | ||
{ | ||
TokenCacheTests.TokenCacheClearTest(File.ReadAllBytes("oldcache.serialized")); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Test to ensure a null IdToken is handled correctly")] | ||
[TestCategory("AdalDotNet.Unit")] | ||
public void NullIdTokenCacheTest() | ||
{ | ||
// arrange & act | ||
TokenCache tokenCache = new TokenCache(); | ||
|
||
var result = CreateAdalResultWithIdToken(false); | ||
|
||
WriteMsalRefreshTokenValues(result, tokenCache.TokenCacheAccessor); | ||
|
||
// assert | ||
// no IdToken present, CacheFallbackOperations exits early | ||
Assert.AreEqual(tokenCache.TokenCacheAccessor.AccountCount, 0); | ||
Assert.AreEqual(tokenCache.TokenCacheAccessor.RefreshTokenCount, 0); | ||
Assert.IsNull(result.Result.UserInfo); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Test to ensure WriteMsalRefreshToken handles presence of IdToken correctly")] | ||
[TestCategory("AdalDotNet.Unit")] | ||
public void IdTokenReturnedCacheTest() | ||
{ | ||
// arrange | ||
TokenCache tokenCache = new TokenCache(); | ||
|
||
var result = CreateAdalResultWithIdToken(true); | ||
|
||
// act | ||
WriteMsalRefreshTokenValues(result, tokenCache.TokenCacheAccessor); | ||
|
||
// assert | ||
// IdToken present | ||
Assert.AreEqual(tokenCache.TokenCacheAccessor.AccountCount, 1); | ||
Assert.AreEqual(tokenCache.TokenCacheAccessor.RefreshTokenCount, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can it be tested that the userinfo values are null? #Closed |
||
Assert.AreEqual(AdalTestConstants.DefaultDisplayableId, result.Result.UserInfo.DisplayableId); | ||
Assert.AreEqual(AdalTestConstants.DefaultUniqueId, result.Result.UserInfo.UniqueId); | ||
} | ||
|
||
private AdalResultWrapper CreateAdalResultWithIdToken(bool withIdToken) | ||
{ | ||
var result = new AdalResultWrapper | ||
{ | ||
RefreshToken = "some-rt", | ||
RawClientInfo = MockHelpers.CreateClientInfo(AdalTestConstants.DefaultUniqueId, AdalTestConstants.DefaultUniqueTenantIdentifier), | ||
ResourceInResponse = AdalTestConstants.DefaultResource | ||
}; | ||
|
||
if (withIdToken) | ||
{ | ||
result.Result = new AdalResult("Bearer", "some-access-token", DateTimeOffset.UtcNow, DateTimeOffset.UtcNow + TimeSpan.FromMinutes(180)) | ||
{ | ||
UserInfo = new AdalUserInfo() | ||
{ | ||
DisplayableId = AdalTestConstants.DefaultDisplayableId, | ||
UniqueId = AdalTestConstants.DefaultUniqueId | ||
}, | ||
IdToken = MockHelpers.CreateAdalIdToken( | ||
AdalTestConstants.DefaultUniqueId, | ||
AdalTestConstants.DefaultDisplayableId) | ||
}; | ||
} | ||
else | ||
{ | ||
result.Result = new AdalResult("Bearer", "some-access-token", DateTimeOffset.UtcNow, DateTimeOffset.UtcNow + TimeSpan.FromMinutes(180)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private void WriteMsalRefreshTokenValues(AdalResultWrapper result, ITokenCacheAccessor tokenCacheAccessor) | ||
{ | ||
CacheFallbackOperations.WriteMsalRefreshToken( | ||
tokenCacheAccessor, | ||
result, | ||
AdalTestConstants.DefaultAuthorityCommonTenant, | ||
AdalTestConstants.DefaultClientId, | ||
result.Result.UserInfo?.DisplayableId, | ||
result.Result.UserInfo?.GivenName, | ||
result.Result.UserInfo?.FamilyName, | ||
result.Result.UserInfo?.UniqueId); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding ? is the actual fix. the customer was getting a null ref because UserInfo is null here. There will no longer be a null ref here.
#Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it then possible to complete the scenario? I believe the MSAL cache will now be empty and thus only when using the ADAL cache will there be any values in the cache... thus depending on how the customer persists the cache (which serializer/deserializer) this may or may not work? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right. if there is no id_token, then we will exit out of writing items to the MSAL cache...see here however, i'm assuming, since this has been out for awhile, that it hasn't been very impactful, or our customers are not using the cache in that way?
In reply to: 288842460 [](ancestors = 288842460)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed the comments. lmk if you'd like to discuss anything further. thanks for the utest suggestions and improvements.
In reply to: 288855740 [](ancestors = 288855740,288842460)