Skip to content

Commit

Permalink
add optional EXPORT_USER_DATA_URL to extract user data
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Mar 20, 2024
1 parent 48a628c commit 66dca78
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
22 changes: 21 additions & 1 deletion KratosSelfService/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using KratosSelfService.Extensions;
using KratosSelfService.Models;
using KratosSelfService.Services;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -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<IActionResult> Profile()
{
Expand All @@ -16,4 +20,20 @@ public async Task<IActionResult> Profile()
session.Identity.SchemaUrl);
return View("Profile", new ProfileModel(session, IdentitySchemaService.GetTraits(schema)));
}

[HttpGet("export-data")]
public async Task<IActionResult> 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");
}
}
10 changes: 9 additions & 1 deletion KratosSelfService/Services/EnvService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public class EnvService
/// <summary>
/// The optional logo URI for the website
/// </summary>
public readonly string? WebsiteTitle =
public readonly string WebsiteTitle =
Environment.GetEnvironmentVariable("WEBSITE_TITLE") ?? "OryUI";

/// <summary>
/// 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.
/// </summary>
public readonly string? ExportUserDataUrl =
Environment.GetEnvironmentVariable("EXPORT_USER_DATA_URL");
}
1 change: 1 addition & 0 deletions KratosSelfService/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Startup(IConfigurationRoot config, IWebHostEnvironment env)
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(ILogger), provider => provider.GetRequiredService<ILogger<Program>>());
services.AddControllersWithViews();

// authentication and authorisation
Expand Down
1 change: 1 addition & 0 deletions OryAdmin/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Startup(IConfigurationRoot config, IWebHostEnvironment env)
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(ILogger), provider => provider.GetRequiredService<ILogger<Program>>());
services.AddHttpContextAccessor();
services.AddRazorComponents()
.AddInteractiveServerComponents();
Expand Down

0 comments on commit 66dca78

Please sign in to comment.