-
Notifications
You must be signed in to change notification settings - Fork 404
Add WithReservedScopes and WithCachePartitionKey public APIs #6014
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a59d7fd
Add WithReservedScopes and WithCachePartitionKey public APIs
iNinja 3333095
Move WithReservedScopes override to ConfidentialAuthCodeRequest
iNinja f85fe25
Simplify WithReservedScopes: remove offline_access after default scop…
iNinja 4bda100
Fix ArgumentException for empty key in WithCachePartitionKey
iNinja cecc2b4
Scope WithReservedScopes to AcquireTokenByAuthorizationCode only
iNinja 2f2855a
Fix test isolation: disable instance discovery in new tests
iNinja 95fd4f5
Use RemoveEmptyEntries in scope split to avoid empty entries
iNinja 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
145 changes: 145 additions & 0 deletions
145
tests/Microsoft.Identity.Test.Unit/PublicApiTests/WithCachePartitionKeyTests.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client; | ||
| using Microsoft.Identity.Client.ApiConfig.Parameters; | ||
| using Microsoft.Identity.Client.Extensibility; | ||
| using Microsoft.Identity.Client.OAuth2; | ||
| using Microsoft.Identity.Client.Utils; | ||
|
iNinja marked this conversation as resolved.
|
||
| using Microsoft.Identity.Test.Common.Core.Helpers; | ||
| using Microsoft.Identity.Test.Common.Core.Mocks; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Microsoft.Identity.Test.Unit.PublicApiTests | ||
| { | ||
| [TestClass] | ||
| public class WithCachePartitionKeyTests | ||
| { | ||
| [TestMethod] | ||
| public async Task WithCachePartitionKey_PopulatesCacheKeyComponents_Async() | ||
| { | ||
| // Arrange | ||
| const string cachePartitionKey = "partition_key"; | ||
| const string cachePartitionValue = "partition_value"; | ||
|
|
||
| var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) | ||
| .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true) | ||
| .WithClientSecret(TestConstants.ClientSecret) | ||
| .BuildConcrete(); | ||
|
|
||
| var builder = app.AcquireTokenForClient(TestConstants.s_scope) | ||
| .WithCachePartitionKey(cachePartitionKey, cachePartitionValue); | ||
|
|
||
| // Act | ||
| var commonParameters = GetCommonParameters(builder); | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(commonParameters.CacheKeyComponents); | ||
| Assert.HasCount(1, commonParameters.CacheKeyComponents); | ||
| Assert.IsTrue(commonParameters.CacheKeyComponents.ContainsKey(cachePartitionKey)); | ||
| var cachedValue = await commonParameters.CacheKeyComponents[cachePartitionKey](CancellationToken.None) | ||
| .ConfigureAwait(false); | ||
| Assert.AreEqual(cachePartitionValue, cachedValue); | ||
| Assert.IsNull(commonParameters.ExtraQueryParameters); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task WithCachePartitionKey_DoesNotAddToExtraQueryParameters_Async() | ||
| { | ||
| using (var httpManager = new MockHttpManager()) | ||
| { | ||
| // Arrange | ||
| const string cachePartitionKey = "partition_key"; | ||
| const string cachePartitionValue = "partition_value"; | ||
|
|
||
| var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) | ||
| .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true) | ||
| .WithClientSecret(TestConstants.ClientSecret) | ||
| .WithHttpManager(httpManager) | ||
| .WithInstanceDiscovery(false) | ||
| .BuildConcrete(); | ||
|
|
||
| var handler = httpManager.AddSuccessTokenResponseMockHandlerForPost(); | ||
| handler.UnExpectedPostData = new Dictionary<string, string> | ||
| { | ||
| { cachePartitionKey, null } | ||
| }; | ||
|
|
||
| // Act | ||
| var result = await app.AcquireTokenByAuthorizationCode(TestConstants.s_scope, TestConstants.DefaultAuthorizationCode) | ||
| .WithCachePartitionKey(cachePartitionKey, cachePartitionValue) | ||
| .ExecuteAsync() | ||
| .ConfigureAwait(false); | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WithCachePartitionKey_ThrowsOnNullOrEmptyKey() | ||
| { | ||
| // Arrange | ||
| var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) | ||
| .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true) | ||
| .WithClientSecret(TestConstants.ClientSecret) | ||
| .BuildConcrete(); | ||
|
|
||
| var builder = app.AcquireTokenForClient(TestConstants.s_scope); | ||
|
|
||
| // Act | ||
| ArgumentNullException nullKeyException = AssertException.Throws<ArgumentNullException>(() => builder.WithCachePartitionKey(null, "value")); | ||
| ArgumentException emptyKeyException = AssertException.Throws<ArgumentException>(() => builder.WithCachePartitionKey(string.Empty, "value")); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual("key", nullKeyException.ParamName); | ||
| Assert.AreEqual("key", emptyKeyException.ParamName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WithCachePartitionKey_ThrowsOnNullValue() | ||
| { | ||
| // Arrange | ||
| var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) | ||
| .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true) | ||
| .WithClientSecret(TestConstants.ClientSecret) | ||
| .BuildConcrete(); | ||
|
|
||
| var builder = app.AcquireTokenForClient(TestConstants.s_scope); | ||
|
|
||
| // Act | ||
| ArgumentNullException exception = AssertException.Throws<ArgumentNullException>(() => builder.WithCachePartitionKey("key", null)); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual("value", exception.ParamName); | ||
| } | ||
|
|
||
| private static AcquireTokenCommonParameters GetCommonParameters(object builder) | ||
| { | ||
| Type currentType = builder.GetType(); | ||
|
|
||
| while (currentType != null) | ||
| { | ||
| var commonParametersProperty = currentType.GetProperty( | ||
| "CommonParameters", | ||
| BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); | ||
|
|
||
|
iNinja marked this conversation as resolved.
|
||
| if (commonParametersProperty != null) | ||
| { | ||
| return (AcquireTokenCommonParameters)commonParametersProperty.GetValue(builder); | ||
| } | ||
|
|
||
| currentType = currentType.BaseType; | ||
| } | ||
|
|
||
| Assert.Fail("CommonParameters property was not found on the builder."); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.