-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
322 additions
and
1,599 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
global using System.Net.Http.Headers; | ||
global using Microsoft.AspNetCore.Identity; | ||
global using Microsoft.AspNetCore.Mvc; | ||
global using Microsoft.AspNetCore.Mvc.Testing; | ||
global using Microsoft.Data.Sqlite; | ||
global using Microsoft.EntityFrameworkCore; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.DependencyInjection.Extensions; | ||
global using Microsoft.Extensions.Hosting; | ||
global using UsersDbContext = Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<TodoApi.TodoUser>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\TodoApi.Tests\DbContextExtensions.cs" Link="DbContextExtensions.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="all" /> | ||
<PackageReference Include="coverlet.collector" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TodoApi.Identity\TodoApi.Identity.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Using Include="System.Net.Http.Json" /> | ||
<Using Include="System.Net.Http" /> | ||
<Using Include="System.Net" /> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.AspNetCore.DataProtection; | ||
|
||
namespace TodoApi.Tests; | ||
|
||
internal class TodoApplication : WebApplicationFactory<Program> | ||
{ | ||
private readonly SqliteConnection _sqliteConnection = new("Filename=:memory:"); | ||
|
||
public UsersDbContext CreateTodoDbContext() | ||
{ | ||
var db = Services.GetRequiredService<IDbContextFactory<UsersDbContext>>().CreateDbContext(); | ||
db.Database.EnsureCreated(); | ||
return db; | ||
} | ||
|
||
public async Task CreateUserAsync(string username, string? password = null) | ||
{ | ||
using var scope = Services.CreateScope(); | ||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TodoUser>>(); | ||
var newUser = new TodoUser { Id = username }; | ||
var result = await userManager.CreateAsync(newUser, password ?? Guid.NewGuid().ToString()); | ||
Assert.True(result.Succeeded); | ||
} | ||
|
||
protected override IHost CreateHost(IHostBuilder builder) | ||
{ | ||
// Open the connection, this creates the SQLite in-memory database, which will persist until the connection is closed | ||
_sqliteConnection.Open(); | ||
|
||
builder.ConfigureServices(services => | ||
{ | ||
// We're going to use the factory from our tests | ||
services.AddDbContextFactory<UsersDbContext>(); | ||
// We need to replace the configuration for the DbContext to use a different configured database | ||
services.AddDbContextOptions<UsersDbContext>(o => o.UseSqlite(_sqliteConnection)); | ||
// Lower the requirements for the tests | ||
services.Configure<IdentityOptions>(o => | ||
{ | ||
o.Password.RequireNonAlphanumeric = false; | ||
o.Password.RequireDigit = false; | ||
o.Password.RequiredUniqueChars = 0; | ||
o.Password.RequiredLength = 1; | ||
o.Password.RequireLowercase = false; | ||
o.Password.RequireUppercase = false; | ||
}); | ||
// Since tests run in parallel, it's possible multiple servers will startup, | ||
// we use an ephemeral key provider and repository to avoid filesystem contention issues | ||
services.AddSingleton<IDataProtectionProvider, EphemeralDataProtectionProvider>(); | ||
}); | ||
|
||
return base.CreateHost(builder); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
_sqliteConnection?.Dispose(); | ||
base.Dispose(disposing); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace TodoApi; | ||
|
||
public static class DataProtectionExtensions | ||
{ | ||
public static IServiceCollection AddSharedKeys(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var keysConnectionString = configuration.GetConnectionString("Keys") ?? "Data Source=../Keys.db"; | ||
services.AddSqlite<SharedKeysDb>(keysConnectionString); | ||
|
||
services.AddDataProtection() | ||
.SetApplicationName("TodoApp") | ||
.PersistKeysToDbContext<SharedKeysDb>(); | ||
|
||
return services; | ||
} | ||
} | ||
|
||
public class SharedKeysDb(DbContextOptions<SharedKeysDb> options) : DbContext(options), IDataProtectionKeyContext | ||
{ | ||
public DbSet<DataProtectionKey> DataProtectionKeys => Set<DataProtectionKey>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
using TodoApi; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddAuthentication().AddIdentityBearerToken<TodoUser>(); | ||
Check failure on line 8 in TodoApi.Identity/Program.cs GitHub Actions / build
Check failure on line 8 in TodoApi.Identity/Program.cs GitHub Actions / build
|
||
|
||
builder.Services.AddSharedKeys(builder.Configuration); | ||
|
||
var connectionString = builder.Configuration.GetConnectionString("Users") ?? "Data Source=.db/Users.db"; | ||
builder.Services.AddSqlite<IdentityDbContext<TodoUser>>(connectionString); | ||
|
||
// Configure Open API | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(o => o.AddOpenApiSecurity()); | ||
|
||
// Configure identity | ||
builder.Services.AddIdentityCore<TodoUser>() | ||
.AddEntityFrameworkStores<IdentityDbContext<TodoUser>>() | ||
.AddApiEndpoints(); | ||
|
||
var app = builder.Build(); | ||
|
||
await MakeDb(app.Services); | ||
|
||
async Task MakeDb(IServiceProvider sp) | ||
{ | ||
await using var scope = sp.CreateAsyncScope(); | ||
|
||
var db0 = scope.ServiceProvider.GetRequiredService<SharedKeysDb>(); | ||
var db1 = scope.ServiceProvider.GetRequiredService<IdentityDbContext<TodoUser>>(); | ||
|
||
await db0.Database.EnsureCreatedAsync(); | ||
await db1.Database.EnsureCreatedAsync(); | ||
} | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.MapUsers(); | ||
|
||
app.Map("/", () => Results.Redirect("/swagger")); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:6521", | ||
"sslPort": 44327 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:5210", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:7104;http://localhost:5210", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\TodoApi\Extensions\OpenApiExtensions.cs" Link="Extensions\OpenApiExtensions.cs" /> | ||
<Compile Include="..\TodoApi\Extensions\RateLimitExtensions.cs" Link="Extensions\RateLimitExtensions.cs" /> | ||
<Compile Include="..\TodoApi\Filters\ValidationFilter.cs" Link="Filters\ValidationFilter.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" /> | ||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" /> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" /> | ||
<PackageReference Include="Microsoft.OpenApi" /> | ||
<PackageReference Include="MiniValidation" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Filters\" /> | ||
<InternalsVisibleTo Include="TodoApi.Identity.Tests" /> | ||
</ItemGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.