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

Support the SAML2 assertion encryption #528

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .devcontainer/keycloak/import/demo-realm.json
Original file line number Diff line number Diff line change
Expand Up @@ -2247,7 +2247,7 @@
"cibaInterval" : "5",
"realmReusableOtpCode" : "false"
},
"keycloakVersion" : "23.0.3",
"keycloakVersion" : "23.0.6",
"userManagedAccessAllowed" : false,
"clientProfiles" : {
"profiles" : [ ]
Expand Down
2 changes: 1 addition & 1 deletion samples/AspNetCoreSample/AspNetCoreSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<None Update="saml2.p12">
<None Update="*.p12">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
16 changes: 15 additions & 1 deletion samples/AspNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,21 @@ await context.Options.Events.RedirectToLogout(new RedirectContext<CookieAuthenti
options.SPOptions.EntityId = new EntityId(builder.Configuration["SAML2:SP:EntityId"]);
options.SPOptions.ServiceCertificates.Add(new X509Certificate2(
builder.Configuration["SAML2:SP:Certificate:Path"]!,
builder.Configuration["SAML2:SP:Certificate:Pass"]!));
builder.Configuration["SAML2:SP:Certificate:Pass"],
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet));

var decryptionCertificatePath = builder.Configuration["SAML2:SP:Decryption:Certificate:Path"];
if (!string.IsNullOrWhiteSpace(decryptionCertificatePath) && File.Exists(decryptionCertificatePath))
{
options.SPOptions.ServiceCertificates.Add(new ServiceCertificate
{
Certificate = new X509Certificate2(decryptionCertificatePath,
builder.Configuration["SAML2:SP:Decryption:Certificate:Pass"],
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet),
Use = CertificateUse.Encryption
});
}

options.SPOptions.TokenValidationParametersTemplate.NameClaimType = ClaimTypes.NameIdentifier;
options.IdentityProviders.Add(
new IdentityProvider(new EntityId(builder.Configuration["SAML2:IdP:EntityId"]), options.SPOptions)
Expand Down
9 changes: 0 additions & 9 deletions samples/AspNetCoreSample/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,5 @@
"ClientId": "oidc",
"ClientSecret": "AGtQZ3m2rHK63lQpoZ35vFXG6MgjkN5w",
"SaveTokens": true
},
"SAML2": {
"SP": {
"Certificate": {
"Path": "saml2.p12",
"Pass": "changeit"
},
"EntityId": "saml"
}
}
}
6 changes: 5 additions & 1 deletion samples/AspNetCoreSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
"MetadataLocation": "https://auth.dev.local/realms/demo/protocol/saml/descriptor"
},
"SP": {
"EntityId": "CHANGE_ME"
"EntityId": "saml",
"Certificate": {
"Path": "saml.p12",
"Pass": "changeit"
}
}
}
}
Binary file added samples/AspNetCoreSample/saml.p12
Binary file not shown.
Binary file removed samples/AspNetCoreSample/saml2.p12
Binary file not shown.
Binary file added samples/OwinSample/App_Data/saml.p12
Binary file not shown.
Binary file removed samples/OwinSample/App_Data/saml2.p12
Binary file not shown.
6 changes: 4 additions & 2 deletions samples/OwinSample/OwinSample.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -83,7 +83,9 @@
<DependentUpon>appsettings.json</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="App_Data\saml2.p12" />
<None Include="App_Data\*.p12">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.Mvc" />
Expand Down
22 changes: 16 additions & 6 deletions samples/OwinSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
Expand Down Expand Up @@ -224,15 +225,24 @@ public void Configuration(IAppBuilder app)

private static Saml2AuthenticationOptions CreateSaml2Options(IConfiguration configuration)
{
var spOptions = new SPOptions() { EntityId = new EntityId(configuration["SAML2:SP:EntityId"]) };
var certPath = configuration["SAML2:SP:Certificate:Path"];
if (certPath != null && certPath.StartsWith("~"))
var spOptions = new SPOptions { EntityId = new EntityId(configuration["SAML2:SP:EntityId"]) };
spOptions.ServiceCertificates.Add(new X509Certificate2(
HttpContext.Current.Server.MapPath(configuration["SAML2:SP:Certificate:Path"]),
configuration["SAML2:SP:Certificate:Pass"],
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet));
var decryptionCertificatePath =
HttpContext.Current.Server.MapPath(configuration["SAML2:SP:Decryption:Certificate:Path"]);
if (!string.IsNullOrWhiteSpace(decryptionCertificatePath) && File.Exists(decryptionCertificatePath))
{
certPath = HttpContext.Current.Server.MapPath(certPath);
spOptions.ServiceCertificates.Add(new ServiceCertificate
{
Certificate = new X509Certificate2(decryptionCertificatePath,
configuration["SAML2:SP:Decryption:Certificate:Pass"]!,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet),
Use = CertificateUse.Encryption
});
}

spOptions.ServiceCertificates.Add(
new X509Certificate2(certPath, configuration["SAML2:SP:Certificate:Pass"]));
spOptions.TokenValidationParametersTemplate.NameClaimType = ClaimTypes.NameIdentifier;
var options = new Saml2AuthenticationOptions(false) { SPOptions = spOptions };
var idp = new IdentityProvider(new EntityId(configuration["SAML2:IdP:EntityId"]), spOptions)
Expand Down
9 changes: 0 additions & 9 deletions samples/OwinSample/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,5 @@
"OIDC": {
"ClientId": "oidc",
"ClientSecret": "AGtQZ3m2rHK63lQpoZ35vFXG6MgjkN5w"
},
"SAML2": {
"SP": {
"Certificate": {
"Path": "~/App_Data/saml2.p12",
"Pass": "changeit"
},
"EntityId": "saml"
}
}
}
6 changes: 5 additions & 1 deletion samples/OwinSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"MetadataLocation": "https://auth.dev.local/realms/demo/protocol/saml/descriptor"
},
"SP": {
"EntityId": "CHANGE_ME"
"EntityId": "saml",
"Certificate": {
"Path": "~/App_Data/saml.p12",
"Pass": "changeit"
}
}
}
}