Skip to content

Commit

Permalink
Optimize bruteforce protection, add .net 8 support. cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jan 2, 2024
1 parent 3b53252 commit 541eb1c
Show file tree
Hide file tree
Showing 29 changed files with 214 additions and 512 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Setup .NET 6.0
- name: Setup .NET 8.0
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
working-directory: ./src
run: dotnet restore
Expand Down
7 changes: 6 additions & 1 deletion src/Nager.Authentication.Abstraction/Entities/UserEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;

namespace Nager.Authentication.Abstraction.Entities
{
Expand All @@ -24,5 +25,9 @@ public class UserEntity

[MaxLength(32)]
public byte[]? PasswordHash { get; set; }

public DateTime LastValidationTimestamp { get; set; }

public DateTime LastSuccessfulValidationTimestamp { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Nager.Authentication.Abstraction.Models
public class AuthenticationInfo
{
public DateTime LastValid { get; set; }
public int InvalidCount { get; set; }

public DateTime LastInvalid { get; set; }

public int InvalidCount { get; set; }
}
}
8 changes: 7 additions & 1 deletion src/Nager.Authentication.Abstraction/Models/UserInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nager.Authentication.Abstraction.Models
using System;

namespace Nager.Authentication.Abstraction.Models
{
public class UserInfo
{
Expand All @@ -11,5 +13,9 @@ public class UserInfo
public string? Firstname { get; set; }

public string? Lastname { get; set; }

public DateTime LastValidationTimestamp { get; set; }

public DateTime LastSuccessfulValidationTimestamp { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Nullable>enable</Nullable>
<TargetFramework>netstandard2.1</TargetFramework>

<Version>1.0.1</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ Task<bool> UpdateAsync(
Task<bool> DeleteAsync(
Expression<Func<UserEntity, bool>> predicate,
CancellationToken cancellationToken = default);

Task<bool> SetLastValidationTimestampAsync(
Expression<Func<UserEntity, bool>> predicate,
CancellationToken cancellationToken = default);

Task<bool> SetLastSuccessfulValidationTimestampAsync(
Expression<Func<UserEntity, bool>> predicate,
CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public async Task<ActionResult<UserInfoDto>> GetUserAsync(
EmailAddress = userInfo.EmailAddress,
Firstname = userInfo.Firstname,
Lastname = userInfo.Lastname,
Roles = userInfo.Roles
Roles = userInfo.Roles,
LastValidationTimestamp = userInfo.LastValidationTimestamp,
LastSuccessfulValidationTimestamp = userInfo.LastSuccessfulValidationTimestamp
};

return StatusCode(StatusCodes.Status200OK, item);
Expand All @@ -90,7 +92,9 @@ public async Task<ActionResult<UserInfoDto[]>> QueryUsersAsync(
EmailAddress = userInfo.EmailAddress,
Firstname = userInfo.Firstname,
Lastname = userInfo.Lastname,
Roles = userInfo.Roles
Roles = userInfo.Roles,
LastValidationTimestamp = userInfo.LastValidationTimestamp,
LastSuccessfulValidationTimestamp = userInfo.LastSuccessfulValidationTimestamp
});

return StatusCode(StatusCodes.Status200OK, items);
Expand Down
8 changes: 7 additions & 1 deletion src/Nager.Authentication.AspNet/Dtos/UserInfoDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nager.Authentication.AspNet.Dtos
using System;

namespace Nager.Authentication.AspNet.Dtos
{
public class UserInfoDto
{
Expand All @@ -11,5 +13,9 @@ public class UserInfoDto
public string Firstname { get; set; }

public string Lastname { get; set; }

public DateTime LastValidationTimestamp { get; set; }

public DateTime LastSuccessfulValidationTimestamp { get; set; }
}
}
18 changes: 12 additions & 6 deletions src/Nager.Authentication.AspNet/Nager.Authentication.AspNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@
<PackageProjectUrl>https://github.com/nager/Nager.Authentication</PackageProjectUrl>
<PackageTags>Authentication</PackageTags>

<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<Nullable>enable</Nullable>

<Version>1.0.9</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.19" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.25" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.19" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.25" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="7.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="7.0.14" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.14" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,35 @@ public Task<bool> DeleteAsync(
var isSuccessful = this._userInfos.TryRemove(item.Id, out _);
return Task.FromResult(isSuccessful);
}

public Task<bool> SetLastValidationTimestampAsync(
Expression<Func<UserEntity, bool>> predicate,
CancellationToken cancellationToken = default)
{
var item = this._userInfos.Values.AsQueryable().Where(predicate).FirstOrDefault();
if (item == null)
{
return Task.FromResult(false);
}

item.LastValidationTimestamp = DateTime.UtcNow;

return Task.FromResult(true);
}

public Task<bool> SetLastSuccessfulValidationTimestampAsync(
Expression<Func<UserEntity, bool>> predicate,
CancellationToken cancellationToken = default)
{
var item = this._userInfos.Values.AsQueryable().Where(predicate).FirstOrDefault();
if (item == null)
{
return Task.FromResult(false);
}

item.LastSuccessfulValidationTimestamp = DateTime.UtcNow;

return Task.FromResult(true);
}
}
}
23 changes: 0 additions & 23 deletions src/Nager.Authentication.MssqlRepository/DatabaseContext.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/Nager.Authentication.MssqlRepository/DatabaseContextFactory.cs

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 541eb1c

Please sign in to comment.