Copy generated code

This commit is contained in:
2024-10-18 20:35:41 +02:00
parent 4001ba1ba5
commit 21a61e2601
6 changed files with 136 additions and 23 deletions
+1 -3
View File
@@ -9,14 +9,12 @@
<InvariantGlobalization>true</InvariantGlobalization>
<PublishAot>true</PublishAot>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<TrimMode>partial</TrimMode>
<IsTrimmable>false</IsTrimmable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Net.Sdk.Web.Extensions" Version="0.8.4" />
<PackageReference Include="Net.Sdk.Web.Extensions.SourceGenerators" Version="0.8.17" />
<PackageReference Include="Net.Sdk.Web.Extensions.SourceGenerators" Version="0.8.20" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.2" />
+2
View File
@@ -9,6 +9,7 @@ using Net.Sdk.Web;
using System.Core.Extensions;
using System.Diagnostics.CodeAnalysis;
using System.Extensions;
using System.Runtime.CompilerServices;
namespace Badge.Controllers;
@@ -55,6 +56,7 @@ public sealed class OAuthController
}
[GeneratePost("token")]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining | MethodImplOptions.PreserveSig)]
public async Task<IResult> GetOAuthToken(
HttpContext httpContext,
CancellationToken cancellationToken)
+1
View File
@@ -13,6 +13,7 @@ RUN dotnet restore
RUN dotnet clean
RUN dotnet build -c $BUILD_CONFIGURATION -o /app/build
RUN dotnet publish Badge.csproj -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true -v detailed
RUN dotnet --info >> /app/publish/dotnet.info
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final
ENV ASPNETCORE_ENVIRONMENT=Production
+132 -11
View File
@@ -1,9 +1,10 @@
using Badge.Controllers;
using Badge.Controllers.Models;
using Badge.Extensions;
using Badge.Options;
using Microsoft.Extensions.Options;
using Badge.Filters;
using Badge.Models;
using Microsoft.AspNetCore.Mvc;
using Net.Sdk.Web;
using Net.Sdk.Web.Options;
using Net.Sdk.Web.Websockets.Extensions;
using System.Runtime.CompilerServices;
@@ -46,16 +47,136 @@ namespace Badge
.UseWwwRoot()
.UseAntiforgeryMiddleware()
.UseUserAuthentication()
.UseHealthChecks()
.UseRoutes();
app.MapPost("/test", async (HttpContext context, OAuthController route) =>
{
var cancellationToken = context.RequestAborted;
return await route.GetOAuthToken(context, cancellationToken);
});
.UseHealthChecks();
//.UseRoutes(); //This call doesn't work. When built with NativeAOT, some of the mappings don't await but instead they return and try to serialize Task<IResult>
UseRoutes2(app); //This is a copy of UseRoutes and placed below. This works for some reason and the mapping always await and never try to serialize Task<IResult>
app.Run();
}
// Identical copy of UseRoutes
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization | MethodImplOptions.PreserveSig)]
public static WebApplication UseRoutes2(WebApplication builder)
{
builder.MapPost("/api/users/login", (HttpContext httpContext, UsersController route, [FromBody] UsernameWithPassword payload) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.Login(payload, httpContext, cancellationToken);
});
builder.MapPost("/api/users/create", (HttpContext httpContext, UsersController route, [FromBody] UsernameWithPassword payload) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.Create(payload, httpContext, cancellationToken);
});
builder.MapGet("/api/users/me", (HttpContext httpContext, UsersController route, [FromServices] AuthenticatedUser authenticatedUser) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.Me(authenticatedUser);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapGet("/api/applications/{applicationId}/redirect-uris/", (HttpContext httpContext, RedirectUriController route, [FromServices] AuthenticatedUser authenticatedUser, string applicationId) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetRedirectUris(authenticatedUser, applicationId, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapPost("/api/applications/{applicationId}/redirect-uris/", (HttpContext httpContext, RedirectUriController route, [FromServices] AuthenticatedUser authenticatedUser, string applicationId, [FromBody] List<string> redirectUris) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.PostRedirectUris(authenticatedUser, applicationId, redirectUris, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapGet("/api/applications/{applicationId}/secrets/", (HttpContext httpContext, ClientSecretController route, string applicationId, [FromServices] AuthenticatedUser authenticatedUser) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetClientSecrets(applicationId, authenticatedUser, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapPost("/api/applications/{applicationId}/secrets/", (HttpContext httpContext, ClientSecretController route, string applicationId, [FromServices] AuthenticatedUser authenticatedUser) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.CreateClientSecret(applicationId, authenticatedUser, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapDelete("/api/applications/{applicationId}/secrets/{clientSecretId}", (HttpContext httpContext, ClientSecretController route, string applicationId, [FromServices] AuthenticatedUser authenticatedUser, string clientSecretId) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.DeleteClientSecret(applicationId, authenticatedUser, clientSecretId, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapPost("/api/applications/{applicationId}/secrets/{clientSecretId}/detail", (HttpContext httpContext, ClientSecretController route, string applicationId, [FromServices] AuthenticatedUser authenticatedUser, string clientSecretId, [FromBody] UpdateClientSecretDetailRequest? request) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.UpdateClientSecretDetail(applicationId, authenticatedUser, clientSecretId, request, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapGet("/api/applications/me", (HttpContext httpContext, ApplicationController route, [FromServices] AuthenticatedUser authenticatedUser) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetApplications(authenticatedUser, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapGet("/api/applications/{applicationId}/info", (HttpContext httpContext, ApplicationController route, string applicationId) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetApplicationInfo(applicationId, cancellationToken);
});
builder.MapGet("/api/applications/{applicationId}", (HttpContext httpContext, ApplicationController route, string applicationId, [FromServices] AuthenticatedUser authenticatedUser) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetApplication(applicationId, authenticatedUser, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapPost("/api/applications/create", (HttpContext httpContext, ApplicationController route, [FromBody] CreateApplicationRequest createApplicationRequest, [FromServices] AuthenticatedUser authenticatedUser) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.CreateApplication(createApplicationRequest, authenticatedUser, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapPost("/api/applications/{applicationId}/scopes", (HttpContext httpContext, ApplicationController route, [FromBody] UpdateApplicationScopesRequest updateApplicationScopesRequest, [FromServices] AuthenticatedUser authenticatedUser, string applicationId) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.UpdateApplicationScopes(updateApplicationScopesRequest, authenticatedUser, applicationId, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
builder.MapGet("/api/status/", (HttpContext httpContext, StatusController route) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetStatus();
});
builder.MapGet("/api/oauth/.well-known/openid-configuration", (HttpContext httpContext, OAuthController route) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetDiscoveryDocument();
});
builder.MapGet("/api/oauth/.well-known/jwks.json", (HttpContext httpContext, OAuthController route) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetJwks(cancellationToken);
});
builder.MapGet("/api/oauth/userinfo", (HttpContext httpContext, OAuthController route) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetUserInfo(httpContext, cancellationToken);
})
.AddEndpointFilter<AccessTokenAuthenticatedFilter>();
builder.MapPost("/api/oauth/token", (HttpContext httpContext, OAuthController route) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetOAuthToken(httpContext, cancellationToken);
});
builder.MapGet("/api/oauth/scopes", (HttpContext httpContext, OAuthController route) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.GetOauthScopes();
});
builder.MapPost("/api/oauth/authorize", (HttpContext httpContext, OAuthController route, [FromServices] AuthenticatedUser authenticatedUser, [FromBody] AuthorizeRequest request) =>
{
var cancellationToken = httpContext.RequestAborted;
return route.Authorize(authenticatedUser, request, cancellationToken);
})
.AddEndpointFilter<LoginAuthenticatedFilter>();
return builder;
}
}
}
-1
View File
@@ -33,7 +33,6 @@ namespace Badge;
[JsonSerializable(typeof(IEnumerable<OAuthScopeResponse>))]
[JsonSerializable(typeof(OAuthResponse))]
[JsonSerializable(typeof(UserInfoResponse))]
[JsonSerializable(typeof(Task<IResult>))]
public partial class SerializationContext : JsonSerializerContext
{
}
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<linker>
<assembly fullname="Badge">
<type fullname="Badge.OAuthController">
<method name="GetToken" />
</type>
</assembly>
</linker>