Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Updating packages and Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Nov 16, 2017
1 parent ac89822 commit 27062ed
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 251 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Unosquare.Swan.AspNetCore.Sample.Database;
using Microsoft.AspNetCore.Authorization;

namespace Unosquare.Swan.AspNetCore.Sample.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace Unosquare.Swan.AspNetCore.Sample.Controllers
{
Expand Down
13 changes: 5 additions & 8 deletions src/Unosquare.Swan.AspNetCore.Sample/Database/AuditTrailEntry.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Unosquare.Swan.AspNetCore.Models;

namespace Unosquare.Swan.AspNetCore.Sample.Database
namespace Unosquare.Swan.AspNetCore.Sample.Database
{
using System;
using System.ComponentModel.DataAnnotations;
using Models;

public class AuditTrailEntry :IAuditTrailEntry
{
[Key]
Expand Down
10 changes: 3 additions & 7 deletions src/Unosquare.Swan.AspNetCore.Sample/Database/Product.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Unosquare.Swan.AspNetCore.Sample.Database
namespace Unosquare.Swan.AspNetCore.Sample.Database
{
using System.ComponentModel.DataAnnotations;

public class Product
{
[Key]
Expand Down
3 changes: 1 addition & 2 deletions src/Unosquare.Swan.AspNetCore.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Unosquare.Swan.AspNetCore.Sample
{
using AspNetCore;
using Database;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -46,7 +45,7 @@ public Startup(IHostingEnvironment env)
}

public IConfigurationRoot Configuration { get; }
private TokenValidationParameters ValidationParameters { get; set; }
private TokenValidationParameters ValidationParameters { get; }

// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Unosquare.Swan.AspNetCore/AuditTrailController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Unosquare.Swan.AspNetCore.Models;
using Unosquare.Swan.Formatters;
using Models;
using Formatters;

/// <summary>
/// Represents an AuditTrail controller to use with BusinessDbContext
Expand Down
6 changes: 3 additions & 3 deletions src/Unosquare.Swan.AspNetCore/BusinessRuleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class BusinessRuleAttribute : Attribute
/// <param name="actionFlags">The action flags.</param>
public BusinessRuleAttribute(ActionFlags actionFlags)
{
this.Action = actionFlags;
Action = actionFlags;
}

/// <summary>
Expand All @@ -53,8 +53,8 @@ public BusinessRuleAttribute(ActionFlags actionFlags)
/// <param name="actionFlags">The action flags.</param>
public BusinessRuleAttribute(Type[] entityTypes, ActionFlags actionFlags)
{
this.EntityTypes = entityTypes;
this.Action = actionFlags;
EntityTypes = entityTypes;
Action = actionFlags;
}

/// <summary>
Expand Down
10 changes: 6 additions & 4 deletions src/Unosquare.Swan.AspNetCore/BusinessRulesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public void RunBusinessRules()

private void ExecuteBusinessRulesMethods(EntityState state, ActionFlags action, MethodInfo[] methodInfoSet)
{
var selfTrackingEntries = Context.ChangeTracker.Entries().Where(x => x.State == state).ToList();
var selfTrackingEntries = Context.ChangeTracker.Entries()
.Where(x => x.State == state)
.ToList();

foreach (var entry in selfTrackingEntries)
{
Expand All @@ -103,12 +105,12 @@ private void ExecuteBusinessRulesMethods(EntityState state, ActionFlags action,
var methods = methodInfoSet.Where(m => m.GetCustomAttributes(typeof(BusinessRuleAttribute), true)
.Select(a => a as BusinessRuleAttribute)
.Any(b => b != null && (b.EntityTypes == null ||
b.EntityTypes.Any(t => t == entityType)) &&
b.Action == action));
b.EntityTypes.Any(t => t == entityType)) &&
b.Action == action));

foreach (var methodInfo in methods)
{
methodInfo.Invoke(this, new[] { entity });
methodInfo.Invoke(this, new[] {entity});
}
}
}
Expand Down
120 changes: 57 additions & 63 deletions src/Unosquare.Swan.AspNetCore/TokenProviderMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,63 @@ public Task Invoke(HttpContext context)
return _next(context);
}

private static void ThrowIfInvalidOptions(TokenProviderOptions options)
{
if (string.IsNullOrEmpty(options.Path))
{
throw new ArgumentNullException(nameof(TokenProviderOptions.Path));
}

if (string.IsNullOrEmpty(options.Issuer))
{
throw new ArgumentNullException(nameof(TokenProviderOptions.Issuer));
}

if (string.IsNullOrEmpty(options.Audience))
{
throw new ArgumentNullException(nameof(TokenProviderOptions.Audience));
}

if (options.Expiration == TimeSpan.Zero)
{
throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(TokenProviderOptions.Expiration));
}

if (options.IdentityResolver == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver));
}

if (options.SigningCredentials == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.SigningCredentials));
}

if (options.NonceGenerator == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.NonceGenerator));
}

if (options.IdentityResolver == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver));
}

if (options.BearerTokenResolver == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.BearerTokenResolver));
}
}

private static string SerializeError(string description, string error = "invalid_grant")
{
return Json.Serialize(new
{
error,
error_description = description
});
}

private async Task GenerateToken(HttpContext context)
{
JwtSecurityToken jwt;
Expand Down Expand Up @@ -178,68 +235,5 @@ private async Task GenerateToken(HttpContext context)

await context.Response.WriteAsync(Json.Serialize(await _options.BearerTokenResolver(identity, responseInfo)));
}

private static void ThrowIfInvalidOptions(TokenProviderOptions options)
{
if (string.IsNullOrEmpty(options.Path))
{
throw new ArgumentNullException(nameof(TokenProviderOptions.Path));
}

if (string.IsNullOrEmpty(options.Issuer))
{
throw new ArgumentNullException(nameof(TokenProviderOptions.Issuer));
}

if (string.IsNullOrEmpty(options.Audience))
{
throw new ArgumentNullException(nameof(TokenProviderOptions.Audience));
}

if (options.Expiration == TimeSpan.Zero)
{
throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(TokenProviderOptions.Expiration));
}

if (options.IdentityResolver == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver));
}

if (options.SigningCredentials == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.SigningCredentials));
}

if (options.NonceGenerator == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.NonceGenerator));
}

if (options.IdentityResolver == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver));
}

if (options.BearerTokenResolver == null)
{
throw new ArgumentNullException(nameof(TokenProviderOptions.BearerTokenResolver));
}
}

/// <summary>
/// Serializes the error.
/// </summary>
/// <param name="description">The description.</param>
/// <param name="error">The error.</param>
/// <returns>The error in JSON format</returns>
private static string SerializeError(string description, string error = "invalid_grant")
{
return Json.Serialize(new
{
error,
error_description = description
});
}
}
}
16 changes: 8 additions & 8 deletions src/Unosquare.Swan.AspNetCore/Unosquare.Swan.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>Bearer Token and logging to fast start any ASP.NET Core project</Description>
<Copyright>Copyright (c) 2016-2017 - Unosquare</Copyright>
<AssemblyTitle>Unosquare SWAN AspNet Core</AssemblyTitle>
<VersionPrefix>0.9.2</VersionPrefix>
<VersionPrefix>0.9.3</VersionPrefix>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<DebugType>Full</DebugType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand All @@ -17,16 +17,16 @@
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.1.4" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.1.4" />
<PackageReference Include="Unosquare.Swan" Version="0.15.0" />
<PackageReference Include="Unosquare.Swan" Version="0.18.0" />
</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions test/Unosquare.Swan.AspNetCore.Test/AuditTrailTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[TestFixture]
class AuditTrailTest
{
private ProductMock product;
private ProductMock _product;

private BusinessDbContextMock SetupDatabase(string name)
{
Expand All @@ -22,15 +22,15 @@ private BusinessDbContextMock SetupDatabase(string name)
[SetUp]
public void SetUp()
{
product = ProductMock.GetProduct();
_product = ProductMock.GetProduct();
}

[Test]
public async Task SaveChangesEntityTestAsync()
{
using (var context = SetupDatabase(nameof(SaveChangesEntityTestAsync)))
{
context.Add(product);
context.Add(_product);
await context.SaveChangesAsync();

var audit = context.AuditTrailEntries.Last();
Expand All @@ -46,7 +46,7 @@ public void SaveChangesEntityTest()
using (var context = SetupDatabase(nameof(SaveChangesEntityTest)))
{

context.Add(product);
context.Add(_product);
context.SaveChanges();

var audit = context.AuditTrailEntries.Last();
Expand All @@ -61,10 +61,10 @@ public void UpdatedChangesEntityTest()
{
using (var context = SetupDatabase(nameof(UpdatedChangesEntityTest)))
{
context.Add(product);
context.Add(_product);
context.SaveChanges();

context.Update(product);
context.Update(_product);
context.SaveChanges();

var audit = context.AuditTrailEntries.Last();
Expand All @@ -79,10 +79,10 @@ public void DeleteChangesEntityTest()
{
using (var context = SetupDatabase(nameof(DeleteChangesEntityTest)))
{
context.Add(product);
context.Add(_product);
context.SaveChanges();

context.Remove(product);
context.Remove(_product);
context.SaveChanges();

var audit = context.AuditTrailEntries.Last();
Expand Down
Loading

0 comments on commit 27062ed

Please sign in to comment.