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
51 lines
1.5 KiB
Plaintext
51 lines
1.5 KiB
Plaintext
@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>
|
|
</div>
|
|
|
|
@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");
|
|
|
|
var uri = authStartData.Endpoint
|
|
+ $"?client_id={authStartData.ClientId}" +
|
|
$"&redirect_uri={authStartData.RedirectUri}" +
|
|
$"&response_type=code";
|
|
|
|
Navigation.NavigateTo(uri, true);
|
|
}
|
|
}
|