ASP.NET Core

Custodian.AspNetCore connects Custodian to the request pipeline. This page covers UseCustodianContext, which takes the actor, tenant and roles from the signed-in user, and MapCustodianEndpoints, which maps the data-subject-rights, break-glass and assisted-session endpoints. It also shows how you decide who may call them.

The pipeline

Add the context middleware after authentication, because it reads the authenticated user. Map the endpoints after it.

app.UseAuthorization();
app.UseCustodianContext();
app.MapCustodianEndpoints(configure: ShopAuthorization.NamePolicies);

The Clinic sample maps no Custodian endpoints. It uses the middleware and its own endpoints, and shows where UseAuthentication goes.

app.UseAuthentication();
app.UseAuthorization();
app.UseCustodianContext();
app.MapClinic();

Where actor, tenant and roles come from

UseCustodianContext reads three things from HttpContext.User on each request and holds them for the length of that request.

What Claim by default Option
Actor, a stable pseudonymous user id ClaimTypes.NameIdentifier ActorClaimType
Tenant, a GUID tenant TenantClaimType
Application roles ClaimTypes.Role RoleClaimType

The correlation id comes from the X-Correlation-Id request header (CorrelationHeader). When the header is absent or has an unexpected shape, the request's trace identifier is used.

Custodian does not authenticate anyone. Your authentication handler issues the claims. The Clinic's demo handler shows the three that matter.

var identity = new ClaimsIdentity(
    [
        new Claim(ClaimTypes.NameIdentifier, name.ToString()),
        new Claim("tenant", member.Tenant.ToString()),
        new Claim(ClaimTypes.Role, member.Role),
    ],
    SchemeName);

Use an id for the actor, not a name or an email address. The actor is written to every audit entry.

Pass a callback to UseCustodianContext to read other claim types. See Claim_types_are_configurable and Claims_map_to_actor_roles_tenant_and_a_correlation_id.

It never invents an identity

The middleware sets no actor at all in these cases, and Custodian then refuses personal data.

The actor is gone when the request ends (A_requests_actor_is_gone_when_the_request_ends).

The middleware supplies the actor, the tenant and the roles. Your code still declares the purpose, with processing.Begin(...). Getting started shows an endpoint that does.

Turning refusals into status codes

In your own endpoints Custodian's refusals surface as exceptions. The Clinic maps them to status codes in one place.

app.Use(async (context, next) =>
{
    try
    {
        await next(context);
    }
    catch (PurposeViolationException)
    {
        context.Response.StatusCode = StatusCodes.Status403Forbidden;
    }
    catch (NoCareBasisException)
    {
        context.Response.StatusCode = StatusCodes.Status409Conflict;
    }
    catch (ProcessingContextMissingException)
    {
        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
    }
});

Do not write the exception's message to the response.

The endpoints Custodian maps

MapCustodianEndpoints maps these under a prefix, /gdpr unless you pass another. It returns the route group, so you can add conventions to every endpoint.

Method and path Does Needs
GET /rights/subjects/{subject}/report Art. 15 report on one subject. RightsPolicy
GET /rights/subjects/{subject}/export?format=json Art. 20 export, json or csv. RightsPolicy
POST /rights/subjects/{subject}/restriction Restricts processing. RightsPolicy
DELETE /rights/subjects/{subject}/restriction Lifts the restriction. RightsPolicy
POST /rights/subjects/{subject}/erasure Erases the subject. The body gives a reason. RightsPolicy
POST /rights/subjects/{subject}/withdrawals Withdraws consent. The body gives a purpose. RightsPolicy
POST /rights/erasure-replays Replays recorded erasures. RightsPolicy
GET /break-glass?status=outstanding Outstanding and escalated items. Filters: subject, actor, overdue. BreakGlassReviewPolicy
GET /break-glass/{item} One item. BreakGlassReviewPolicy
GET /break-glass/mine The caller's own outstanding items. A signed-in user
POST /break-glass/{item}/justification Explains the caller's own item. The body gives a reasonCode and optional text. A signed-in user
POST /assisted-sessions Opens an assisted session for one customer. AssistedAccessPolicy

The body of POST /assisted-sessions names the subject, a reason, a ticket, an authorisation (kind is verbal or one-time-code, the latter with a code), a scope (read-only or support), and optionally durationMinutes and a purpose. The answer holds the grant id, the subject and the expiry. The session is closed when the request ends. See A_request_becomes_an_assisted_access_request_and_the_session_is_closed_after_the_answer.

The rights endpoints run under the purpose in RightsPurpose, subject-rights by default. The break-glass endpoints run under BreakGlassPurpose, break-glass-review by default. RightsPurpose must be a purpose that a role mapped to the privacy database role may process. See Each_rights_endpoint_calls_its_service_for_the_subject_under_the_rights_purpose_as_the_signed_in_actor and The_purposes_are_configurable.

The pages on data-subject rights, break-glass and assisted access describe what the calls do.

Protecting the endpoints

Every mapped endpoint requires an authenticated user. The endpoints that expose or change other people's data also require an authorization policy, and you must name it. Custodian has no default for who may erase a subject, read everyone's break-glass items or open an assisted session. The Shop defines two policies and names them.

using Custodian.AspNetCore;

namespace Shop;

/// <summary>
/// Who may call the Custodian endpoints the shop maps: privacy officers answer rights requests and review break-glass
/// items, customer support opens assisted sessions.
/// </summary>
public static class ShopAuthorization
{
    public const string PrivacyOffice = "PrivacyOffice";
    public const string CustomerSupport = "CustomerSupport";

    public static IServiceCollection AddShopAuthorization(this IServiceCollection services) =>
        services.AddAuthorization(o =>
        {
            o.AddPolicy(PrivacyOffice, policy => policy.RequireRole(ShopRoles.PrivacyOfficer.Name));
            o.AddPolicy(CustomerSupport, policy => policy.RequireRole(ShopRoles.Support.Name));
        });

    public static void NamePolicies(CustodianEndpointOptions o)
    {
        o.RightsPolicy = PrivacyOffice;
        o.BreakGlassReviewPolicy = PrivacyOffice;
        o.AssistedAccessPolicy = CustomerSupport;
        o.RightsPurpose = Purposes.SubjectRights;
    }
}

The policy is the outer check. Custodian's services check roles and purposes again behind it. A purpose violation or a missing processing context is answered with 403 and a fixed text. The exception's own text never reaches the response. See A_purpose_violation_is_answered_403_without_the_exceptions_text and A_user_with_no_tenant_who_passes_authorisation_is_refused_by_the_missing_context.

The two endpoints open to any signed-in user act only on the caller's own break-glass items.

Next