Appearance
ADR 0018 — Verified user identity: one CPF per person per tenant, compared by an opaque key
- Status: Accepted
- Date: 2026-11-14
- Baseline inspected:
develop,63638c74(11.5.1) - Related: ADR 0005, ADR 0011, ADR 0012, ADR 0015
- Request answered: Nexio P17, "verified user identity, with CPF unique per tenant"
- Backlog:
backlog/user-identity/backlog-user-identity.md - Guide: Verified user identity
Delivery status
Everything below is delivered, in one additive minor. The text below describes the delivered design. What changed or was decided during delivery:
- Owner decisions (backlog section 12). No built-in role receives either permission (D-5). The revoked verification keeps its encrypted CPF while the user exists (D-6). One permission covers verify and revoke (D-7). Unlinking a user does not revoke the identity (D-8). Without the
PersonKeysection the feature is off, as a non-breaking minor (D-9). Expiry is lazy and there is no sweep job; the job is a follow-up (D-10). The second person is per login, and per person is left to P18 (D-11). - Fact serialization.
AuthUserIdentityChangedV1is serialized in PascalCase, likeAuthUserRoleChangedV1, not camelCase as first planned. - Request kind on the wire.
kindaccepts only the text"Verify"or"Revoke", without regard to case. A number answers 400. - Reveal. The response carries
Cache-Control: no-store, and the audit record is anAccessedchange. Cpf.Mask.SensitiveDataMasker.MaskCpfnow delegates toGryd.Core.Documents.Cpf.Mask. It no longer emits the stray space, and any text that is not exactly 11 digits gets the full mask***.***.***-**. This reaches the Audit export of personal data.- Persistence API.
UnitOfWorktakes two more repositories andIGrydAuthUnitOfWorkgains two members. It is a binary change for a host that buildsUnitOfWorkby hand or implements the interface. It ships as "Changed", without a major, asTenantLinkRequestsdid (D-14). - Scope domains.
user-identitiesanduser-identity-requestsare tenant-only domains, both read underverify:user-identity(ADR 0011, amendment 2026-11). - Audit contract. The six actions and three entity types are already in the audit logs contract.
Context
Anti-fraud controls in a product separate people, not logins. Examples are four eyes on a supplier's bank account change, "who proposes does not effect", and "who decides one control step does not decide another". Today those controls compare userId, so one person with two users in the same tenant passes all of them. The Nexio already consumes a HasVerifiedCpf flag and fails closed while it is null.
What GrydAuth had on develop at 63638c74:
- No identity attribute of the person. There is no CPF, document or verification state.
Gryd.Core.DocumentshasCnpjonly. - No two-person rule anywhere.
TenantLinkRequesthas a pending/approved lifecycle, but approval does not check that the approver differs from the requester. - A reversible-secret protector (
ISecretProtector, ADR 0005) with purpose and row context in the AAD. There is no keyed hash helper. - In-process readers that answer a batch for a trusted tenant (
IUserTenantEligibilityService,IPermissionHolderReader,AuthReaderServices). - Transactional facts and audit staged in the same save (
AuthUserRoleChangedV1,IAuditTrail.Stage). SensitiveDataMasker.MaskCpfexists but emits a stray space. Serilog redaction does not list a CPF property.
Decision
1. Uniqueness: one verified user per CPF per tenant (option a)
A CPF can be Verified on at most one user of a tenant. A second verification of the same CPF in the tenant is refused (USER_IDENTITY_PERSON_ALREADY_VERIFIED_CONFLICT). The database enforces it with a partial unique index on the active verification's person key.
Why (a) over a shared key (b): with (a), two verified users are always two people. A consumer that still compares userId is then safe for verified users, and a second login of the same person stays NotVerified, which the consumer already treats as "fail closed". With (b), every consumer would have to compare the key everywhere, and one that forgot would reopen the hole. A person who must move to another login has the old one revoked first.
2. Scope: the tenant
An identity belongs to (TenantId, UserId). Verifying in tenant A says nothing in tenant B. The verifier is an actor of the tenant, and the tenant is the controller of the data (LGPD purpose). Re-verification in another tenant is the price.
3. PersonKey: keyed, per tenant, stable, recorded once
PersonKey = "pk1." + base64url(HMAC-SHA256(k_t, cpf)), where k_t = HKDF-SHA256(ikm = personKeyMaster, salt = "gryd.person-key.v1", info = "tenant:" + tenantId:N) and cpf is the 11 normalized digits.
- Keyed, not a plain hash. The CPF space is about 10⁹ values; an unkeyed hash is reversed by enumeration.
- Per tenant. The same CPF gives different keys in different tenants, so keys do not correlate tenants.
- Stable. The key is computed when the verification is proposed, stored with it and never recomputed. A consumer that recorded a key in its trail can compare it later.
- Its own master key,
GrydAuth:Security:PersonKey:MasterKey(base64, at least 32 bytes). It is separate from the ADR 0005 key ring, so rotating the CPF's encryption key never changes a person key. Replacing the person-key master is an incident procedure, and thepk1.prefix is there to version it. - Rejected: deriving from the ADR 0005 ring, because its routine rotation would break every recorded comparison.
4. Model: an aggregate with immutable verifications, and a request
UserIdentityhas one row per(TenantId, UserId). It holdsState(Verified|Revoked) andChangeSequence, which is the ordering stream of the fact. The absence of a row meansNotVerified.UserIdentityVerificationis a child ofUserIdentityand is immutable once written. It holds:- the protected CPF and the person key;
- the method (
DocumentReview); - the request, the proposer, and the verifier with the instant;
- once revoked, the revocation request, its proposer, the revoker and the instant.
A CPF is never edited. A correction is a revocation followed by a new verification, and both stay as history.
UserIdentityRequesthas a kind (Verify|Revoke) and a status (Pending|Approved|Rejected|Expired).- The reason is mandatory and is refused if it contains a run of 11 digits.
- The request expires lazily after a configurable TTL (default 72 h).
- At most one request per target user can be pending.
- The protected CPF is erased from the request when it is decided or expires.
5. Second person
- The requester cannot be the target.
- The approver cannot be the requester and cannot be the target.
- The target of a verification must be eligible in the tenant (
UserTenantEligibilityService). A revocation only needs a verified identity, so a login that was unlinked or deactivated can still be revoked and free its CPF. - The person rules (requester, approver, target) are domain invariants, checked by the handler before mutation and answered with a
_FORBIDDENcode. Target eligibility is an application rule and answersUSER_IDENTITY_TARGET_NOT_FOUND(anti-disclosure). - Verify and revoke share one permission,
verify:user-identity. Reading the CPF in clear needsreveal:user-identity-document. Both are tenant permissions that no built-in role receives: the product grants them on purpose (precedent:export:audit-logs-personal-data).
6. Encryption at rest
The CPF is protected by ISecretProtector with purpose auth.user-identity-cpf and context tenant:{t:N}/user:{u:N}. It is the same context in the request and in the verification, so the ciphertext moves between them without being reopened. The context binds the tenant and the user, not the row: swapping one verification's ciphertext for another of the same user needs write access to the database and is accepted. Both tables join the rewrap through IProtectedSecretSource. The column is in AuditExcludedProperties, so the audit interceptor never records it.
7. Reading in process
IUserIdentityReader.CheckIdentitiesAsync(IReadOnlyCollection<Guid> userIds, Guid tenantId) returns one UserIdentityCheck(UserId, State, PersonKey?, VerifiedAt?, VerifiedByUserId?) per distinct id asked.
- It takes up to 500 ids and runs one SQL command.
PersonKeyand the verification fields are filled only when the state isVerified.- An id of another tenant, or of nobody, answers
NotVerifiedand reveals nothing. - It follows the reader contract of
AuthReaderServices(TenantReadScope, read-only transaction). - The CPF in clear never leaves through it.
8. The fact
The fact is AuthUserIdentityChangedV1, kind auth.user-identity.changed, version 1. Its fields are SourceEventId, TenantId, UserIdentityId, UserId, State, PersonKey, RequestId, ProposedByUserId, ActorUserId, ActorType, OccurredAt and Sequence.
- It is staged in the same save as the change, with stream
UserIdentity.Idand sequenceChangeSequence. - It is delivered after the commit, in the pattern of
AuthUserRoleChangedV1. - It carries the PersonKey of the affected verification, on revocation too, and never the CPF.
- The actor is the approver, and the proposer travels beside it.
9. Audit
Each act stages an IAuditTrail record in the same save. The acts are a request created, approved or rejected, an identity verified or revoked, and a document revealed. Each record carries the author and the reason.
Revealing the CPF is a command, POST …/document/reveal, with a mandatory reason. The record is saved before the value is returned, and a failed save returns nothing. This is decision 8 of ADR 0015 applied locally; the generic ISensitiveDataRead behavior is still not delivered.
10. Never in clear outside the protected column and the reveal
- No log:
Cpf.MaskfixesSensitiveDataMasker.MaskCpf, andCpfis a default redacted property. - No error message, fact or audit metadata.
- No DTO except the reveal response, which is sent with
Cache-Control: no-store.
An integration test sweeps logs, outbox rows, audit rows and error bodies for the digits.
11. Opt-in by configuration
Without the GrydAuth:Security:PersonKey section, the feature is off:
- the commands that create, approve or reveal answer 503
USER_IDENTITY_NOT_CONFIGURED_UNAVAILABLE. Listing, reading, rejecting and the identity status keep working; - the reader answers
NotVerifiedfor every id.
That keeps the release a non-breaking minor. A section that is present but invalid fails at boot (ADR 0005 §7).
Retention and erasure
- The CPF of a request is erased at decision or when its expiry is evaluated. Expiry is lazy: a request nobody touches keeps its ciphertext until the next create, approve or reject for it. A sweep job is a follow-up decision (backlog DP-6).
- The CPF of a verification, revoked or not, is kept as the evidence of what was verified, for as long as the user exists.
- The person key and the metadata are pseudonymous trail.
- GrydAuth has no erasure path for user data today. Erasure on request is out of scope here and follows the platform's custody rule when one exists. Until then it is an operator procedure, which this ADR does not define.
Out of scope
- How the verification happens: the method is recorded as
DocumentReview, the verifier's attestation. - A KMS for the person-key master.
- A global (cross-tenant) identity.
- P18, the second person on granting a control role, which will reuse the PersonKey.
- Automatic revocation when the membership ends: the consumer combines identity with eligibility.
Consequences
- Products can compare people without holding a CPF.
- Operators gain one more secret to provision when they turn the feature on.
- Two new tables and two new permissions are added, with a backfill migration.