XMLDoc updates. Seperation of classes.

This commit is contained in:
Barry Dorrans 2015-01-08 11:09:08 -08:00
parent 0e7755ab79
commit 53b994da19
3 changed files with 96 additions and 32 deletions

View File

@ -1,24 +1,28 @@
using Microsoft.AspNet.Security.DataProtection; // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
using Microsoft.Framework.OptionsModel; // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Identity namespace Microsoft.AspNet.Identity
{ {
public class DataProtectionTokenProviderOptions
{
public string Name { get; set; } = "DataProtection";
public TimeSpan TokenLifespan { get; set; } = TimeSpan.FromDays(1);
}
/// <summary> /// <summary>
/// Token provider that uses an IDataProtector to generate encrypted tokens based off of the security stamp /// Provides protection and validation of identity tokens.
/// </summary> /// </summary>
/// <typeparam name="TUser">The type used to represent a user.</typeparam>
public class DataProtectorTokenProvider<TUser> : IUserTokenProvider<TUser> where TUser : class public class DataProtectorTokenProvider<TUser> : IUserTokenProvider<TUser> where TUser : class
{ {
/// <summary>
/// Initializes a new instance of the <see cref="DataProtectorTokenProvider{TUser}"/> class.
/// </summary>
/// <param name="dataProtectionProvider">The system data protection provider.</param>
/// <param name="options">The configured <see cref="DataProtectionTokenProviderOptions"/>.</param>
public DataProtectorTokenProvider(IDataProtectionProvider dataProtectionProvider, IOptions<DataProtectionTokenProviderOptions> options) public DataProtectorTokenProvider(IDataProtectionProvider dataProtectionProvider, IOptions<DataProtectionTokenProviderOptions> options)
{ {
if (options == null || options.Options == null) if (options == null || options.Options == null)
@ -34,18 +38,38 @@ namespace Microsoft.AspNet.Identity
Protector = dataProtectionProvider.CreateProtector(Name ?? "DataProtectorTokenProvider"); Protector = dataProtectionProvider.CreateProtector(Name ?? "DataProtectorTokenProvider");
} }
/// <summary>
/// Gets the <see cref="DataProtectionTokenProviderOptions"/> for this instance.
/// </summary>
/// <value>
/// The <see cref="DataProtectionTokenProviderOptions"/> for this instance.
/// </value>
public DataProtectionTokenProviderOptions Options { get; private set; } public DataProtectionTokenProviderOptions Options { get; private set; }
/// <summary>
/// Gets the <see cref="IDataProtector"/> for this instance.
/// </summary>
/// <value>
/// The <see cref="IDataProtector"/> for this instance.
/// </value>
public IDataProtector Protector { get; private set; } public IDataProtector Protector { get; private set; }
/// <summary>
/// Gets the name of this instance.
/// </summary>
/// <value>
/// The name of this instance.
/// </value>
public string Name { get { return Options.Name; } } public string Name { get { return Options.Name; } }
/// <summary> /// <summary>
/// Generate a protected string for a user /// Generates a protected token for the specified <paramref name="User"/>.
/// </summary> /// </summary>
/// <param name="purpose"></param> /// <param name="purpose">The purpose the token will be used for.</param>
/// <param name="manager"></param> /// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve user properties from.</param>
/// <param name="user"></param> /// <param name="user">The <see cref="TUser"/> the token will be generated from.</param>
/// <returns></returns> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
/// <returns>A <see cref="Task{TResult}"/> that contains the protected token.</returns>
public async Task<string> GenerateAsync(string purpose, UserManager<TUser> manager, TUser user, public async Task<string> GenerateAsync(string purpose, UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken)) CancellationToken cancellationToken = default(CancellationToken))
{ {
@ -72,13 +96,14 @@ namespace Microsoft.AspNet.Identity
} }
/// <summary> /// <summary>
/// Return false if the token is not valid /// Validates the protected <paramref name="token"/> for the specified <paramref name="user"/> and <paramref name="purpose"/>.
/// </summary> /// </summary>
/// <param name="purpose"></param> /// <param name="purpose">The purpose the token was be used for.</param>
/// <param name="token"></param> /// <param name="token">The token to validate.</param>
/// <param name="manager"></param> /// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve user properties from.</param>
/// <param name="user"></param> /// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
/// <returns></returns> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
/// <returns>A <see cref="Task{TResult}"/> that is true if the token is valid, otherwise false.</returns>
public async Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user, public async Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken)) CancellationToken cancellationToken = default(CancellationToken))
{ {
@ -128,11 +153,13 @@ namespace Microsoft.AspNet.Identity
} }
/// <summary> /// <summary>
/// Returns false because tokens are two long to be used for two factor /// Returns a <see cref="boolean"/> indicating whether a token generated by this instance
/// can be used as a Two Factor Authentication token.
/// </summary> /// </summary>
/// <param name="manager"></param> /// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve user properties from.</param>
/// <param name="user"></param> /// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
/// <returns></returns> /// <returns>True if a token generated by this instance can be used as a Two Factor Authentication token, otherwise false.</returns>
/// <remarks>This method will always return false for instances of <see cref="DataProtectorTokenProvider{TUser}"/>.</remarks>
public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user, public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken)) CancellationToken cancellationToken = default(CancellationToken))
{ {
@ -140,12 +167,13 @@ namespace Microsoft.AspNet.Identity
} }
/// <summary> /// <summary>
/// This provider no-ops by default when asked to notify a user /// Creates a notification task for A <paramref name="user"/> based on the supplied <paramref name="token"/>.
/// </summary> /// </summary>
/// <param name="token"></param> /// <param name="token">The token to generate notifications for..</param>
/// <param name="manager"></param> /// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve user properties from.</param>
/// <param name="user"></param> /// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
/// <returns></returns> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the started task.</returns>
public Task NotifyAsync(string token, UserManager<TUser> manager, TUser user, public Task NotifyAsync(string token, UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken)) CancellationToken cancellationToken = default(CancellationToken))
{ {
@ -153,7 +181,9 @@ namespace Microsoft.AspNet.Identity
} }
} }
// Based on Levi's authentication sample /// <summary>
/// Utility extensions to streams
/// </summary>
internal static class StreamExtensions internal static class StreamExtensions
{ {
internal static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true); internal static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true);

View File

@ -0,0 +1,29 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.Identity
{
/// <summary>
/// Contains options for the <see cref="DataProtectorTokenProvider{TUser}"/>.
/// </summary>
public class DataProtectionTokenProviderOptions
{
/// <summary>
/// Gets or sets the name of the <see cref="DataProtectorTokenProvider{TUser}"/>.
/// </summary>
/// <value>
/// The name of the <see cref="DataProtectorTokenProvider{TUser}"/>.
/// </value>
public string Name { get; set; } = "DataProtection";
/// <summary>
/// Gets or sets the amount of time a generated token remains valid.
/// </summary>
/// <value>
/// The amount of time a generated token remains valid.
/// </value>
public TimeSpan TokenLifespan { get; set; } = TimeSpan.FromDays(1);
}
}

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
@ -14,4 +14,9 @@
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project> <ProjectExtensions>
<VisualStudio>
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
</VisualStudio>
</ProjectExtensions>
</Project>