Assisted access
This page covers customer service working on one customer's account at the customer's request. It shows how to give a role no standing access, how a representative starts a session the customer authorised, what the session can see, how the audit tells the representative from the customer, and how the session ends. The code is the Shop sample.
A role with no standing access
A support representative needs a customer's data only while helping that customer. The Shop sample marks its Support
role as assisted-only.
public static ShopRole Support { get; } = new("Support", DbRole.Writer, [Purposes.CustomerAccount], AssistedOnly: true);
The policy maps such a role with MapAssistedOnly. The role holds its purposes inside an assisted session and holds
none outside one.
foreach (var role in ShopRoles.All)
{
_ = role.AssistedOnly
? o.Roles.MapAssistedOnly(role.Name, role.DbRole, [.. role.Purposes])
: o.Roles.Map(role.Name, role.DbRole, purposes: [.. role.Purposes]);
}
Outside a session, a representative who declares the customer-account purpose is refused with
PurposeViolationException. See
Without_an_assisted_session_support_sees_no_customers.
Roles and purposes covers role mapping in general.
Start a session
A session starts from an AssistedAccessRequest. The sample's tests build one like this:
public AssistedWork OnBehalfOf(ShopCustomer customer, string reason = "Order 1182 not delivered", TimeSpan? lasting = null) =>
new(new AssistedFrame(
_scenario,
_shop,
_staff,
new AssistedAccessRequest(
Subject: customer.Subject,
Reason: reason,
Ticket: "CS-48213",
Authorisation: CustomerAuthorisation.OneTimeCode("482913"),
Scope: AssistedScope.ReadOnly,
Duration: lasting,
Purpose: Purposes.CustomerAccount),
workAfter: null));
Subjectis the one customer the session is for.ReasonandTicketsay why. Both are stored as you give them, and the customer is shown the reason afterwards. Do not put personal data in either.Authorisationsays how the customer agreed. It isCustomerAuthorisation.Verbalor a one-time code the customer read back. Custodian records which kind was used and does not store the code. This is access control. It is not consent in the sense of a lawful basis.ScopeisAssistedScope.ReadOnlyorAssistedScope.Support, which also allows edits. Special-category fields stay hidden in either scope unless you name them withIncludingSpecialCategory.Durationis how long the session lasts. It is 30 minutes when you leave it out.Purposeis the purpose the session processes for. The representative's role must hold it.
The representative must be the signed-in actor. Then IAssistedAccess.BeginAsync opens the session, and disposing it
closes the session.
using var actor = processing.BeginActor(representative.In(shop));
await using var session = await services.GetRequiredService<IAssistedAccess>().BeginAsync(request, ShopScenario.Cancellation);
In a web application the middleware sets the actor for you. The session carries its own purpose, so the code inside it
does not call processing.Begin. Custodian writes the grant and audits it before the session can read anything.
Custodian also maps an endpoint that opens a session. ASP.NET Core covers it.
What the session can see
Inside the session the representative sees that one customer and nobody else. The query is ordinary EF Core.
[Theory, EachProvider]
public async Task In_an_assisted_session_support_sees_only_that_customer(DatabaseProvider provider)
{
await using var shops = await servers.OpenShop(provider);
var astrid = await shops.Nordic.SignUp(Customers.Astrid());
await shops.Nordic.SignUp(Customers.Clara());
var names = await shops.Nordic.As(Staff.Support).OnBehalfOf(astrid).Query(db => db.Customers.Select(c => c.Name));
Assert.Equal([astrid.Name], names);
}
- The database enforces the same limit. Raw SQL inside the session returns that customer's orders only.
See
In_an_assisted_session_raw_sql_sees_only_that_customers_orders. - A read-only session cannot write. The attempt throws
AssistedScopeViolationException. SeeA_read_only_session_cannot_change_the_customers_details.
Tenants and row-level security explains how the database knows which customer the session is for.
Actor and on-behalf-of in the audit
The representative is always the actor. The customer is recorded as on-behalf-of. Nothing is recorded as if the customer had done it. Every audit entry of the session also carries the id of the grant, so you can list everything that happened in one session. The sample picks out the entries that carry a grant and checks the other two fields on each:
public void ShouldAttributeEveryAssistedEntryTo(StaffMember representative, ShopCustomer onBehalfOf)
{
var assisted = _entries.Where(entry => entry.Event.Grant is not null).ToList();
Assert.NotEmpty(assisted);
Assert.All(assisted, entry =>
{
Assert.Equal(representative.Actor, entry.Event.Actor);
Assert.Equal(onBehalfOf.Subject, entry.Event.OnBehalfOf);
});
}
See
Every_audit_entry_of_an_assisted_session_names_the_representative_as_actor_and_the_customer_as_on_behalf_of.
Audit trail describes the entries.
Expiry
A session ends on its own when its time is up. After that it neither reads nor writes, even if the code still holds the session object.
[Theory, EachProvider]
public async Task An_assisted_session_ends_when_its_time_is_up(DatabaseProvider provider)
{
await using var shops = await servers.OpenShop(provider);
var astrid = await shops.Nordic.SignUp(Customers.Astrid());
var readingTooLate = () => shops.Nordic.As(Staff.Support)
.OnBehalfOf(astrid, lasting: TimeSpan.FromMinutes(30))
.After(TimeSpan.FromMinutes(31))
.Query(db => db.Customers);
await Assert.ThrowsAsync<AssistedScopeViolationException>(readingTooLate);
}
IAssistedSession.ExpiresAt tells you when that will be.
Limit the session length
A representative cannot ask for an arbitrarily long session. A request longer than the maximum is refused before
anything is opened. The maximum is two hours. Change it with o.Assisted.MaxSessionLength(...) when you register
Custodian.
Tell the customer
When a session closes, Custodian calls your IAssistedAccessNotifier with the tenant, the customer, the grant id, the
reason the representative gave, and when the session opened and closed. Send the customer an email or an in-app
message from there. The samples' tests use a notifier that keeps the notices in memory:
using System.Collections.Concurrent;
using Custodian;
namespace Samples.Testing;
/// <summary>Keeps every customer notice instead of sending it. Register it after <c>AddCustodian</c>.</summary>
public sealed class RecordingAssistedAccessNotifier : IAssistedAccessNotifier
{
private readonly ConcurrentQueue<AssistedAccessNotice> _notices = new();
public IReadOnlyList<AssistedAccessNotice> Notices => [.. _notices];
public Task NotifyAsync(AssistedAccessNotice notice, CancellationToken cancellationToken = default)
{
_notices.Enqueue(notice);
return Task.CompletedTask;
}
}
Register your notifier as a singleton IAssistedAccessNotifier after AddCustodian. If you register none, Custodian
uses one that does nothing, and the customer is not told. See
The_customer_is_told_that_a_representative_accessed_their_account_and_why.