Skip to content

feat(consent): record consent in the same transaction as the user - #1914

Open
rohanchkrabrty wants to merge 1 commit into
feature/featconsent-add-appconsent-config-the-consent-service-andfrom
feature/featconsent-record-consent-in-the-same-transaction-as-the
Open

feat(consent): record consent in the same transaction as the user#1914
rohanchkrabrty wants to merge 1 commit into
feature/featconsent-add-appconsent-config-the-consent-service-andfrom
feature/featconsent-record-consent-in-the-same-transaction-as-the

Conversation

@rohanchkrabrty

@rohanchkrabrty rohanchkrabrty commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Closes the invariant the whole feature rests on: a user row without a consent record is impossible. ResolveAll runs before the transaction opens, so an incomplete payload never starts one; inside it, the user insert and the consent insert both land or neither does. Per RFC 0002, Enforcement and Storage.
  • user.Repository gains CreateWithTx, which is the one place this feature reaches outside its own domain. pkg/db has WithTxn but carries no transaction on the context, so it is threaded through explicitly. Both create paths share one query builder, so they cannot drift.
  • An existing user is written nothing, whatever the flow carries. A record written outside a user creation would stamp this moment's timestamp and IP on an agreement made elsewhere, which is worse than no record because it reads like evidence. The IP and time come from when the user accepted, carried on the flow across the redirect.
  • A user.consent_granted audit record is written after the commit, through the repository rather than the service, with Actor filled in explicitly — these endpoints are skip-listed, so an empty actor would be enriched to the nil UUID and the system actor for an act a person performed. It cannot be atomic with the consent row, which is why that row is the source of truth and a failed audit write is logged and carried past.
  • The rollback is proved against a real database, not a mocked transaction — a mock can only pretend to roll back.

@rohanchkrabrty rohanchkrabrty self-assigned this Aug 31, 2026
@vercel

vercel Bot commented Aug 31, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
frontier Ready Ready Preview Sep 2, 2026 7:02pm UTC

@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: e952be8d-68ec-4e86-9364-92366416c998

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coveralls

coveralls commented Aug 31, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 33670678027

Coverage increased (+0.3%) to 50.021%

Details

  • Coverage increased (+0.3%) from the base build.
  • Patch coverage: 33 uncovered changes across 5 files (226 of 259 lines covered, 87.26%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
internal/store/postgres/user_repository.go 35 18 51.43%
cmd/serve.go 6 0 0.0%
internal/store/postgres/user_consent_repository.go 44 38 86.36%
core/authenticate/service.go 53 51 96.23%
internal/store/postgres/user_consent.go 37 35 94.59%
Total (7 files) 259 226 87.26%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 41011
Covered Lines: 20514
Line Coverage: 50.02%
Coverage Strength: 15.94 hits per line

💛 - Coveralls

The invariant this establishes: a user row without a consent record is
impossible. ResolveAll runs before the transaction opens, so an incomplete
payload never starts one; inside it, the user insert and the consent insert
both land or neither does.

user.Repository and the new user_consents repository each gain a Create that
takes a *sqlx.Tx. pkg/db has WithTxn but carries no transaction on the
context, so the transaction is threaded through explicitly rather than found
on one. Both are additive, and the user repository change is the one place
this feature reaches outside its own domain. The consent repository has
Create and nothing else, because the table is immutable.

consent.Grant writes one record for the documents it is given and has no
completeness rule of its own. ResolveAll is what decides a signup covers
every configured document, and keeping that out of Grant leaves room for a
later re-consent covering a subset without a second write path.

getOrCreateUser now has three outcomes for a new user. A complete payload
writes both rows in one transaction. An incomplete one returns
ErrConsentRequired and writes nothing. An existing user gets no record at
all, which is absolute: a record written outside a user creation would carry
that moment's timestamp and IP for an agreement made elsewhere, which is
worse than no record because it reads like evidence. A nil flow means one of
the paths that create a user without one, and those stay exempt because no
account holder is present to consent.

The completeness check runs at user creation under every intent, not for the
error but as the invariant guarding the write. An unset intent is permissive
for the login gate but never for consent. With app.consent disabled ResolveAll
resolves nothing, and an empty document set means write no record, so nothing
changes for a deployment that does not ask for consent.

Each signup also writes one audit record, with UserConsentGrantedEvent and
ConsentType added to pkg/auditrecord following the entity.verb naming already
there. It goes through the repository with the actor filled in, as userpat
does for its PAT events: the repository enriches an empty actor from the
context, and these endpoints are on the authentication skip list with no
actor in it, so the record would otherwise land as the system actor for an
act a person performed. It is written after the commit, since the audit
repository has no transactional create, so it cannot be atomic with the
record it describes. That is why the consent record is the source of truth
and this one is a breadcrumb: a failure is logged and the signup stands. Its
target metadata carries the whole document snapshot rather than the id and
the version alone, so a reader working from the audit trail can say what was
accepted without reading back a record they may not have access to.

The rollback is tested against a real Postgres rather than a mocked
transaction, since a mock can only pretend to roll back.

See docs/rfcs/0002-explicit-consent-at-signup.md, Enforcement and Storage.
@rohanchkrabrty
rohanchkrabrty force-pushed the feature/featconsent-record-consent-in-the-same-transaction-as-the branch from 6989f83 to fa677e9 Compare September 2, 2026 19:00
@rohanchkrabrty
rohanchkrabrty marked this pull request as ready for review September 2, 2026 20:06
Comment thread core/consent/service.go
// written once, so there is nothing to deduplicate.
}); err != nil && s.logger != nil {
s.logger.ErrorContext(ctx, "failed to write the audit record for a granted consent",
"consent_id", granted.ID, "user_id", granted.UserID, "error", err)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of error we can manually create the record for book keeping. In that case we will need all the fields and info. Check if we need to print more info here or the rest is already there in consents table?

@rohanchkrabrty rohanchkrabrty Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the info needed is present in user_consents table. Only org_id is missing but that will always be PlatformOrgID

Comment thread core/consent/service.go
Comment on lines +20 to +22
type Repository interface {
Create(ctx context.Context, tx *sqlx.Tx, cnst Consent) (Consent, error)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to establish a pattern across the codebase on how to move transactions across services — this doesn't exist as of now, and will be done later. The approach may vary as well. So instead of doing it as a one-off case and making db trnx as dependency in service, can we instead look at alternatives, e.g. a postgres-layer method (say CreateWithConsent with the interface declared on authenticate's side since it already imports both domains) which opens WithTxn internally and inserts all the rows required?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohilsurana thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants