Skip to content

Commit 5b12943

Browse files
Update fastfed solution
1 parent bd9c763 commit 5b12943

25 files changed

Lines changed: 478 additions & 17 deletions

File tree

SimpleIdServer.FastFed.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleIdServer.FastFed.Iden
7373
EndProject
7474
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleIdServer.IdServer.Saml.Sp", "src\IdServer\SimpleIdServer.IdServer.Saml.Sp\SimpleIdServer.IdServer.Saml.Sp.csproj", "{4104E18C-467E-4BA0-B8AE-925E37AEBF22}"
7575
EndProject
76+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.Sid", "src\FastFed\SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.Sid\SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.Sid.csproj", "{A744CC8C-0C91-4826-85C4-20877185FF30}"
77+
EndProject
7678
Global
7779
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7880
Debug|Any CPU = Debug|Any CPU
@@ -191,6 +193,10 @@ Global
191193
{4104E18C-467E-4BA0-B8AE-925E37AEBF22}.Debug|Any CPU.Build.0 = Debug|Any CPU
192194
{4104E18C-467E-4BA0-B8AE-925E37AEBF22}.Release|Any CPU.ActiveCfg = Release|Any CPU
193195
{4104E18C-467E-4BA0-B8AE-925E37AEBF22}.Release|Any CPU.Build.0 = Release|Any CPU
196+
{A744CC8C-0C91-4826-85C4-20877185FF30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
197+
{A744CC8C-0C91-4826-85C4-20877185FF30}.Debug|Any CPU.Build.0 = Debug|Any CPU
198+
{A744CC8C-0C91-4826-85C4-20877185FF30}.Release|Any CPU.ActiveCfg = Release|Any CPU
199+
{A744CC8C-0C91-4826-85C4-20877185FF30}.Release|Any CPU.Build.0 = Release|Any CPU
194200
EndGlobalSection
195201
GlobalSection(SolutionProperties) = preSolution
196202
HideSolutionNode = FALSE
@@ -226,6 +232,7 @@ Global
226232
{419C42B1-6A41-4A43-AA46-379C336B3097} = {CDB564B6-1FB0-4F6C-87FE-FF79A6FDFF64}
227233
{72BAA8FC-8418-41EA-9F12-F1ABC8C7F7A5} = {E7858D10-1FFB-4682-98E1-DA5DBD40BC17}
228234
{4104E18C-467E-4BA0-B8AE-925E37AEBF22} = {CC647F05-F14B-4D2C-84ED-E8E76EA6EFE1}
235+
{A744CC8C-0C91-4826-85C4-20877185FF30} = {E7858D10-1FFB-4682-98E1-DA5DBD40BC17}
229236
EndGlobalSection
230237
GlobalSection(ExtensibilityGlobals) = postSolution
231238
SolutionGuid = {AFEA5C5B-33EB-4AEC-BB66-20E5AD9C228D}

src/FastFed/SimpleIdServer.FastFed.ApplicationProvider.Authentication.Saml/FastFedServicesBuilderExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using SimpleIdServer.FastFed.Apis.FastFedMetadata;
77
using SimpleIdServer.FastFed.ApplicationProvider.Authentication.Saml.Infrastructures;
8+
using SimpleIdServer.IdServer.Saml.Sp;
89
using System;
910

1011
namespace SimpleIdServer.FastFed.ApplicationProvider.Authentication.Saml;
@@ -14,6 +15,15 @@ public static class FastFedServicesBuilderExtensions
1415
public static FastFedServicesBuilder AddSamlAppProviderAuthenticationProfile(this FastFedServicesBuilder builder, Action<SamlAuthenticationOptions> cb)
1516
{
1617
builder.Services.Configure(cb);
18+
var opts = new SamlAuthenticationOptions();
19+
cb(opts);
20+
builder.Services.Configure<SamlSpOptions>(o =>
21+
{
22+
o.BackchannelHttpHandler = opts.BackchannelHttpHandler;
23+
o.SigningCertificate = opts.SigningCertificate;
24+
o.ContactPersons = opts.ContactPersons;
25+
o.SPId = opts.SpId;
26+
});
1727
builder.Services.AddTransient<IProviderMetadataEnricher, SamlAuthenticationProviderMetadataEnricher>();
1828
builder.Services.AddTransient<IAppProviderProvisioningService, SamlAuthenticationProvisioningService>();
1929
builder.Services.AddScoped<IAuthenticationHandlerProvider, DynamicSamlAuthenticationHandlerProvider>();

src/FastFed/SimpleIdServer.FastFed.ApplicationProvider.Authentication.Saml/Infrastructures/DynamicSamlAuthenticationSchemeProvider.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class DynamicSamlAuthenticationSchemeProvider : AuthenticationSchemeProvi
2626
private readonly IBusControl _busControl;
2727
private readonly IServiceProvider _serviceProvider;
2828
private readonly SamlAuthenticationOptions _samlAuthOptions;
29-
private readonly SamlSpOptions _samlSpOptions;
3029
private DateTime? _nextExpirationTime;
3130
private IEnumerable<AuthSchemeProvider> _cachedAuthSchemeProviders;
3231
private object _lck = new object();
@@ -35,13 +34,11 @@ public DynamicSamlAuthenticationSchemeProvider(
3534
IBusControl busControl,
3635
IServiceProvider serviceProvider,
3736
IOptions<SamlAuthenticationOptions> samlAuthOptions,
38-
IOptions<SamlSpOptions> samlSpOptions,
3937
IOptions<AuthenticationOptions> options) : base(options)
4038
{
4139
_busControl = busControl;
4240
_serviceProvider = serviceProvider;
4341
_samlAuthOptions = samlAuthOptions.Value;
44-
_samlSpOptions = samlSpOptions.Value;
4542
}
4643

4744
public async override Task<IEnumerable<AuthenticationScheme>> GetAllSchemesAsync()
@@ -80,12 +77,12 @@ private SamlAuthenticationScheme Convert(AuthSchemeProvider provider)
8077
var handlerType = typeof(SamlSpHandler);
8178
var options = new SamlSpOptions
8279
{
83-
SPId = _samlSpOptions.SPId,
80+
SPId = _samlAuthOptions.SpId,
8481
IdpMetadataUrl = provider.SamlMetadataUri,
85-
SigningCertificate = _samlSpOptions.SigningCertificate
82+
SigningCertificate = _samlAuthOptions.SigningCertificate
8683
};
8784
if (options.Backchannel == null)
88-
options.Backchannel = new HttpClient(_samlSpOptions.BackchannelHttpHandler ?? new HttpClientHandler());
85+
options.Backchannel = new HttpClient(_samlAuthOptions.BackchannelHttpHandler ?? new HttpClientHandler());
8986
return new SamlAuthenticationScheme(new AuthenticationScheme(provider.Name, provider.DisplayName, handlerType), options);
9087
}
9188
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
// Copyright (c) SimpleIdServer. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4+
using ITfoxtec.Identity.Saml2.Schemas.Metadata;
45
using SimpleIdServer.FastFed.Authentication.Saml;
6+
using System.Collections.Generic;
7+
using System.Net.Http;
8+
using System.Security.Cryptography.X509Certificates;
59

610
namespace SimpleIdServer.FastFed.ApplicationProvider.Authentication.Saml;
711

812
public class SamlAuthenticationOptions
913
{
1014
public string SpId { get; set; } = "samlApplicationProvider";
1115
public string SamlMetadataUri { get; set; }
16+
public X509Certificate2 SigningCertificate { get; set; }
17+
public HttpClientHandler BackchannelHttpHandler { get; set; }
18+
public IEnumerable<ContactPerson> ContactPersons { get; set; } = new List<ContactPerson>
19+
{
20+
new ContactPerson(ContactTypes.Technical)
21+
{
22+
Company = "SimpleIdServer",
23+
EmailAddress = "agentsimpleidserver@gmail.com"
24+
}
25+
};
1226
public SamlEntrepriseMappingsResult Mappings { get; set; }
1327
public int? CacheSamlAuthProvidersInSeconds { get; set; }
1428
}

src/FastFed/SimpleIdServer.FastFed.ApplicationProvider.Startup/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
6+
using SimpleIdServer.FastFed;
67
using SimpleIdServer.FastFed.ApplicationProvider.Authentication.Saml;
78
using SimpleIdServer.FastFed.ApplicationProvider.Options;
89
using SimpleIdServer.FastFed.ApplicationProvider.Provisioning.Scim;
@@ -64,6 +65,7 @@
6465
};
6566
})
6667
.AddFastFedApplicationProvider(cbChooser: (t) => t.UseInMemoryEfStore())
68+
.UseDefaultAppProviderSecurity(authOptions: authOptions)
6769
.AddAppProviderScimProvisioning(cb =>
6870
{
6971
cb.ScimServiceUri = scimOptions.Url;
@@ -89,6 +91,7 @@
8991
.AddSamlAppProviderAuthenticationProfile(cb =>
9092
{
9193
cb.SamlMetadataUri = "https://localhost:5021/saml-metadata.xml";
94+
cb.SigningCertificate = KeyGenerator.GenerateSelfSignedCertificate();
9295
cb.Mappings = new SamlEntrepriseMappingsResult
9396
{
9497
SamlSubject = new SamlSubject
@@ -110,8 +113,7 @@
110113
}
111114
}
112115
};
113-
})
114-
.UseDefaultAppProviderSecurity(authOptions: authOptions);
116+
});
115117
builder.Services.AddControllersWithViews();
116118

117119
var app = builder.Build();

src/FastFed/SimpleIdServer.FastFed.ApplicationProvider/Services/FastFedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public async Task<ValidationResult<Dictionary<string, JsonObject>>> Register(str
164164
IdProviderConfiguration = claim.Value
165165
});
166166
}
167-
dic.Add(service.Name, enableResult);
167+
dic.Add(service.RegisterConfigurationName, enableResult);
168168
}
169169

170170
idProviderFederation.LastCapabilities.Status = IdentityProviderStatus.CONFIRMED;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
using MassTransit;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using System;
7+
8+
namespace SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.Sid;
9+
10+
public static class FastFedServicesBuilderExtensions
11+
{
12+
public static FastFedServicesBuilder AddSidSamlAuthentication(this FastFedServicesBuilder builder, Action<FastFedSidSamlAuthenticationOptions> cb = null)
13+
{
14+
builder.Services.Configure(cb);
15+
builder.Services.AddTransient<ISamlClientProvisioningService, SidSamlClientProvisioningService>();
16+
return builder;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.Sid;
5+
6+
public class FastFedSidSamlAuthenticationOptions
7+
{
8+
public string SidBaseUrl { get; set; }
9+
public string ClientId { get; set; }
10+
public string ClientSecret { get; set; }
11+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
using Microsoft.Extensions.Options;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using System.Text.Json;
9+
using System.Text.Json.Nodes;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
13+
namespace SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.Sid;
14+
15+
public class SidSamlClientProvisioningService : ISamlClientProvisioningService
16+
{
17+
private readonly IdServer.Helpers.IHttpClientFactory _httpClientFactory;
18+
private readonly FastFedSidSamlAuthenticationOptions _options;
19+
20+
public SidSamlClientProvisioningService(
21+
IdServer.Helpers.IHttpClientFactory httpClientFactory,
22+
IOptions<FastFedSidSamlAuthenticationOptions> options)
23+
{
24+
_httpClientFactory = httpClientFactory;
25+
_options = options.Value;
26+
}
27+
28+
public async Task Provision(string clientId, string metadataUrl, CancellationToken cancellationToken)
29+
{
30+
using (var httpClient = _httpClientFactory.GetHttpClient())
31+
{
32+
var client = new IdServer.Domains.Client
33+
{
34+
Id = Guid.NewGuid().ToString(),
35+
ClientSecret = Guid.NewGuid().ToString(),
36+
ClientId = clientId,
37+
ClientType = "SAML",
38+
CreateDateTime = DateTime.UtcNow,
39+
UpdateDateTime = DateTime.UtcNow,
40+
Parameters = new JsonObject
41+
{
42+
{ "SAML2_SP_METADATA", metadataUrl }
43+
}
44+
};
45+
var requestMessage = new System.Net.Http.HttpRequestMessage
46+
{
47+
RequestUri = new Uri($"{_options.SidBaseUrl}/clients"),
48+
Method = System.Net.Http.HttpMethod.Post,
49+
Content = new System.Net.Http.StringContent(JsonSerializer.Serialize(client), Encoding.UTF8, "application/json")
50+
};
51+
var accessToken = await GetAccessToken(httpClient);
52+
requestMessage.Headers.Add("Authorization", $"Bearer {accessToken}");
53+
var httpResult = await httpClient.SendAsync(requestMessage);
54+
string ss = "";
55+
}
56+
}
57+
58+
private async Task<string> GetAccessToken(System.Net.Http.HttpClient httpClient)
59+
{
60+
var content = new List<KeyValuePair<string, string>>
61+
{
62+
new KeyValuePair<string, string>("client_id", _options.ClientId),
63+
new KeyValuePair<string, string>("client_secret", _options.ClientSecret),
64+
new KeyValuePair<string, string>("scope", "clients"),
65+
new KeyValuePair<string, string>("grant_type", "client_credentials")
66+
};
67+
var httpRequest = new System.Net.Http.HttpRequestMessage
68+
{
69+
Method = System.Net.Http.HttpMethod.Post,
70+
RequestUri = new Uri($"{_options.SidBaseUrl}/token"),
71+
Content = new System.Net.Http.FormUrlEncodedContent(content)
72+
};
73+
var httpResult = await httpClient.SendAsync(httpRequest);
74+
var json = await httpResult.Content.ReadAsStringAsync();
75+
var accessToken = JsonObject.Parse(json)["access_token"].GetValue<string>();
76+
return accessToken;
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<Description>Add saml authentication support and provision SAML clients to SimpleIdServer.</Description>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<ProjectReference Include="..\..\IdServer\SimpleIdServer.IdServer.Domains\SimpleIdServer.IdServer.Domains.csproj" />
8+
<ProjectReference Include="..\SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml\SimpleIdServer.FastFed.IdentityProvider.Authentication.Saml.csproj" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)