Skip to content

Commit fc5ed95

Browse files
committed
allow issuer plan.uaa.systemdomain when authority is plan.login.systemdomain
1 parent 0088297 commit fc5ed95

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/Security/src/Authentication.JwtBearer/PostConfigureJwtBearerOptions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Authentication.JwtBearer;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.Options;
8+
using Steeltoe.Common;
89

910
namespace Steeltoe.Security.Authentication.JwtBearer;
1011

@@ -42,7 +43,18 @@ public void PostConfigure(string? name, JwtBearerOptions options)
4243
return;
4344
}
4445

45-
options.TokenValidationParameters.ValidIssuer = $"{options.Authority}/oauth/token";
46+
if (Platform.IsCloudFoundry && options.Authority.Contains(".login", StringComparison.OrdinalIgnoreCase))
47+
{
48+
options.TokenValidationParameters.ValidIssuers =
49+
[
50+
$"{options.Authority}/oauth/token",
51+
$"{options.Authority.Replace(".login", ".uaa", StringComparison.OrdinalIgnoreCase)}/oauth/token"
52+
];
53+
}
54+
else
55+
{
56+
options.TokenValidationParameters.ValidIssuer = $"{options.Authority}/oauth/token";
57+
}
4658

4759
var keyResolver = new TokenKeyResolver(options.Authority, options.Backchannel);
4860
options.TokenValidationParameters.IssuerSigningKeyResolver = (_, _, keyId, _) => keyResolver.ResolveSigningKey(keyId);

src/Security/test/Authentication.JwtBearer.Test/PostConfigureJwtBearerOptionsTest.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,53 @@ public void PostConfigure_AddsClientIdToValidAudiences()
3636

3737
[Fact]
3838
public async Task PostConfigure_ConfiguresForCloudFoundry()
39+
{
40+
const string vcapServices = """
41+
{
42+
"p-identity": [
43+
{
44+
"label": "p-identity",
45+
"provider": null,
46+
"plan": "steeltoe",
47+
"name": "mySSOService",
48+
"tags": [],
49+
"instance_guid": "ea8b8ac0-ce85-4726-8b39-d1b2eb55b45b",
50+
"instance_name": "mySSOService",
51+
"binding_guid": "be94e8e7-9246-49af-935f-5390ff10ac23",
52+
"binding_name": null,
53+
"credentials": {
54+
"auth_domain": "https://steeltoe.uaa.sys.cf-app.com",
55+
"grant_types": [ "client_credentials" ],
56+
"client_secret": "dd2c82e1-aa99-4eaf-9871-2eb7412b79bb",
57+
"client_id": "4e6f8e34-f42b-440e-a042-f2b13c1d5bed"
58+
},
59+
"syslog_drain_url": null,
60+
"volume_mounts": []
61+
}]
62+
}
63+
""";
64+
65+
using var servicesScope = new EnvironmentVariableScope("VCAP_SERVICES", vcapServices);
66+
IConfiguration configuration = new ConfigurationBuilder().AddCloudFoundryServiceBindings().Build();
67+
var services = new ServiceCollection();
68+
services.AddSingleton(configuration);
69+
services.AddAuthentication().AddJwtBearer().ConfigureJwtBearerForCloudFoundry();
70+
71+
await using ServiceProvider serviceProvider = services.BuildServiceProvider(true);
72+
var optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<JwtBearerOptions>>();
73+
JwtBearerOptions options = optionsMonitor.Get(JwtBearerDefaults.AuthenticationScheme);
74+
75+
options.Authority.Should().Be("https://steeltoe.uaa.sys.cf-app.com");
76+
options.MetadataAddress.Should().Be("https://steeltoe.uaa.sys.cf-app.com/.well-known/openid-configuration");
77+
options.RequireHttpsMetadata.Should().BeTrue();
78+
options.TokenValidationParameters.ValidIssuer.Should().Be("https://steeltoe.uaa.sys.cf-app.com/oauth/token");
79+
options.TokenValidationParameters.ValidIssuers.Should().BeEmpty();
80+
options.TokenValidationParameters.IssuerSigningKeyResolver.Should().NotBeNull();
81+
options.TokenValidationParameters.ValidAudiences.Should().Contain("4e6f8e34-f42b-440e-a042-f2b13c1d5bed");
82+
}
83+
84+
[Fact]
85+
public async Task PostConfigure_ConfiguresForCloudFoundry_AllowMultipleIssuers()
3986
{
4087
const string vcapServices = """
4188
{
@@ -62,6 +109,7 @@ public async Task PostConfigure_ConfiguresForCloudFoundry()
62109
}
63110
""";
64111

112+
using var applicationScope = new EnvironmentVariableScope("VCAP_APPLICATION", "{}");
65113
using var servicesScope = new EnvironmentVariableScope("VCAP_SERVICES", vcapServices);
66114
IConfiguration configuration = new ConfigurationBuilder().AddCloudFoundryServiceBindings().Build();
67115
var services = new ServiceCollection();
@@ -75,7 +123,10 @@ public async Task PostConfigure_ConfiguresForCloudFoundry()
75123
options.Authority.Should().Be("https://steeltoe.login.sys.cf-app.com");
76124
options.MetadataAddress.Should().Be("https://steeltoe.login.sys.cf-app.com/.well-known/openid-configuration");
77125
options.RequireHttpsMetadata.Should().BeTrue();
78-
options.TokenValidationParameters.ValidIssuer.Should().Be("https://steeltoe.login.sys.cf-app.com/oauth/token");
126+
options.TokenValidationParameters.ValidIssuer.Should().BeNull();
127+
options.TokenValidationParameters.ValidIssuers.Should().Contain("https://steeltoe.login.sys.cf-app.com/oauth/token").And
128+
.Contain("https://steeltoe.uaa.sys.cf-app.com/oauth/token");
129+
79130
options.TokenValidationParameters.IssuerSigningKeyResolver.Should().NotBeNull();
80131
options.TokenValidationParameters.ValidAudiences.Should().Contain("4e6f8e34-f42b-440e-a042-f2b13c1d5bed");
81132
}

0 commit comments

Comments
 (0)