Skip to content

Commit

Permalink
Add settings endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lerch committed Jan 17, 2025
1 parent 34feaf6 commit e9ae390
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions server/Korga/Configuration/OpenIdConnectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class OpenIdConnectOptions
public string? Authority { get; set; }
public string? ClientId { get; set; }
public string? ClientSecret { get; set; }
public string? RedirectUri { get; set; }
}
33 changes: 33 additions & 0 deletions server/Korga/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Korga.Configuration;
using Korga.Models.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Korga.Controllers;

[ApiController]
public class SettingsController : ControllerBase
{
private readonly IOptions<OpenIdConnectOptions> options;

public SettingsController(IOptions<OpenIdConnectOptions> options)
{
this.options = options;
}

[HttpGet("~/api/settings")]
[ProducesResponseType(typeof(SettingsResponse), StatusCodes.Status200OK)]
public IActionResult Index()
{
if (options.Value.Authority == null || options.Value.ClientId == null || options.Value.RedirectUri == null)
return StatusCode(StatusCodes.Status500InternalServerError);

return new JsonResult(new SettingsResponse
{
OidcAuthority = options.Value.Authority,
OidcClientId = options.Value.ClientId,
OidcRedirectUri = options.Value.RedirectUri,
});
}
}
8 changes: 8 additions & 0 deletions server/Korga/Models/Json/SettingsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Korga.Models.Json;

public class SettingsResponse
{
public required string OidcAuthority { get; init; }
public required string OidcClientId { get; init; }
public required string OidcRedirectUri { get; init; }
}
28 changes: 23 additions & 5 deletions webapp/src/services/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useUserManagerStore } from "./usermanager";
import { UserManager } from "oidc-client-ts";

const baseUrl = import.meta.env.PROD
? window.basePath.slice(0, -1)
: import.meta.env.VITE_API_BASE_PATH.slice(0, -1)

let userManager: UserManager | null = null

const getInfo: RequestInit = {
credentials: "include",
}
Expand All @@ -20,9 +22,25 @@ const deleteInfo: RequestInit = {
}

export default {
async userManager() {
if (userManager != null) {
return userManager
}
const response = await fetch(baseUrl + "/api/settings")
if (response.ok === false) {
throw new Error("Unexpected status code " + response.status)
}
const settings = await response.json() as { oidcAuthority: string, oidcClientId: string, oidcRedirectUri: string }
userManager = new UserManager({
authority: settings.oidcAuthority,
client_id: settings.oidcClientId,
redirect_uri: settings.oidcRedirectUri,
})
return userManager
},
async get<T>(path: string) {
const store = useUserManagerStore()
const user = await store.userManager.getUser()
const userManager = await this.userManager()
const user = await userManager.getUser()
const response = await fetch(baseUrl + path, {
headers: { Authorization: `Bearer ${user?.access_token}` },
})
Expand All @@ -37,8 +55,8 @@ export default {
return (await response.json()) as T
},
async getResponse(path: string) {
const store = useUserManagerStore()
const user = await store.userManager.getUser()
const userManager = await this.userManager()
const user = await userManager.getUser()
const response = await fetch(baseUrl + path, {
headers: { Authorization: `Bearer ${user?.access_token}` },
})
Expand Down

0 comments on commit e9ae390

Please sign in to comment.