Skip to content

Commit ead93b8

Browse files
authored
Merge branch 'master' into lozensky/AuthorityHttpsCheck
2 parents 75df8cd + b798b9a commit ead93b8

File tree

13 files changed

+209
-49
lines changed

13 files changed

+209
-49
lines changed

.github/workflows/dotnetcore.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ jobs:
4242
- name: Build solution
4343
run: msbuild Microsoft.Identity.Web.sln -r -t:build -verbosity:m -property:Configuration=Release
4444

45-
- name: Test with .NET 6.0.x
46-
run: dotnet test --no-restore --no-build Microsoft.Identity.Web.sln -f net6.0 -v normal -p:FROM_GITHUB_ACTION=true --configuration Release --filter "(FullyQualifiedName!~Microsoft.Identity.Web.Test.Integration)&(FullyQualifiedName!~WebAppUiTests)&(FullyQualifiedName!~IntegrationTests)&(FullyQualifiedName!~TokenAcquirerTests)"
47-
4845
- name: Test with .NET 8.0.x
4946
run: dotnet test --no-restore --no-build Microsoft.Identity.Web.sln -f net8.0 -v normal -p:FROM_GITHUB_ACTION=true --configuration Release --collect "Xplat Code Coverage" --filter "(FullyQualifiedName!~Microsoft.Identity.Web.Test.Integration)&(FullyQualifiedName!~WebAppUiTests)&(FullyQualifiedName!~IntegrationTests)&(FullyQualifiedName!~TokenAcquirerTests)"
5047

5148
- name: Test with .NET 9.0.x
5249
run: dotnet test --no-restore --no-build Microsoft.Identity.Web.sln -f net9.0 -v normal -p:FROM_GITHUB_ACTION=true --configuration Release --collect "Xplat Code Coverage" --filter "(FullyQualifiedName!~Microsoft.Identity.Web.Test.Integration)&(FullyQualifiedName!~WebAppUiTests)&(FullyQualifiedName!~IntegrationTests)&(FullyQualifiedName!~TokenAcquirerTests)"
5350

51+
- name: Test with .NET 6.0.x
52+
run: dotnet test Microsoft.Identity.Web.sln -f net6.0 -v normal -p:FROM_GITHUB_ACTION=true --configuration Release --filter "(FullyQualifiedName!~Microsoft.Identity.Web.Test.Integration)&(FullyQualifiedName!~WebAppUiTests)&(FullyQualifiedName!~IntegrationTests)&(FullyQualifiedName!~TokenAcquirerTests)"
53+
5454
- name: Create code coverage report
5555
run: |
5656
dotnet tool install -g dotnet-reportgenerator-globaltool --version 5.4.1

src/Microsoft.Identity.Web.TokenAcquisition/TokenAcquirerFactory.cs

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Linq;
99
using System.Reflection;
10+
using System.Threading;
1011
using Microsoft.Extensions.Configuration;
1112
using Microsoft.Extensions.DependencyInjection;
1213
using Microsoft.Identity.Abstractions;
@@ -50,6 +51,13 @@ private set
5051
}
5152
}
5253
private ServiceCollection _services = new ServiceCollection();
54+
#if NET9_0_OR_GREATER
55+
private static readonly Lock s_defaultInstanceLock = new();
56+
private readonly Lock _buildLock = new();
57+
#else
58+
private static readonly object s_defaultInstanceLock = new();
59+
private readonly object _buildLock = new();
60+
#endif
5361

5462
/// <summary>
5563
/// Constructor
@@ -78,25 +86,30 @@ protected TokenAcquirerFactory()
7886
T instance;
7987
if (defaultInstance == null)
8088
{
81-
instance = new T();
82-
instance.ReadConfiguration();
83-
defaultInstance = instance;
84-
instance.Services.AddTokenAcquisition();
85-
instance.Services.AddHttpClient();
86-
instance.Services.Configure<MicrosoftIdentityApplicationOptions>(option =>
89+
lock (s_defaultInstanceLock)
8790
{
88-
instance.Configuration.GetSection(configSection).Bind(option);
91+
if (defaultInstance == null)
92+
{
93+
instance = new T();
94+
instance.ReadConfiguration();
95+
defaultInstance = instance;
96+
instance.Services.AddTokenAcquisition();
97+
instance.Services.AddHttpClient();
98+
instance.Services.Configure<MicrosoftIdentityApplicationOptions>(option =>
99+
{
100+
instance.Configuration.GetSection(configSection).Bind(option);
89101

90-
// This is temporary and will be removed eventually.
91-
CiamAuthorityHelper.BuildCiamAuthorityIfNeeded(option);
92-
});
93-
instance.Services.AddSingleton<ITokenAcquirerFactory, DefaultTokenAcquirerFactoryImplementation>();
94-
instance.Services.AddSingleton(defaultInstance.Configuration);
102+
// This is temporary and will be removed eventually.
103+
CiamAuthorityHelper.BuildCiamAuthorityIfNeeded(option);
104+
});
105+
instance.Services.AddSingleton<ITokenAcquirerFactory, DefaultTokenAcquirerFactoryImplementation>();
106+
instance.Services.AddSingleton(defaultInstance.Configuration);
107+
}
108+
}
95109
}
96110
return (defaultInstance as T)!;
97111
}
98112

99-
100113
/// <summary>
101114
/// Get the default instance. Use this method to retrieve the instance, optionally add some services to
102115
/// the service collection, and build the instance.
@@ -116,20 +129,26 @@ static public TokenAcquirerFactory GetDefaultInstance(string configSection = "Az
116129
TokenAcquirerFactory instance;
117130
if (defaultInstance == null)
118131
{
119-
instance = new TokenAcquirerFactory();
120-
instance.ReadConfiguration();
121-
defaultInstance = instance;
122-
instance.Services.AddTokenAcquisition();
123-
instance.Services.AddHttpClient();
124-
instance.Services.Configure<MicrosoftIdentityApplicationOptions>(option =>
132+
lock (s_defaultInstanceLock)
125133
{
126-
instance.Configuration.GetSection(configSection).Bind(option);
134+
if (defaultInstance == null)
135+
{
136+
instance = new TokenAcquirerFactory();
137+
instance.ReadConfiguration();
138+
defaultInstance = instance;
139+
instance.Services.AddTokenAcquisition();
140+
instance.Services.AddHttpClient();
141+
instance.Services.Configure<MicrosoftIdentityApplicationOptions>(option =>
142+
{
143+
instance.Configuration.GetSection(configSection).Bind(option);
127144

128-
// This is temporary and will be removed eventually.
129-
CiamAuthorityHelper.BuildCiamAuthorityIfNeeded(option);
130-
});
131-
instance.Services.AddSingleton<ITokenAcquirerFactory, DefaultTokenAcquirerFactoryImplementation>();
132-
instance.Services.AddSingleton(defaultInstance.Configuration);
145+
// This is temporary and will be removed eventually.
146+
CiamAuthorityHelper.BuildCiamAuthorityIfNeeded(option);
147+
});
148+
instance.Services.AddSingleton<ITokenAcquirerFactory, DefaultTokenAcquirerFactoryImplementation>();
149+
instance.Services.AddSingleton(defaultInstance.Configuration);
150+
}
151+
}
133152
}
134153
return defaultInstance!;
135154
}
@@ -157,9 +176,18 @@ public IServiceProvider Build()
157176
throw new InvalidOperationException("You shouldn't call Build() twice");
158177
}
159178

160-
// Additional processing before creating the service provider
161-
PreBuild();
162-
ServiceProvider = Services.BuildServiceProvider();
179+
lock(_buildLock)
180+
{
181+
if (ServiceProvider != null)
182+
{
183+
throw new InvalidOperationException("You shouldn't call Build() twice");
184+
}
185+
186+
// Additional processing before creating the service provider
187+
PreBuild();
188+
ServiceProvider = Services.BuildServiceProvider();
189+
}
190+
163191
return ServiceProvider;
164192
}
165193

tests/DevApps/aspnet-mvc/OwinWebApi/Web.config

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
</dependentAssembly>
5959
<dependentAssembly>
6060
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
61-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
61+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
6262
</dependentAssembly>
6363
<dependentAssembly>
6464
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
65-
<bindingRedirect oldVersion="0.0.0.0-6.0.0.2" newVersion="6.0.0.2"/>
65+
<bindingRedirect oldVersion="0.0.0.0-6.0.0.1" newVersion="6.0.0.1"/>
6666
</dependentAssembly>
6767
<dependentAssembly>
6868
<assemblyIdentity name="System.Buffers" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
@@ -74,31 +74,31 @@
7474
</dependentAssembly>
7575
<dependentAssembly>
7676
<assemblyIdentity name="Microsoft.IdentityModel.Tokens" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
77-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
77+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
7878
</dependentAssembly>
7979
<dependentAssembly>
8080
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.WsFederation" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
8181
<bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0"/>
8282
</dependentAssembly>
8383
<dependentAssembly>
8484
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.OpenIdConnect" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
85-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
85+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
8686
</dependentAssembly>
8787
<dependentAssembly>
8888
<assemblyIdentity name="Microsoft.IdentityModel.Protocols" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
89-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
89+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
9090
</dependentAssembly>
9191
<dependentAssembly>
9292
<assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
93-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
93+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
9494
</dependentAssembly>
9595
<dependentAssembly>
9696
<assemblyIdentity name="Microsoft.IdentityModel.Abstractions" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
97-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
97+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
9898
</dependentAssembly>
9999
<dependentAssembly>
100100
<assemblyIdentity name="Microsoft.Identity.Client" publicKeyToken="0A613F4DD989E8AE" culture="neutral"/>
101-
<bindingRedirect oldVersion="0.0.0.0-4.68.0.0" newVersion="4.68.0.0"/>
101+
<bindingRedirect oldVersion="0.0.0.0-4.67.2.0" newVersion="4.67.2.0"/>
102102
</dependentAssembly>
103103
<dependentAssembly>
104104
<assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="ADB9793829DDAE60" culture="neutral"/>

tests/DevApps/aspnet-mvc/OwinWebApp/Web.config

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
</dependentAssembly>
6060
<dependentAssembly>
6161
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
62-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
62+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
6363
</dependentAssembly>
6464
<dependentAssembly>
6565
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
66-
<bindingRedirect oldVersion="0.0.0.0-6.0.0.2" newVersion="6.0.0.2"/>
66+
<bindingRedirect oldVersion="0.0.0.0-6.0.0.1" newVersion="6.0.0.1"/>
6767
</dependentAssembly>
6868
<dependentAssembly>
6969
<assemblyIdentity name="System.Buffers" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
@@ -75,31 +75,31 @@
7575
</dependentAssembly>
7676
<dependentAssembly>
7777
<assemblyIdentity name="Microsoft.IdentityModel.Tokens" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
78-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
78+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
7979
</dependentAssembly>
8080
<dependentAssembly>
8181
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.WsFederation" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
8282
<bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0"/>
8383
</dependentAssembly>
8484
<dependentAssembly>
8585
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.OpenIdConnect" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
86-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
86+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
8787
</dependentAssembly>
8888
<dependentAssembly>
8989
<assemblyIdentity name="Microsoft.IdentityModel.Protocols" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
90-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
90+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
9191
</dependentAssembly>
9292
<dependentAssembly>
9393
<assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
94-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
94+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
9595
</dependentAssembly>
9696
<dependentAssembly>
9797
<assemblyIdentity name="Microsoft.IdentityModel.Abstractions" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
98-
<bindingRedirect oldVersion="0.0.0.0-8.6.0.0" newVersion="8.6.0.0"/>
98+
<bindingRedirect oldVersion="0.0.0.0-8.3.1.0" newVersion="8.3.1.0"/>
9999
</dependentAssembly>
100100
<dependentAssembly>
101101
<assemblyIdentity name="Microsoft.Identity.Client" publicKeyToken="0A613F4DD989E8AE" culture="neutral"/>
102-
<bindingRedirect oldVersion="0.0.0.0-4.68.0.0" newVersion="4.68.0.0"/>
102+
<bindingRedirect oldVersion="0.0.0.0-4.67.2.0" newVersion="4.67.2.0"/>
103103
</dependentAssembly>
104104
<dependentAssembly>
105105
<assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="ADB9793829DDAE60" culture="neutral"/>

tests/E2E Tests/CustomSignedAssertionProviderTests/CustomSignedAssertionProviderExtensibilityTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
using Microsoft.Identity.Abstractions;
99
using Microsoft.Identity.Client;
1010
using Microsoft.Identity.Web;
11+
using Microsoft.Identity.Web.Test.Common;
1112
using Xunit.Sdk;
1213

1314

1415
namespace CustomSignedAssertionProviderTests
1516
{
17+
[Collection(nameof(TokenAcquirerFactorySingletonProtection))]
1618
public class CustomSignedAssertionProviderExtensibilityTests
1719
{
1820
[Fact]

tests/E2E Tests/CustomSignedAssertionProviderTests/CustomSignedAssertionProviderTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="..\..\..\src\Microsoft.Identity.Web\Microsoft.Identity.Web.csproj" />
2929
<ProjectReference Include="..\..\..\src\Microsoft.Identity.Web.Certificate\Microsoft.Identity.Web.Certificate.csproj" />
3030
<ProjectReference Include="..\..\..\src\Microsoft.Identity.Web.Certificateless\Microsoft.Identity.Web.Certificateless.csproj" />
31+
<ProjectReference Include="..\..\Microsoft.Identity.Web.Test.Common\Microsoft.Identity.Web.Test.Common.csproj" />
3132
</ItemGroup>
3233

3334
<ItemGroup>

tests/E2E Tests/TokenAcquirerTests/CertificateRotationTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace TokenAcquirerTests
2121
{
22+
[Collection(nameof(TokenAcquirerFactorySingletonProtection))]
2223
public sealed class CertificateRotationTest : ICertificatesObserver
2324
{
2425
const string MicrosoftGraphAppId = "00000003-0000-0000-c000-000000000000";

tests/E2E Tests/TokenAcquirerTests/TokenAcquirer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace TokenAcquirerTests
2525
{
26+
[CollectionDefinition(nameof(TokenAcquirerFactorySingletonProtection))]
2627
#if !FROM_GITHUB_ACTION
2728
public class TokenAcquirer
2829
{

tests/Microsoft.Identity.Web.Test.Common/Microsoft.Identity.Web.Test.Common.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net462; net472; net6.0; net8.0; net9.0</TargetFrameworks>
@@ -17,7 +17,6 @@
1717
<PackageReference Include="Microsoft.Identity.Lab.Api" Version="$(MicrosoftIdentityLabApiVersion)" />
1818
<PackageReference Include="NSubstitute" Version="$(NSubstituteVersion)" />
1919
<PackageReference Include="xunit" Version="$(XunitVersion)" />
20-
<PackageReference Include="xunit.assert" Version="$(XunitAssertVersion)" />
2120
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
2221
<PackageReference Include="System.Net.Http" Version="$(SystemNetHttpVersion)" />
2322
<PackageReference Include="System.Text.RegularExpressions" Version="$(SystemTextRegularExpressions)" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.Identity.Web.Test.Common
7+
{
8+
[CollectionDefinition(nameof(TokenAcquirerFactorySingletonProtection))]
9+
public class TokenAcquirerFactorySingletonProtection
10+
{
11+
// This class has no code, and is never created. Its purpose is to prevent test classes using the
12+
// static singleton DefaultTokenAcquirerFactory from running in parallel as some tests modify this singleton.
13+
}
14+
}

0 commit comments

Comments
 (0)