Skip to content

Instantly share code, notes, and snippets.

@ducas
Created September 5, 2014 01:27
Show Gist options
  • Save ducas/17bfbe67710dfd4137f9 to your computer and use it in GitHub Desktop.
Save ducas/17bfbe67710dfd4137f9 to your computer and use it in GitHub Desktop.
In-Memory Issuer Name Registry Implementation for WAAD
using System.Collections.Generic;
using System.IdentityModel.Tokens;
using System.Linq;
using Web.Models;
namespace Web.Utils
{
public class InMemoryIssuerNameRegistry : ValidatingIssuerNameRegistry
{
public class TenantData
{
//Note - this is a little lazy. It really should be using a thread safe collection, etc, but it's not.
//It's a very low use site, so we should be able to get away with it.
private readonly List<IssuingAuthorityKey> _issuingAuthorityKeys = new List<IssuingAuthorityKey>();
private readonly List<Tenant> _tenants = new List<Tenant>();
public List<IssuingAuthorityKey> IssuingAuthorityKeys
{
get { return _issuingAuthorityKeys; }
}
public List<Tenant> Tenants
{
get { return _tenants; }
}
}
private static readonly TenantData Data = new TenantData();
public static bool ContainsTenant(string tenantId)
{
return Data.Tenants.Any(tenant => tenant.Id == tenantId);
}
public static bool ContainsKey(string thumbprint)
{
return Data.IssuingAuthorityKeys.Any(key => key.Id == thumbprint);
}
public static void RefreshKeys(string metadataLocation)
{
var issuingAuthority = GetIssuingAuthority(metadataLocation);
var newKeys = issuingAuthority.Thumbprints.Any(thumbprint => !ContainsKey(thumbprint));
if (!newKeys) return;
Data.IssuingAuthorityKeys.Clear();
foreach (var thumbprint in issuingAuthority.Thumbprints)
{
Data.IssuingAuthorityKeys.Add(new IssuingAuthorityKey { Id = thumbprint });
}
foreach (var issuer in issuingAuthority.Issuers)
{
Data.Tenants.Add(new Tenant { Id = issuer.TrimEnd('/').Split('/').Last() });
}
}
protected override bool IsThumbprintValid(string thumbprint, string issuer)
{
var issuerId = issuer.TrimEnd('/').Split('/').Last();
return ContainsTenant(issuerId) && ContainsKey(thumbprint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment