Skip to content
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

initial commit for adding aadIssuerValidatorOptions #696

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/Microsoft.Identity.Web/Microsoft.Identity.Web.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Identity.Web
{
/// <summary>
/// Options passed-in to create the AadIssuerValidator object.
/// </summary>
public class AadIssuerValidatorOptions
{
/// <summary>
/// Sets the name of the HttpClient to get from the IHttpClientFactory for use with the configuration manager.
/// Needed when customizing the client such as configuring a proxy.
/// </summary>
public string? HttpClientName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web.InstanceDiscovery;
using Microsoft.IdentityModel.Protocols;

Expand All @@ -15,9 +17,30 @@ namespace Microsoft.Identity.Web.Resource
/// </summary>
internal class MicrosoftIdentityIssuerValidatorFactory
{
public MicrosoftIdentityIssuerValidatorFactory(
IOptions<AadIssuerValidatorOptions> aadIssuerValidatorOptions,
IHttpClientFactory httpClientFactory)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you need to add a package dependency for this? You at least need to register it in DI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add a package dependency. IHttpClientFactory is already used by Microsoft.Identity.Web

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, what @jmprieur said.

{
if (aadIssuerValidatorOptions?.Value?.HttpClientName != null)
{
_configManager =
new ConfigurationManager<IssuerMetadata>(
Constants.AzureADIssuerMetadataUrl,
new IssuerConfigurationRetriever(),
httpClientFactory.CreateClient(aadIssuerValidatorOptions.Value.HttpClientName));
}
else
{
_configManager =
new ConfigurationManager<IssuerMetadata>(
Constants.AzureADIssuerMetadataUrl,
new IssuerConfigurationRetriever());
}
}

private readonly IDictionary<string, AadIssuerValidator> _issuerValidators = new ConcurrentDictionary<string, AadIssuerValidator>();

private readonly ConfigurationManager<IssuerMetadata> _configManager = new ConfigurationManager<IssuerMetadata>(Constants.AzureADIssuerMetadataUrl, new IssuerConfigurationRetriever());
private readonly ConfigurationManager<IssuerMetadata> _configManager;

/// <summary>
/// Gets an <see cref="AadIssuerValidator"/> for an authority.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using System.Linq;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -160,6 +159,7 @@ private static void AddMicrosoftIdentityWebApiImplementation(
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();
builder.Services.TryAddSingleton<MicrosoftIdentityIssuerValidatorFactory>();
builder.Services.AddOptions<AadIssuerValidatorOptions>();

if (subscribeToJwtBearerMiddlewareDiagnosticsEvents)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Security.Claims;
using Microsoft.Identity.Web.Resource;
using Microsoft.Identity.Web.Test.Common;
Expand All @@ -17,10 +18,14 @@ namespace Microsoft.Identity.Web.Test.Resource
public class AadIssuerValidatorTests
{
private readonly MicrosoftIdentityIssuerValidatorFactory _issuerValidatorFactory;
private IHttpClientFactory _httpClientFactory;

public AadIssuerValidatorTests()
{
_issuerValidatorFactory = new MicrosoftIdentityIssuerValidatorFactory();
_httpClientFactory = new HttpClientFactoryTest();
_issuerValidatorFactory = new MicrosoftIdentityIssuerValidatorFactory(
null,
_httpClientFactory);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Net.Http;

namespace Microsoft.Identity.Web.Test.Resource
{
public class HttpClientFactoryTest : IHttpClientFactory
{
public Dictionary<string, HttpClient> dictionary = new Dictionary<string, HttpClient>();

public HttpClient CreateClient(string name)
{
SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler();
jennyf19 marked this conversation as resolved.
Show resolved Hide resolved
socketsHttpHandler.UseProxy = true;
return new HttpClient(socketsHttpHandler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ public void ConfigureServices(IServiceCollection services)
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
// JwtSecurityTokenHandler.DefaultMapInboundClaims = false;



// Adds Microsoft Identity platform (AAD v2.0) support to protect this Api
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();

services.AddControllers();

// below code is how customers would use a proxy
//services.Configure<AadIssuerValidatorOptions>(options => { options.HttpClientName = "cats"; });
//services.AddHttpClient("cats", c =>
//{
// // configure things here
//});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down