Skip to content

Commit 610c9d8

Browse files
Internal and External auth cannot be enabled at the same time, redirect
1 parent 0b9f1bc commit 610c9d8

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

CometServer.Tests/Configuration/AppConfigServiceTestFixture.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ namespace CometServer.Tests.Configuration
2626
{
2727
using System.IO;
2828

29-
using CDP4Authentication;
30-
3129
using CometServer.Authentication.Basic;
3230
using CometServer.Authentication.Bearer;
3331
using CometServer.Configuration;
3432

3533
using Microsoft.Extensions.Configuration;
3634

37-
using Moq;
38-
3935
using NUnit.Framework;
4036

4137
/// <summary>
@@ -45,7 +41,6 @@ namespace CometServer.Tests.Configuration
4541
public class AppConfigServiceTestFixture
4642
{
4743
private IConfiguration configuration;
48-
private Mock<IAuthenticationPluginInjector> pluginInjector;
4944

5045
[SetUp]
5146
public void SetUp()
@@ -59,15 +54,12 @@ public void SetUp()
5954

6055
// Build the IConfiguration instance
6156
this.configuration = configurationBuilder.Build();
62-
63-
this.pluginInjector = new Mock<IAuthenticationPluginInjector>();
64-
this.pluginInjector.Setup(x => x.Connectors).Returns([]);
6557
}
6658

6759
[Test]
6860
public void Verify_that_configuration_is_loaded_from_appsettings()
6961
{
70-
var appConfigService = new AppConfigService(this.configuration, this.pluginInjector.Object);
62+
var appConfigService = new AppConfigService(this.configuration);
7163

7264
Assert.Multiple(() =>
7365
{
@@ -91,7 +83,7 @@ public void Verify_that_configuration_is_loaded_from_appsettings()
9183
Assert.That(appConfigService.AppConfig.AuthenticationConfig.ExternalJwtAuthenticationConfig.IsEnabled, Is.True);
9284
Assert.That(appConfigService.IsAuthenticationSchemeEnabled(BasicAuthenticationDefaults.AuthenticationScheme), Is.True);
9385
Assert.That(appConfigService.IsAuthenticationSchemeEnabled(JwtBearerDefaults.LocalAuthenticationScheme), Is.True);
94-
Assert.That(appConfigService.IsAuthenticationSchemeEnabled(JwtBearerDefaults.ExternalAuthenticationScheme), Is.False);
86+
Assert.That(appConfigService.IsAuthenticationSchemeEnabled(JwtBearerDefaults.ExternalAuthenticationScheme), Is.True);
9587
});
9688
}
9789
}

CometServer/Configuration/AppConfigService.cs

+2-14
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
namespace CometServer.Configuration
2626
{
27-
using System.Linq;
28-
29-
using CDP4Authentication;
30-
3127
using CometServer.Authentication.Basic;
3228
using CometServer.Authentication.Bearer;
3329

@@ -38,22 +34,15 @@ namespace CometServer.Configuration
3834
/// </summary>
3935
public class AppConfigService : IAppConfigService
4036
{
41-
/// <summary>
42-
/// Gets the injected <see cref="IAuthenticationPluginInjector"/>, uses to check wheter that external JWT authentication is enabled or not
43-
/// </summary>
44-
private readonly IAuthenticationPluginInjector authenticationPluginInjector;
45-
4637
/// <summary>
4738
/// Initializes a new instance of the <see cref="AppConfigService"/>
4839
/// </summary>
4940
/// <param name="configuration">
5041
/// The <see cref="IConfiguration"/> used to set the properties
5142
/// </param>
52-
/// <param name="authenticationPluginInjector">The injected <see cref="IAuthenticationPluginInjector"/>, uses to check wheter that external JWT authentication is enabled or not</param>
53-
public AppConfigService(IConfiguration configuration, IAuthenticationPluginInjector authenticationPluginInjector)
43+
public AppConfigService(IConfiguration configuration)
5444
{
5545
this.AppConfig = new AppConfig(configuration);
56-
this.authenticationPluginInjector = authenticationPluginInjector;
5746
}
5847

5948
/// <summary>
@@ -72,8 +61,7 @@ public bool IsAuthenticationSchemeEnabled(string schemeName)
7261
{
7362
BasicAuthenticationDefaults.AuthenticationScheme => this.AppConfig.AuthenticationConfig.BasicAuthenticationConfig.IsEnabled,
7463
JwtBearerDefaults.LocalAuthenticationScheme => this.AppConfig.AuthenticationConfig.LocalJwtAuthenticationConfig.IsEnabled,
75-
JwtBearerDefaults.ExternalAuthenticationScheme => this.AppConfig.AuthenticationConfig.ExternalJwtAuthenticationConfig.IsEnabled
76-
&& this.authenticationPluginInjector.Connectors.Any(x => x.Name == "CDP4ExternalJwtAuthentication" && x.Properties.IsEnabled),
64+
JwtBearerDefaults.ExternalAuthenticationScheme => this.AppConfig.AuthenticationConfig.ExternalJwtAuthenticationConfig.IsEnabled,
7765
_ => false
7866
};
7967
}

CometServer/Configuration/ExternalJwtAuthenticationConfig.cs

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public ExternalJwtAuthenticationConfig(IConfiguration configuration)
5555
this.Authority = configuration["Authentication:ExternalJwtBearer:Authority"];
5656
this.ValidIssuer = configuration["Authentication:ExternalJwtBearer:ValidIssuer"];
5757
this.IdentifierClaimName = configuration["Authentication:ExternalJwtBearer:IdentifierClaimName"];
58+
this.RedirectUrl = configuration["Authentication:ExternalJwtBearer:RedirectUrl"];
5859
this.PersonIdentifierPropertyKind = Enum.Parse<PersonIdentifierPropertyKind>(configuration["Authentication:ExternalJwtBearer:PersonIdentifierPropertyKind"]!);
5960
}
6061

@@ -90,5 +91,10 @@ public ExternalJwtAuthenticationConfig(IConfiguration configuration)
9091
/// Gets or sets the <see cref="PersonIdentifierPropertyKind"/> to use for the authorization part
9192
/// </summary>
9293
public PersonIdentifierPropertyKind PersonIdentifierPropertyKind { get; set; }
94+
95+
/// <summary>
96+
/// Gets or sets the Url that should be sent back to unauthenticated client
97+
/// </summary>
98+
public string RedirectUrl { get; set; }
9399
}
94100
}

CometServer/Modules/Authentication/AuthenticationModule.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@
2525
namespace CometServer.Modules
2626
{
2727
using System;
28-
using System.Collections.Generic;
2928
using System.Linq;
3029
using System.Threading.Tasks;
3130

3231
using Carter;
3332
using Carter.Response;
3433

3534
using CometServer.Authentication;
35+
using CometServer.Authentication.Bearer;
3636
using CometServer.Configuration;
3737

38-
using Microsoft.AspNetCore.Authentication;
3938
using Microsoft.AspNetCore.Builder;
4039
using Microsoft.AspNetCore.Http;
4140
using Microsoft.AspNetCore.Routing;
@@ -86,7 +85,12 @@ public override void AddRoutes(IEndpointRouteBuilder app)
8685
private static Task ProvideEnabledAuthenticationScheme(HttpResponse res, IAppConfigService appConfigService)
8786
{
8887
var enabledSchemes = ApiBase.AuthenticationSchemes.Where(appConfigService.IsAuthenticationSchemeEnabled).ToList();
89-
return res.AsJson(enabledSchemes);
88+
89+
var redirectUrl = appConfigService.IsAuthenticationSchemeEnabled(JwtBearerDefaults.ExternalAuthenticationScheme)
90+
? appConfigService.AppConfig.AuthenticationConfig.ExternalJwtAuthenticationConfig.RedirectUrl
91+
: string.Empty;
92+
93+
return res.AsJson(new { Schemes = enabledSchemes, RedirectUrl = redirectUrl });
9094
}
9195

9296
/// <summary>

CometServer/Startup.cs

+20-6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ namespace CometServer
8686
using Microsoft.Extensions.Configuration;
8787
using Microsoft.Extensions.DependencyInjection;
8888
using Microsoft.Extensions.Hosting;
89+
using Microsoft.Extensions.Logging.Abstractions;
8990
using Microsoft.IdentityModel.JsonWebTokens;
9091
using Microsoft.IdentityModel.Tokens;
9192

@@ -197,9 +198,9 @@ private static void SetUpAuthentication(IServiceCollection services, IConfigurat
197198
throw new ConfigurationErrorsException("At least one authentication must be enabled");
198199
}
199200

200-
if (isLocalJwtBearerEnabled && isExternalJwtBearerEnabled)
201+
if (isExternalJwtBearerEnabled && (isBasicAuthEnabled || isLocalJwtBearerEnabled))
201202
{
202-
throw new ConfigurationErrorsException("Both local and external JWT Bearer authentication is enabled, only one may be enabled");
203+
throw new ConfigurationErrorsException("Both local and external authentication are enabled, local one is not supported while external authentication is enabled");
203204
}
204205

205206
var authenticationBuilder = services
@@ -311,6 +312,13 @@ private static void SetUpAuthentication(IServiceCollection services, IConfigurat
311312
throw new ConfigurationErrorsException($"Invalid value for Authentication:ExternalJwtBearer:PersonIdentifierPropertyKind," +
312313
$" should be one of: {string.Join(", ",Enum.GetValues<PersonIdentifierPropertyKind>())}");
313314
}
315+
316+
var redirectUrl = configuration["Authentication:ExternalJwtBearer:RedirectUrl"];
317+
318+
if (string.IsNullOrEmpty(redirectUrl))
319+
{
320+
throw new ConfigurationErrorsException("The Authentication:ExternalJwtBearer:RedirectUrl setting must be available");
321+
}
314322

315323
authenticationBuilder.AddExternalJwtBearerAuthentication(configure: options =>
316324
{
@@ -322,12 +330,18 @@ private static void SetUpAuthentication(IServiceCollection services, IConfigurat
322330
ValidateAudience = true,
323331
ValidateLifetime = true,
324332
ValidateIssuerSigningKey = false,
325-
SignatureValidator = (token, parameters) => new JsonWebToken(token),
326333
ValidAudience = validAudience,
327-
ValidIssuer = validIssuer
334+
ValidIssuer = validIssuer,
335+
SignatureValidator = (token, _) => new JsonWebToken(token)
328336
};
329-
}
330-
);
337+
});
338+
339+
var pluginInjector = new AuthenticationPluginInjector(new NullLogger<AuthenticationPluginInjector>());
340+
341+
if (!pluginInjector.Connectors.Any(x => x.Name == "CDP4ExternalJwtAuthentication" && x.Properties.IsEnabled))
342+
{
343+
throw new ConfigurationErrorsException("External JWT Authentication plugin is not present, please contact administrator to include Enterprise Edition plugins.");
344+
}
331345
}
332346
else
333347
{

CometServer/appsettings.Development.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,23 @@
5454
},
5555
"Authentication": {
5656
"Basic": {
57-
"IsEnabled": true
57+
"IsEnabled": false
5858
},
5959
"LocalJwtBearer": {
60-
"IsEnabled": true,
60+
"IsEnabled": false,
6161
"ValidIssuer": "CDP4-COMET",
6262
"ValidAudience": "localhost:5000",
6363
"SymmetricSecurityKey": "needs-to-be-updated-with-a-secret",
6464
"TokenExpirationMinutes": 150
6565
},
6666
"ExternalJwtBearer": {
67-
"IsEnabled": false,
67+
"IsEnabled": true,
6868
"ValidIssuer": "http://localhost:8080/realms/CDP4COMET",
6969
"ValidAudience": "account",
7070
"Authority": "http://localhost:8080/realms/CDP4COMET",
7171
"IdentifierClaimName": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
72-
"PersonIdentifierPropertyKind": "ShortName"
72+
"PersonIdentifierPropertyKind": "ShortName",
73+
"RedirectUrl": "localhost:8080/realms/CDP4COMET/protocol/openid-connect/auth?response_type=token&client_id=cdp4-comet-server&scope=openid_offline_access&redirect_url=http://localhost:5000"
7374
}
7475
},
7576
"Serilog": {

0 commit comments

Comments
 (0)