зеркало из https://github.com/microsoft/Omex.git
resolve comments
This commit is contained in:
Родитель
3a4bd0e03d
Коммит
f7e7f220cf
|
@ -11,10 +11,10 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.Middlewares
|
|||
{
|
||||
internal class EmailBasedUserIdentityProvider : IUserIdentityProvider
|
||||
{
|
||||
private static int MaxBytesPerChar { get; } = 4; // UTF-8
|
||||
private const int MaxBytesPerChar = 4; // UTF-8
|
||||
public int MaxBytesInIdentity { get; } = 256 * MaxBytesPerChar; // maximum email address length plus max bytes per char
|
||||
|
||||
public class UserEmail
|
||||
private class UserEmail
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
}
|
||||
|
@ -37,8 +37,10 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.Middlewares
|
|||
bytesWritten += MaxBytesPerChar;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult((success, bytesWritten));
|
||||
}
|
||||
|
||||
catch {
|
||||
return Task.FromResult((false, -1));
|
||||
}
|
||||
|
|
|
@ -17,8 +17,6 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.Middlewares
|
|||
internal class StaticSaltProvider : ISaltProvider
|
||||
{
|
||||
private readonly IMemoryOwner<byte> m_currentSaltMemory;
|
||||
// string salt;
|
||||
// byte[] saltValue = Encoding.UTF8.GetBytes(salt);
|
||||
private readonly byte[] m_saltValue;
|
||||
private readonly ILogger<RotatingSaltProvider> m_logger;
|
||||
|
||||
|
|
|
@ -13,40 +13,40 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.UnitTests
|
|||
public class EmailBasedUserIdentityProviderTests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task TryWriteBytes_ReturnFalseIfNoEmail()
|
||||
public async Task TryWriteBytes_NoEmail_ReturnFalse()
|
||||
{
|
||||
IUserIdentityProvider provider = new EmailBasedUserIdentityProvider();
|
||||
(HttpContext context, _) = HttpContextHelper.CreateHttpContext();
|
||||
Memory<byte> memory = new byte[provider.MaxBytesInIdentity];
|
||||
(bool result, int bytesWritten) = await provider.TryWriteBytesAsync(context, memory).ConfigureAwait(false);
|
||||
(bool result, int bytesWritten) = await provider.TryWriteBytesAsync(context, memory);
|
||||
Assert.IsFalse(result);
|
||||
Assert.AreEqual(-1, bytesWritten);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TryWriteBytes_ChangedBaseOnEmail()
|
||||
public async Task TryWriteBytes_DifferentEmail_HashValueChanged()
|
||||
{
|
||||
IUserIdentityProvider provider = new EmailBasedUserIdentityProvider();
|
||||
|
||||
HttpContext context1 = HttpContextHelper.GetContextWithEmail("Abc123@outlook.com");
|
||||
HttpContext context2 = HttpContextHelper.GetContextWithEmail("Abc456@gmail.com");
|
||||
|
||||
byte[] hash1 = await GetIdentityAsync(provider, context1).ConfigureAwait(false);
|
||||
byte[] hash2 = await GetIdentityAsync(provider, context2).ConfigureAwait(false);
|
||||
byte[] hash1 = await GetIdentityAsync(provider, context1);
|
||||
byte[] hash2 = await GetIdentityAsync(provider, context2);
|
||||
|
||||
CollectionAssert.AreNotEqual(hash1, hash2);
|
||||
|
||||
HttpContext context3 = HttpContextHelper.GetContextWithEmail("Abc123@outlook.com");
|
||||
HttpContext context4 = HttpContextHelper.GetContextWithEmail("Abc456@gmail.com");
|
||||
|
||||
CollectionAssert.AreEqual(hash1, await GetIdentityAsync(provider, context3).ConfigureAwait(false));
|
||||
CollectionAssert.AreEqual(hash2, await GetIdentityAsync(provider, context4).ConfigureAwait(false));
|
||||
CollectionAssert.AreEqual(hash1, await GetIdentityAsync(provider, context3));
|
||||
CollectionAssert.AreEqual(hash2, await GetIdentityAsync(provider, context4));
|
||||
}
|
||||
|
||||
private async Task<byte[]> GetIdentityAsync(IUserIdentityProvider provider, HttpContext context)
|
||||
{
|
||||
Memory<byte> memory = new byte[provider.MaxBytesInIdentity];
|
||||
(_, int bytes) = await provider.TryWriteBytesAsync(context, memory).ConfigureAwait(false);
|
||||
(_, int bytes) = await provider.TryWriteBytesAsync(context, memory);
|
||||
Assert.IsTrue(provider.MaxBytesInIdentity >= bytes, "Written size bigger then max size");
|
||||
return memory.Span.ToArray();
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ using System.Net;
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Moq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Omex.Extensions.Hosting.Services.Web.UnitTests
|
||||
{
|
||||
|
@ -22,7 +23,7 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.UnitTests
|
|||
public static HttpContext GetContextWithEmail(string email)
|
||||
{
|
||||
(HttpContext context, _) = CreateHttpContext();
|
||||
byte[] emailBytes = System.Text.Encoding.UTF8.GetBytes($"{{\"Email\":\"{email}\"}}");
|
||||
byte[] emailBytes = Encoding.UTF8.GetBytes($"{{\"Email\":\"{email}\"}}");
|
||||
context.Request.Body.Write(emailBytes, 0, emailBytes.Length);
|
||||
return context;
|
||||
}
|
||||
|
@ -34,7 +35,7 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.UnitTests
|
|||
FeatureCollection features = new();
|
||||
features.Set<IHttpConnectionFeature>(feature);
|
||||
|
||||
Stream requestBody = new MemoryStream();
|
||||
using Stream requestBody = new MemoryStream();
|
||||
|
||||
Mock<HttpContext> contextMock = new();
|
||||
contextMock.SetupGet(c => c.Features).Returns(features);
|
||||
|
|
|
@ -68,12 +68,12 @@ namespace Microsoft.Omex.Extensions.Hosting.Services.Web.UnitTests
|
|||
TestIdentityProvider provider = new ("ProviderWrapper", new EmailBasedUserIdentityProvider());
|
||||
|
||||
random.NextBytes(saltValue);
|
||||
TestStaticSaltProvider saltProvider1 = new(saltValue);
|
||||
using TestStaticSaltProvider saltProvider1 = new(saltValue);
|
||||
UserHashIdentityMiddleware middleware1 = GetMiddelware(saltProvider: saltProvider1, provider);
|
||||
string initialHash = await middleware1.CreateUserHashAsync(context).ConfigureAwait(false);
|
||||
|
||||
random.NextBytes(saltValue);
|
||||
TestStaticSaltProvider saltProvider2 = new(saltValue);
|
||||
using TestStaticSaltProvider saltProvider2 = new(saltValue);
|
||||
UserHashIdentityMiddleware middleware2 = GetMiddelware(saltProvider: saltProvider2, provider);
|
||||
string changedHash = await middleware2.CreateUserHashAsync(context).ConfigureAwait(false);
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче