Skip to content

Commit

Permalink
optimize saml2 authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Nov 8, 2023
1 parent e246047 commit 4daf86e
Show file tree
Hide file tree
Showing 13 changed files with 632 additions and 309 deletions.
502 changes: 495 additions & 7 deletions .editorconfig

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
Expand All @@ -8,9 +8,6 @@

namespace Passingwind.AspNetCore.Authentication.Saml2.Configuration;

/// <summary>
///
/// </summary>
public class ConfigurationManager : IConfigurationManager
{
private Saml2Configuration? _saml2Configuration;
Expand All @@ -19,30 +16,21 @@ public class ConfigurationManager : IConfigurationManager
private readonly Uri _idpMetadataUri;
private readonly HttpClient _httpClient;

/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <param name="idpMetadataUrl"></param>
/// <param name="httpClient"></param>
public ConfigurationManager(Saml2Options options, Uri idpMetadataUrl, HttpClient httpClient)
{
_options = options;
_idpMetadataUri = idpMetadataUrl;
_httpClient = httpClient;
}

/// <summary>
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Saml2Configuration> GetConfigurationAsync(CancellationToken cancellationToken = default)
{
if (_saml2Configuration != null)
{
return _saml2Configuration;
}

var configuration = new Saml2Configuration()
Saml2Configuration configuration = new()
{
Issuer = _options.Issuer,
CertificateValidationMode = _options.CertificateValidationMode,
Expand All @@ -53,20 +41,20 @@ public async Task<Saml2Configuration> GetConfigurationAsync(CancellationToken ca

configuration.AllowedAudienceUris.Add(configuration.Issuer);

var entityDescriptor = new EntityDescriptor();
EntityDescriptor entityDescriptor = new();

if (_idpMetadataUri.IsFile)
{
entityDescriptor.ReadIdPSsoDescriptorFromFile(_idpMetadataUri.ToString());
_ = entityDescriptor.ReadIdPSsoDescriptorFromFile(_idpMetadataUri.ToString());
}
else
{
// await entityDescriptor.ReadIdPSsoDescriptorFromUrlAsync(_httpClientFactory, _idpMetadata, cancellationToken);
var metadataGetResponse = await _httpClient.GetAsync(_idpMetadataUri, cancellationToken);
metadataGetResponse.EnsureSuccessStatusCode();
HttpResponseMessage metadataGetResponse = await _httpClient.GetAsync(_idpMetadataUri, cancellationToken);
_ = metadataGetResponse.EnsureSuccessStatusCode();

var metadataString = await metadataGetResponse.Content.ReadAsStringAsync(cancellationToken);
entityDescriptor.ReadIdPSsoDescriptor(metadataString);
string metadataString = await metadataGetResponse.Content.ReadAsStringAsync(cancellationToken);
_ = entityDescriptor.ReadIdPSsoDescriptor(metadataString);
}

if (entityDescriptor.IdPSsoDescriptor != null)
Expand All @@ -76,7 +64,7 @@ public async Task<Saml2Configuration> GetConfigurationAsync(CancellationToken ca
configuration.SingleSignOnDestination = entityDescriptor.IdPSsoDescriptor.SingleSignOnServices.First().Location;
configuration.SingleLogoutDestination = entityDescriptor.IdPSsoDescriptor.SingleLogoutServices.First().Location;

foreach (var signingCertificate in entityDescriptor.IdPSsoDescriptor.SigningCertificates)
foreach (System.Security.Cryptography.X509Certificates.X509Certificate2? signingCertificate in entityDescriptor.IdPSsoDescriptor.SigningCertificates)
{
if (signingCertificate.IsValidLocalTime())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace Passingwind.AspNetCore.Authentication.Saml2.Configuration;

/// <summary>
///
///
/// </summary>
public interface IConfigurationManager
{
/// <summary>
///
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<Saml2Configuration> GetConfigurationAsync(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Passingwind.AspNetCore.Authentication.Saml2.Configuration;

/// <summary>
///
///
/// </summary>
public class StaticConfigurationManager : IConfigurationManager
{
private readonly Saml2Configuration _saml2Configuration;

/// <summary>
///
///
/// </summary>
/// <param name="saml2Configuration"></param>
public StaticConfigurationManager(Saml2Configuration saml2Configuration)
Expand All @@ -21,10 +21,9 @@ public StaticConfigurationManager(Saml2Configuration saml2Configuration)
}

/// <summary>
///
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<Saml2Configuration> GetConfigurationAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(_saml2Configuration);
Expand Down
14 changes: 3 additions & 11 deletions src/Authentication.Saml2/source/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.AspNetCore.Http;
Expand All @@ -21,19 +21,11 @@ public static ITfoxtec.Identity.Saml2.Http.HttpRequest ToGenericHttpRequest(this

private static NameValueCollection ToNameValueCollection(IEnumerable<KeyValuePair<string, StringValues>> items)
{
var nv = new NameValueCollection();
foreach (var item in items)
NameValueCollection nv = new();
foreach (KeyValuePair<string, StringValues> item in items)
{
nv.Add(item.Key, item.Value[0]);
}
return nv;
}

//private static async Task<string> ReadBodyStringAsync(HttpRequest request)
//{
// using (var reader = new StreamReader(request.Body))
// {
// return await reader.ReadToEndAsync();
// }
//}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6;net7</TargetFrameworks>
<Nullable>enable</Nullable>
<RootNamespace>Passingwind.AspNetCore.Authentication.Saml2</RootNamespace>
<AnalysisLevel>latest-minimum</AnalysisLevel>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<FileVersion>$(AssemblyVersion)</FileVersion>
<AssemblyVersion>0.1</AssemblyVersion>
<NoWarn>1701;1702;CS1591</NoWarn>
<FileVersion>$(Version)</FileVersion>
<Version>0.2.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -23,7 +24,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageVersion>0.1.0</PackageVersion>
<PackageVersion>0.2.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 0 additions & 9 deletions src/Authentication.Saml2/source/Saml2Defaults.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
namespace Passingwind.AspNetCore.Authentication.Saml2;

/// <summary>
///
/// </summary>
public static class Saml2Defaults
{
/// <summary>
///
/// </summary>
public const string AuthenticationScheme = "Saml2";

/// <summary>
///
/// </summary>
public const string DisplayName = "Saml2";
}
Loading

0 comments on commit 4daf86e

Please sign in to comment.