Skip to content

Commit

Permalink
Update CertificateDescription to inherit from CredentialDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 committed Jul 12, 2022
1 parent 5aa8fdb commit c1d20d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 167 deletions.
201 changes: 34 additions & 167 deletions src/Microsoft.Identity.Web.Certificate/CertificateDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Identity.Web
/// <summary>
/// Description of a certificate.
/// </summary>
public class CertificateDescription
public class CertificateDescription : CredentialDescription
{
/// <summary>
/// Creates a certificate description from a certificate (by code).
Expand Down Expand Up @@ -133,111 +133,6 @@ public static CertificateDescription FromStoreWithDistinguishedName(
};
}

/// <summary>
/// Type of the source of the certificate.
/// </summary>
public CertificateSource SourceType { get; set; }

/// <summary>
/// Container in which to find the certificate.
/// <list type="bullet">
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.KeyVault"/>, then
/// the container is the Key Vault base URL.</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.Base64Encoded"/>, then
/// this value is not used.</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.Path"/>, then
/// this value is the path on disk where to find the certificate.</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.StoreWithDistinguishedName"/>,
/// or <see cref="CertificateSource.StoreWithThumbprint"/>, then
/// this value is the path to the certificate in the cert store, for instance <c>CurrentUser/My</c>.</item>
/// </list>
/// </summary>
internal string? Container
{
get
{
switch (SourceType)
{
case CertificateSource.Certificate:
return null;
case CertificateSource.KeyVault:
return KeyVaultUrl;
case CertificateSource.Base64Encoded:
return null;
case CertificateSource.Path:
return CertificateDiskPath;
case CertificateSource.StoreWithThumbprint:
case CertificateSource.StoreWithDistinguishedName:
return CertificateStorePath;
default:
return null;
}
}
set
{
switch (SourceType)
{
case CertificateSource.Certificate:
break;
case CertificateSource.KeyVault:
KeyVaultUrl = value;
break;
case CertificateSource.Base64Encoded:
break;
case CertificateSource.Path:
CertificateDiskPath = value;
break;
case CertificateSource.StoreWithDistinguishedName:
case CertificateSource.StoreWithThumbprint:
CertificateStorePath = value;
break;
default:
break;
}
}
}

/// <summary>
/// URL of the Key Vault, for instance https://msidentitywebsamples.vault.azure.net.
/// </summary>
public string? KeyVaultUrl { get; set; }

/// <summary>
/// Certificate store path, for instance "CurrentUser/My".
/// </summary>
/// <remarks>This property should only be used in conjunction with DistinguishedName or Thumbprint.</remarks>
public string? CertificateStorePath { get; set; }

/// <summary>
/// Certificate distinguished name.
/// </summary>
public string? CertificateDistinguishedName { get; set; }

/// <summary>
/// Name of the certificate in Key Vault.
/// </summary>
public string? KeyVaultCertificateName { get; set; }

/// <summary>
/// Certificate thumbprint.
/// </summary>
public string? CertificateThumbprint { get; set; }

/// <summary>
/// Path on disk to the certificate.
/// </summary>
public string? CertificateDiskPath { get; set; }

/// <summary>
/// Path on disk to the certificate password.
/// </summary>
public string? CertificatePassword { get; set; }

/// <summary>
/// Base64 encoded certificate value.
/// </summary>
public string? Base64EncodedValue { get; set; }

#if DOTNET_462 || DOTNET_STANDARD_20
/// <summary>
/// Defines where and how to import the private key of an X.509 certificate.
Expand All @@ -250,73 +145,45 @@ internal string? Container
public X509KeyStorageFlags X509KeyStorageFlags { get; set; } = X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet;
#endif

// Should Container and ReferenceOrValue be moved to
// the tests (As extension methods)

#region Backwards compatibilty with 1.x
/// <summary>
/// Reference to the certificate or value.
/// <inheritdoc/>.
/// </summary>
/// <list type="bullet">
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.KeyVault"/>, then
/// the reference is the name of the certificate in Key Vault (maybe the version?).</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.Base64Encoded"/>, then
/// this value is the base 64 encoded certificate itself.</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.Path"/>, then
/// this value is the password to access the certificate (if needed).</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.StoreWithDistinguishedName"/>,
/// this value is the distinguished name.</item>
/// <item>If <see cref="SourceType"/> equals <see cref="CertificateSource.StoreWithThumbprint"/>,
/// this value is the thumbprint.</item>
/// </list>
internal string? ReferenceOrValue
internal new string? Container
{
get
{
switch (SourceType)
{
case CertificateSource.KeyVault:
return KeyVaultCertificateName;
case CertificateSource.Path:
return CertificatePassword;
case CertificateSource.StoreWithThumbprint:
return CertificateThumbprint;
case CertificateSource.StoreWithDistinguishedName:
return CertificateDistinguishedName;
case CertificateSource.Certificate:
case CertificateSource.Base64Encoded:
return Base64EncodedValue;
default:
return null;
}
}
set
{
switch (SourceType)
{
case CertificateSource.Certificate:
break;
case CertificateSource.KeyVault:
KeyVaultCertificateName = value;
break;
case CertificateSource.Base64Encoded:
Base64EncodedValue = value;
break;
case CertificateSource.Path:
CertificateDiskPath = value;
break;
case CertificateSource.StoreWithThumbprint:
CertificateThumbprint = value;
break;
case CertificateSource.StoreWithDistinguishedName:
CertificateDistinguishedName = value;
break;
default:
break;
}
}
get { return base.Container; }
set { base.Container = value; }
}

/// <summary>
/// <inheritdoc/>.
/// </summary>
internal new string? ReferenceOrValue
{
get { return base.ReferenceOrValue; }
set { base.ReferenceOrValue = value; }
}

/// <summary>
/// The certificate, either provided directly in code
/// or loaded from the description.
/// <inheritdoc/>.
/// </summary>
public X509Certificate2? Certificate { get; protected internal set; }
public new X509Certificate2? Certificate
{
get { return base.Certificate; }
protected internal set { base.Certificate = value; }
}

/// <summary>
/// <inheritdoc/>.
/// </summary>
public new CertificateSource SourceType
{
get { return (CertificateSource)base.SourceType; }
set { base.SourceType = (CredentialSource)value; }
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
<PackageReference Include="System.Text.Encodings.Web" Version="$(SystemTextEncodingsWebVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Identity.Web.TokenAcquisition.Abstractions\Microsoft.Identity.Web.TokenAcquisition.Abstractions.csproj" />
</ItemGroup>
</Project>

0 comments on commit c1d20d1

Please sign in to comment.