// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.DataProtection.Abstractions;
namespace Microsoft.AspNetCore.DataProtection
{
///
/// Helpful extension methods for data protection APIs.
///
public static class DataProtectionExtensions
{
///
/// Creates an given a list of purposes.
///
/// The from which to generate the purpose chain.
/// The list of purposes which contribute to the purpose chain. This list must
/// contain at least one element, and it may not contain null elements.
/// An tied to the provided purpose chain.
///
/// This is a convenience method which chains together several calls to
/// . See that method's
/// documentation for more information.
///
public static IDataProtector CreateProtector(this IDataProtectionProvider provider, IEnumerable purposes)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (purposes == null)
{
throw new ArgumentNullException(nameof(purposes));
}
bool collectionIsEmpty = true;
IDataProtectionProvider retVal = provider;
foreach (string purpose in purposes)
{
if (purpose == null)
{
throw new ArgumentException(Resources.DataProtectionExtensions_NullPurposesCollection, nameof(purposes));
}
retVal = retVal.CreateProtector(purpose) ?? CryptoUtil.Fail("CreateProtector returned null.");
collectionIsEmpty = false;
}
if (collectionIsEmpty)
{
throw new ArgumentException(Resources.DataProtectionExtensions_NullPurposesCollection, nameof(purposes));
}
Debug.Assert(retVal is IDataProtector); // CreateProtector is supposed to return an instance of this interface
return (IDataProtector)retVal;
}
///
/// Creates an given a list of purposes.
///
/// The from which to generate the purpose chain.
/// The primary purpose used to create the .
/// An optional list of secondary purposes which contribute to the purpose chain.
/// If this list is provided it cannot contain null elements.
/// An tied to the provided purpose chain.
///
/// This is a convenience method which chains together several calls to
/// . See that method's
/// documentation for more information.
///
public static IDataProtector CreateProtector(this IDataProtectionProvider provider, string purpose, params string[] subPurposes)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (purpose == null)
{
throw new ArgumentNullException(nameof(purpose));
}
// The method signature isn't simply CreateProtector(this IDataProtectionProvider, params string[] purposes)
// because we don't want the code provider.CreateProtector() [parameterless] to inadvertently compile.
// The actual signature for this method forces at least one purpose to be provided at the call site.
IDataProtector protector = provider.CreateProtector(purpose);
if (subPurposes != null && subPurposes.Length > 0)
{
protector = protector?.CreateProtector((IEnumerable)subPurposes);
}
return protector ?? CryptoUtil.Fail("CreateProtector returned null.");
}
///
/// Retrieves an from an .
///
/// The service provider from which to retrieve the .
/// An . This method is guaranteed never to return null.
/// If no service exists in .
public static IDataProtectionProvider GetDataProtectionProvider(this IServiceProvider services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
// We have our own implementation of GetRequiredService since we don't want to
// take a dependency on DependencyInjection.Interfaces.
IDataProtectionProvider provider = (IDataProtectionProvider)services.GetService(typeof(IDataProtectionProvider));
if (provider == null)
{
throw new InvalidOperationException(Resources.FormatDataProtectionExtensions_NoService(typeof(IDataProtectionProvider).FullName));
}
return provider;
}
///
/// Retrieves an from an given a list of purposes.
///
/// An which contains the
/// from which to generate the purpose chain.
/// The list of purposes which contribute to the purpose chain. This list must
/// contain at least one element, and it may not contain null elements.
/// An tied to the provided purpose chain.
///
/// This is a convenience method which calls
/// then . See those methods'
/// documentation for more information.
///
public static IDataProtector GetDataProtector(this IServiceProvider services, IEnumerable purposes)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (purposes == null)
{
throw new ArgumentNullException(nameof(purposes));
}
return services.GetDataProtectionProvider().CreateProtector(purposes);
}
///
/// Retrieves an from an given a list of purposes.
///
/// An which contains the
/// from which to generate the purpose chain.
/// The primary purpose used to create the .
/// An optional list of secondary purposes which contribute to the purpose chain.
/// If this list is provided it cannot contain null elements.
/// An tied to the provided purpose chain.
///
/// This is a convenience method which calls
/// then . See those methods'
/// documentation for more information.
///
public static IDataProtector GetDataProtector(this IServiceProvider services, string purpose, params string[] subPurposes)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (purpose == null)
{
throw new ArgumentNullException(nameof(purpose));
}
return services.GetDataProtectionProvider().CreateProtector(purpose, subPurposes);
}
///
/// Cryptographically protects a piece of plaintext data.
///
/// The data protector to use for this operation.
/// The plaintext data to protect.
/// The protected form of the plaintext data.
public static string Protect(this IDataProtector protector, string plaintext)
{
if (protector == null)
{
throw new ArgumentNullException(nameof(protector));
}
if (plaintext == null)
{
throw new ArgumentNullException(nameof(plaintext));
}
try
{
byte[] plaintextAsBytes = EncodingUtil.SecureUtf8Encoding.GetBytes(plaintext);
byte[] protectedDataAsBytes = protector.Protect(plaintextAsBytes);
return WebEncoders.Base64UrlEncode(protectedDataAsBytes);
}
catch (Exception ex) when (ex.RequiresHomogenization())
{
// Homogenize exceptions to CryptographicException
throw Error.CryptCommon_GenericError(ex);
}
}
///
/// Cryptographically unprotects a piece of protected data.
///
/// The data protector to use for this operation.
/// The protected data to unprotect.
/// The plaintext form of the protected data.
///
/// Thrown if is invalid or malformed.
///
public static string Unprotect(this IDataProtector protector, string protectedData)
{
if (protector == null)
{
throw new ArgumentNullException(nameof(protector));
}
if (protectedData == null)
{
throw new ArgumentNullException(nameof(protectedData));
}
try
{
byte[] protectedDataAsBytes = WebEncoders.Base64UrlDecode(protectedData);
byte[] plaintextAsBytes = protector.Unprotect(protectedDataAsBytes);
return EncodingUtil.SecureUtf8Encoding.GetString(plaintextAsBytes);
}
catch (Exception ex) when (ex.RequiresHomogenization())
{
// Homogenize exceptions to CryptographicException
throw Error.CryptCommon_GenericError(ex);
}
}
}
}