-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Svelte website: login and logout (#835)
- Implement login button and baas auth - Reserve /api for website data endpoints. Example included with functional validation of idToken cookie, but not used yet
- Loading branch information
1 parent
8a0d8c9
commit 8e74d2a
Showing
54 changed files
with
1,232 additions
and
726 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
29 changes: 29 additions & 0 deletions
29
DragaliaAPI/DragaliaAPI/Features/Web/ConfigureJwtBearerOptions.cs
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,29 @@ | ||
using DragaliaAPI.Models.Options; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace DragaliaAPI.Features.Web; | ||
|
||
/// <summary> | ||
/// Allows setting additional properties on <see cref="JwtBearerOptions"/> using DI. | ||
/// </summary> | ||
/// <remarks> | ||
/// Sourced from: <see href="https://gist.github.com/xiaomi7732/20ff2ad11b085a851759d3835b95c8d7"/> | ||
/// </remarks> | ||
public class ConfigureJwtBearerOptions(IOptions<BaasOptions> baasOptions) | ||
: IConfigureNamedOptions<JwtBearerOptions> | ||
{ | ||
// Never called | ||
public void Configure(JwtBearerOptions options) => | ||
this.Configure(JwtBearerDefaults.AuthenticationScheme, options); | ||
|
||
public void Configure(string? name, JwtBearerOptions options) | ||
{ | ||
options.Authority = baasOptions.Value.BaasUrl; | ||
options.TokenValidationParameters = new() | ||
{ | ||
ValidAudience = baasOptions.Value.TokenAudience, | ||
ValidIssuer = baasOptions.Value.TokenIssuer, | ||
}; | ||
} | ||
} |
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,43 @@ | ||
using DragaliaAPI.Features.Web; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.Options; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace DragaliaAPI; | ||
|
||
public static partial class FeatureExtensions | ||
{ | ||
public static IServiceCollection AddWebFeature(this IServiceCollection serviceCollection) | ||
{ | ||
serviceCollection | ||
.AddTransient<IConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>() | ||
.AddScoped<UserService>(); | ||
|
||
serviceCollection | ||
.AddAuthentication() | ||
.AddJwtBearer( | ||
WebAuthenticationHelper.SchemeName, | ||
opts => | ||
{ | ||
opts.Events = new() | ||
{ | ||
OnMessageReceived = WebAuthenticationHelper.OnMessageReceived, | ||
OnTokenValidated = WebAuthenticationHelper.OnTokenValidated | ||
}; | ||
// The rest is configured in ConfigureJwtBearerOptions.cs after the ServiceProvider is built. | ||
} | ||
); | ||
|
||
serviceCollection | ||
.AddAuthorizationBuilder() | ||
.AddPolicy( | ||
WebAuthenticationHelper.PolicyName, | ||
builder => | ||
builder | ||
.RequireAuthenticatedUser() | ||
.AddAuthenticationSchemes(WebAuthenticationHelper.SchemeName) | ||
); | ||
|
||
return serviceCollection; | ||
} | ||
} |
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,19 @@ | ||
using Immediate.Apis.Shared; | ||
using Immediate.Handlers.Shared; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace DragaliaAPI.Features.Web; | ||
|
||
[Handler] | ||
[MapGet("/api/user")] | ||
[Authorize(WebAuthenticationHelper.PolicyName)] | ||
public static partial class UserQuery | ||
{ | ||
public record Query; | ||
|
||
private static async ValueTask<User> HandleAsync( | ||
Query _, | ||
UserService userService, | ||
CancellationToken cancellationToken | ||
) => await userService.GetUser(cancellationToken); | ||
} |
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,21 @@ | ||
using DragaliaAPI.Database; | ||
using DragaliaAPI.Shared.PlayerDetails; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DragaliaAPI.Features.Web; | ||
|
||
public class UserService(IPlayerIdentityService playerIdentityService, ApiContext apiContext) | ||
{ | ||
public Task<User> GetUser(CancellationToken cancellationToken) => | ||
apiContext | ||
.Players.Where(x => x.ViewerId == playerIdentityService.ViewerId) | ||
.Select(x => new User() { Name = x.UserData!.Name, ViewerId = x.ViewerId, }) | ||
.FirstAsync(cancellationToken); | ||
} | ||
|
||
public class User | ||
{ | ||
public long ViewerId { get; init; } | ||
|
||
public required string Name { get; init; } | ||
} |
Oops, something went wrong.