From 66dca78d260192f8767f20b25b732106a2054017 Mon Sep 17 00:00:00 2001 From: Joscha <34318751+josxha@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:48:02 +0100 Subject: [PATCH] add optional EXPORT_USER_DATA_URL to extract user data --- .../Controllers/ProfileController.cs | 22 ++++++++++++++++++- KratosSelfService/Services/EnvService.cs | 10 ++++++++- KratosSelfService/Startup.cs | 1 + OryAdmin/Startup.cs | 1 + 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/KratosSelfService/Controllers/ProfileController.cs b/KratosSelfService/Controllers/ProfileController.cs index 61b8be07..fbe8f17d 100644 --- a/KratosSelfService/Controllers/ProfileController.cs +++ b/KratosSelfService/Controllers/ProfileController.cs @@ -1,3 +1,4 @@ +using KratosSelfService.Extensions; using KratosSelfService.Models; using KratosSelfService.Services; using Microsoft.AspNetCore.Authorization; @@ -6,8 +7,11 @@ namespace KratosSelfService.Controllers; -public class ProfileController(IdentitySchemaService schemaService) : Controller +public class ProfileController( + IdentitySchemaService schemaService, EnvService envService, ILogger logger) : Controller { + private HttpClient _httpClient = new(); + [HttpGet("")] public async Task Profile() { @@ -16,4 +20,20 @@ public async Task Profile() session.Identity.SchemaUrl); return View("Profile", new ProfileModel(session, IdentitySchemaService.GetTraits(schema))); } + + [HttpGet("export-data")] + public async Task ExportData(CancellationToken cancellationToken) + { + var session = HttpContext.GetSession()!; + if (string.IsNullOrWhiteSpace(envService.ExportUserDataUrl)) + { + logger.LogDebug("Called disabled export-data endpoint, return 404."); + return NotFound(); + } + var url = envService.ExportUserDataUrl.Replace("{Id}", session.Identity.Id); + var stream = await _httpClient.GetStreamAsync(url, cancellationToken:cancellationToken); + var timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"); + return File(stream, "APPLICATION/octet-stream", + $"user-export-{timestamp}.zip"); + } } \ No newline at end of file diff --git a/KratosSelfService/Services/EnvService.cs b/KratosSelfService/Services/EnvService.cs index d65ca08c..01db88ef 100644 --- a/KratosSelfService/Services/EnvService.cs +++ b/KratosSelfService/Services/EnvService.cs @@ -58,6 +58,14 @@ public class EnvService /// /// The optional logo URI for the website /// - public readonly string? WebsiteTitle = + public readonly string WebsiteTitle = Environment.GetEnvironmentVariable("WEBSITE_TITLE") ?? "OryUI"; + + /// + /// Optional internal URL to collect and bundle all user data. + /// Setting this allows the user to download its user data. + /// {Id} will get replaced by the uuid of the user. + /// + public readonly string? ExportUserDataUrl = + Environment.GetEnvironmentVariable("EXPORT_USER_DATA_URL"); } \ No newline at end of file diff --git a/KratosSelfService/Startup.cs b/KratosSelfService/Startup.cs index 4a0df7d5..a4d114f8 100644 --- a/KratosSelfService/Startup.cs +++ b/KratosSelfService/Startup.cs @@ -11,6 +11,7 @@ public class Startup(IConfigurationRoot config, IWebHostEnvironment env) { public void ConfigureServices(IServiceCollection services) { + services.AddSingleton(typeof(ILogger), provider => provider.GetRequiredService>()); services.AddControllersWithViews(); // authentication and authorisation diff --git a/OryAdmin/Startup.cs b/OryAdmin/Startup.cs index b38af142..fc014241 100644 --- a/OryAdmin/Startup.cs +++ b/OryAdmin/Startup.cs @@ -9,6 +9,7 @@ public class Startup(IConfigurationRoot config, IWebHostEnvironment env) { public void ConfigureServices(IServiceCollection services) { + services.AddSingleton(typeof(ILogger), provider => provider.GetRequiredService>()); services.AddHttpContextAccessor(); services.AddRazorComponents() .AddInteractiveServerComponents();