-
Notifications
You must be signed in to change notification settings - Fork 236
Adding fix for OBO cache key error in long running OBO with DefaultAuthorizationHeaderProvider #3381
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
Adding fix for OBO cache key error in long running OBO with DefaultAuthorizationHeaderProvider #3381
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12a694e
Adding fix for OBO cache key error
3174675
Refactoring
cd78c79
Refactoring, updating test cases
1b493a5
Adding LRObo mock response
b61c690
Fixing test issue
3e56d61
Adding new test
1d67806
Merge branch 'master' into trwalke/OboKeyFix
trwalke 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
232 changes: 232 additions & 0 deletions
232
tests/Microsoft.Identity.Web.Test/AuthorizationHeaderProviderTests.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,232 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Identity.Abstractions; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Identity.Web.Test.Common.Mocks; | ||
using Microsoft.Identity.Web.TestOnly; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Xunit; | ||
|
||
namespace Microsoft.Identity.Web.Test | ||
{ | ||
public class AuthorizationHeaderProviderTests | ||
{ | ||
[Fact] | ||
bgavrilMS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public async Task LongRunningSessionForDefaultAuthProviderForUserDefaultKeyTest() | ||
{ | ||
// Arrange | ||
var tokenAcquirerFactory = InitTokenAcquirerFactoryForTest(); | ||
IServiceProvider serviceProvider = tokenAcquirerFactory.Build(); | ||
|
||
IAuthorizationHeaderProvider authorizationHeaderProvider = | ||
serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>(); | ||
var mockHttpClient = serviceProvider.GetRequiredService<IMsalHttpClientFactory>() as MockHttpClientFactory; | ||
|
||
using (mockHttpClient) | ||
{ | ||
// Create a test ClaimsPrincipal | ||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, "[email protected]") | ||
}; | ||
|
||
var identity = new CaseSensitiveClaimsIdentity(claims, "TestAuth"); | ||
identity.BootstrapContext = CreateTestJwt(); | ||
var claimsPrincipal = new ClaimsPrincipal(identity); | ||
|
||
// Create options with LongRunningWebApiSessionKey | ||
var options = new AuthorizationHeaderProviderOptions | ||
{ | ||
AcquireTokenOptions = new AcquireTokenOptions | ||
{ | ||
//When this is set to Auto, the first call will set the LongRunningWebApiSessionKey in the options to | ||
//the hash of the ClaimsPrincipal's access token. | ||
LongRunningWebApiSessionKey = TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto | ||
trwalke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
// Act & Assert | ||
|
||
// Step 3: First call with ClaimsPrincipal to initiate LR session | ||
var scopes = new[] { "User.Read" }; | ||
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Read")); | ||
var result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( | ||
scopes, | ||
options, | ||
claimsPrincipal); | ||
|
||
Assert.NotNull(result); | ||
Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); | ||
trwalke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string key1 = options.AcquireTokenOptions.LongRunningWebApiSessionKey; | ||
|
||
// Step 4: Second call without ClaimsPrincipal should return the token from cache | ||
result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( | ||
scopes, | ||
options); | ||
|
||
Assert.NotNull(result); | ||
Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); | ||
Assert.Equal(key1, options.AcquireTokenOptions.LongRunningWebApiSessionKey); | ||
|
||
// Step 5: First call with ClaimsPrincipal to initiate LR session for CreateAuthorizationHeaderAsync | ||
scopes = new[] { "User.Write" }; | ||
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Write")); | ||
result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( | ||
scopes, | ||
options, | ||
claimsPrincipal); | ||
|
||
Assert.NotNull(result); | ||
Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); | ||
key1 = options.AcquireTokenOptions.LongRunningWebApiSessionKey; | ||
|
||
// Step 6: Second call without ClaimsPrincipal should return the token from cache for CreateAuthorizationHeaderAsync | ||
result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( | ||
scopes, | ||
options); | ||
|
||
Assert.NotNull(result); | ||
Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); | ||
Assert.Equal(key1, options.AcquireTokenOptions.LongRunningWebApiSessionKey); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task LongRunningSessionForDefaultAuthProviderForUserTest() | ||
{ | ||
// Arrange | ||
var tokenAcquirerFactory = InitTokenAcquirerFactoryForTest(); | ||
IServiceProvider serviceProvider = tokenAcquirerFactory.Build(); | ||
|
||
IAuthorizationHeaderProvider authorizationHeaderProvider = | ||
serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>(); | ||
var mockHttpClient = serviceProvider.GetRequiredService<IMsalHttpClientFactory>() as MockHttpClientFactory; | ||
|
||
using (mockHttpClient) | ||
{ | ||
// Create a test ClaimsPrincipal | ||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, "[email protected]") | ||
}; | ||
|
||
var identity = new CaseSensitiveClaimsIdentity(claims, "TestAuth"); | ||
identity.BootstrapContext = CreateTestJwt(); | ||
var claimsPrincipal = new ClaimsPrincipal(identity); | ||
|
||
// Create options with LongRunningWebApiSessionKey | ||
var options = new AuthorizationHeaderProviderOptions | ||
{ | ||
AcquireTokenOptions = new AcquireTokenOptions | ||
{ | ||
LongRunningWebApiSessionKey = "oboKey1" | ||
} | ||
}; | ||
|
||
// Act & Assert | ||
|
||
// Step 3: First call with ClaimsPrincipal to initiate LR session | ||
var scopes = new[] { "User.Read" }; | ||
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Read")); | ||
var result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( | ||
scopes, | ||
options, | ||
claimsPrincipal); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal("oboKey1", options.AcquireTokenOptions.LongRunningWebApiSessionKey); | ||
|
||
// Step 4: Second call without ClaimsPrincipal should return the token from cache | ||
result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( | ||
scopes, | ||
options); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal("oboKey1", options.AcquireTokenOptions.LongRunningWebApiSessionKey); | ||
|
||
options = new AuthorizationHeaderProviderOptions | ||
{ | ||
AcquireTokenOptions = new AcquireTokenOptions | ||
{ | ||
LongRunningWebApiSessionKey = "oboKey2" | ||
} | ||
}; | ||
|
||
// Step 5: First call with ClaimsPrincipal to initiate LR session for CreateAuthorizationHeaderAsync | ||
scopes = new[] { "User.Write" }; | ||
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Write")); | ||
result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( | ||
scopes, | ||
options, | ||
claimsPrincipal); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal("oboKey2", options.AcquireTokenOptions.LongRunningWebApiSessionKey); | ||
|
||
// Step 6: Second call without ClaimsPrincipal should return the token from cache for CreateAuthorizationHeaderAsync | ||
result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( | ||
scopes, | ||
options); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal("oboKey2", options.AcquireTokenOptions.LongRunningWebApiSessionKey); | ||
} | ||
} | ||
|
||
private static string CreateTestJwt() | ||
{ | ||
var header = new Dictionary<string, object> | ||
{ | ||
{ "alg", "HS256" }, | ||
{ "typ", "JWT" } | ||
}; | ||
|
||
var payload = new Dictionary<string, object> | ||
{ | ||
{ "iss", "https://login.microsoftonline.com/test-tenant-id/v2.0" } | ||
}; | ||
|
||
string headerJson = System.Text.Json.JsonSerializer.Serialize(header); | ||
string payloadJson = System.Text.Json.JsonSerializer.Serialize(payload); | ||
|
||
string headerBase64 = Base64UrlEncoder.Encode(headerJson); | ||
string payloadBase64 = Base64UrlEncoder.Encode(payloadJson); | ||
|
||
// For testing purposes, we're using a fixed signature | ||
const string signature = "test_signature"; | ||
string signatureBase64 = Base64UrlEncoder.Encode(signature); | ||
|
||
return $"{headerBase64}.{payloadBase64}.{signatureBase64}"; | ||
} | ||
|
||
private TokenAcquirerFactory InitTokenAcquirerFactoryForTest() | ||
{ | ||
TokenAcquirerFactoryTesting.ResetTokenAcquirerFactoryInTest(); | ||
TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance(); | ||
tokenAcquirerFactory.Services.Configure<MicrosoftIdentityApplicationOptions>(options => | ||
{ | ||
options.Instance = "https://login.microsoftonline.com/"; | ||
options.TenantId = "testTenantId"; | ||
options.ClientId = "testClientId"; | ||
options.ClientCredentials = [ new CredentialDescription() { | ||
SourceType = CredentialSource.ClientSecret, | ||
ClientSecret = "test-secret" | ||
}]; | ||
}); | ||
|
||
// Add required services | ||
tokenAcquirerFactory.Services.AddSingleton<IMsalHttpClientFactory, MockHttpClientFactory>(); | ||
tokenAcquirerFactory.Services.AddScoped<IAuthorizationHeaderProvider, DefaultAuthorizationHeaderProvider>(); | ||
|
||
return tokenAcquirerFactory; | ||
} | ||
} | ||
} |
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.