Implemented complete oauth2 flow with modular providers

The local oauth2 provider still needs A LOT of love. But the general oauth2 workflow works. The http api client needs an option to disable the authentication things and we need to switch to the local storage for storing access, refresh and timestamp for the client
This commit is contained in:
Masu Baumgartner
2024-10-19 20:09:03 +02:00
parent 71dc81c4dc
commit f166de1a43
9 changed files with 156 additions and 45 deletions

View File

@@ -64,9 +64,9 @@ builder.Services.AddScoped(sp =>
var cookieService = sp.GetRequiredService<CookieService>();
return new TokenConsumer(
await cookieService.GetValue("ml-access"),
await cookieService.GetValue("ml-refresh"),
DateTimeOffset.FromUnixTimeSeconds(long.Parse(await cookieService.GetValue("ml-timestamp"))).UtcDateTime,
await cookieService.GetValue("kms-access", "x"),
await cookieService.GetValue("kms-refresh", "x"),
DateTimeOffset.FromUnixTimeSeconds(long.Parse(await cookieService.GetValue("kms-timestamp", "0"))).UtcDateTime,
async refreshToken =>
{
await httpClient.PostAsync("api/auth/refresh", new StringContent(
@@ -78,8 +78,8 @@ builder.Services.AddScoped(sp =>
return new TokenPair()
{
AccessToken = await cookieService.GetValue("ml-access"),
RefreshToken = await cookieService.GetValue("ml-refresh")
AccessToken = await cookieService.GetValue("kms-access", "x"),
RefreshToken = await cookieService.GetValue("kms-refresh", "x")
};
}
);

View File

@@ -1,8 +1,12 @@
@page "/auth"
@using MoonCore.Helpers
@using Moonlight.Shared.Http.Requests.Auth
@using Moonlight.Shared.Http.Responses.Auth
@inject NavigationManager Navigation
@inject HttpApiClient HttpApiClient
@inject CookieService CookieService
<div class="flex justify-center">
<WButton OnClick="StartAuth" CssClasses="btn btn-primary">Authenticate</WButton>
@@ -10,9 +14,31 @@
@code
{
[SupplyParameterFromQuery(Name = "code")]
[Parameter]
public string? Code { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(!firstRender)
return;
if(Code == null)
return;
var authHandleData = await HttpApiClient.PostJson<OAuth2HandleResponse>("api/auth", new OAuth2HandleRequest()
{
Code = Code
});
await CookieService.SetValue("kms-access", authHandleData.AccessToken, 10);
await CookieService.SetValue("kms-refresh", authHandleData.RefreshToken, 10);
await CookieService.SetValue("kms-timestamp", DateTimeOffset.UtcNow.AddSeconds(60).ToUnixTimeSeconds().ToString(), 10);
}
private async Task StartAuth(WButton _)
{
var authStartData = await HttpApiClient.GetJson<OAuth2StartResponse>("api/auth/start");
var authStartData = await HttpApiClient.GetJson<OAuth2StartResponse>("api/auth");
var uri = authStartData.Endpoint
+ $"?client_id={authStartData.ClientId}" +