feat(identity): confirm registration email atomically
This commit is contained in:
1 parent
8e7ce8d71c
commit
fade4e76e4
75 files changed
+4188
-403
No files matched your search
@@ -153,13 +153,15 @@ func (service *Service) GrantAtRegistration(
|
||||
ctx context.Context,
|
||||
repositories outbound.ConsentRepositories,
|
||||
subjectID uuid.UUID,
|
||||
registrationVersion int64,
|
||||
version domain.ConsentTextVersion,
|
||||
) (GrantResult, error) {
|
||||
result, err := service.grant(ctx, repositories, grantRequest{
|
||||
subjectID: subjectID,
|
||||
purpose: domain.ConsentPurposePersonalData,
|
||||
version: version,
|
||||
source: domain.ConsentSourceRegistration,
|
||||
subjectID: subjectID,
|
||||
purpose: domain.ConsentPurposePersonalData,
|
||||
version: version,
|
||||
source: domain.ConsentSourceRegistration,
|
||||
registrationVersion: registrationVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return GrantResult{}, fmt.Errorf("Service.GrantAtRegistration - %w", err)
|
||||
@@ -236,10 +238,11 @@ func (service *Service) GrantInSettingsInNewTransaction(ctx context.Context, inp
|
||||
// grantRequest is a grant of C1, C2, C3, or C5 with its source, which the
|
||||
// public entry points fix.
|
||||
type grantRequest struct {
|
||||
subjectID uuid.UUID
|
||||
purpose domain.ConsentPurpose
|
||||
version domain.ConsentTextVersion
|
||||
source domain.ConsentSource
|
||||
subjectID uuid.UUID
|
||||
purpose domain.ConsentPurpose
|
||||
version domain.ConsentTextVersion
|
||||
source domain.ConsentSource
|
||||
registrationVersion int64
|
||||
}
|
||||
|
||||
func (service *Service) grant(ctx context.Context, repositories outbound.ConsentRepositories, request grantRequest) (GrantResult, error) {
|
||||
@@ -259,12 +262,13 @@ func (service *Service) grant(ctx context.Context, repositories outbound.Consent
|
||||
return GrantResult{}, fmt.Errorf("check text version: %w", err)
|
||||
}
|
||||
grant, err := domain.NewConsentGrant(domain.ConsentGrantInput{
|
||||
SubjectID: request.subjectID,
|
||||
Purpose: request.purpose,
|
||||
TextVersion: text.Version(),
|
||||
TextHash: text.Hash(),
|
||||
Source: request.source,
|
||||
GrantedAt: now,
|
||||
SubjectID: request.subjectID,
|
||||
Purpose: request.purpose,
|
||||
TextVersion: text.Version(),
|
||||
TextHash: text.Hash(),
|
||||
Source: request.source,
|
||||
RegistrationVersion: request.registrationVersion,
|
||||
GrantedAt: now,
|
||||
})
|
||||
if err != nil {
|
||||
return GrantResult{}, fmt.Errorf("build grant: %w", err)
|
||||
|
||||
@@ -71,17 +71,18 @@ func TestGrantAtRegistrationRecordsC1WithTheRegistrationSource(t *testing.T) {
|
||||
harness := newHarness(t)
|
||||
subject := uuid.New()
|
||||
|
||||
result, err := harness.service.GrantAtRegistration(t.Context(), harness.repositories, subject, harness.version(domain.ConsentPurposePersonalData))
|
||||
result, err := harness.service.GrantAtRegistration(t.Context(), harness.repositories, subject, 1, harness.version(domain.ConsentPurposePersonalData))
|
||||
if err != nil || !result.Created {
|
||||
t.Fatalf("GrantAtRegistration() = %v, %v, want a new record", result.Created, err)
|
||||
}
|
||||
record := result.Record
|
||||
if record.Purpose() != domain.ConsentPurposePersonalData || record.Source() != domain.ConsentSourceRegistration ||
|
||||
record.RegistrationVersion() != 1 ||
|
||||
record.SubjectID() != subject || !record.GrantedAt().Equal(clockStart) {
|
||||
t.Fatalf("GrantAtRegistration() record = %s %s, want the registration C1 at the application clock", record.Purpose(), record.Source())
|
||||
}
|
||||
|
||||
_, err = harness.service.GrantAtRegistration(t.Context(), harness.repositories, subject, "c1-1999-01-01")
|
||||
_, err = harness.service.GrantAtRegistration(t.Context(), harness.repositories, subject, 1, "c1-1999-01-01")
|
||||
if !errors.Is(err, consent.ErrStaleConsentText) {
|
||||
t.Fatalf("GrantAtRegistration(outdated) error = %v, want ErrStaleConsentText", err)
|
||||
}
|
||||
@@ -315,7 +316,7 @@ func TestPurposesAreIndependent(t *testing.T) {
|
||||
var result consent.GrantResult
|
||||
var err error
|
||||
if purpose == domain.ConsentPurposePersonalData {
|
||||
result, err = harness.service.GrantAtRegistration(t.Context(), harness.repositories, subject, harness.version(purpose))
|
||||
result, err = harness.service.GrantAtRegistration(t.Context(), harness.repositories, subject, 1, harness.version(purpose))
|
||||
} else {
|
||||
result, err = harness.service.GrantInSettings(t.Context(), harness.repositories, consent.SettingsGrantInput{
|
||||
SubjectID: subject, Purpose: purpose, TextVersion: harness.version(purpose),
|
||||
@@ -621,7 +622,8 @@ func (ledger *fakeLedger) Grant(_ context.Context, grant domain.ConsentGrant) (d
|
||||
hash := grant.TextHash()
|
||||
record, err := domain.RestoreConsentRecord(domain.StoredConsentRecord{
|
||||
ID: grant.ID(), SubjectID: grant.SubjectID(), Purpose: string(grant.Purpose()), TextVersion: string(grant.TextVersion()),
|
||||
TextHash: hash[:], Source: string(grant.Source()), State: string(domain.ConsentStateActive), GrantedAt: grant.GrantedAt(),
|
||||
TextHash: hash[:], Source: string(grant.Source()), RegistrationVersion: grant.RegistrationVersion(),
|
||||
State: string(domain.ConsentStateActive), GrantedAt: grant.GrantedAt(),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.ConsentRecord{}, false, err
|
||||
@@ -674,6 +676,17 @@ func (ledger *fakeLedger) Current(_ context.Context, subjectID uuid.UUID, purpos
|
||||
return domain.ConsentRecord{}, outbound.ErrConsentNotFound
|
||||
}
|
||||
|
||||
func (ledger *fakeLedger) LockRegistration(_ context.Context, subjectID uuid.UUID, registrationVersion int64) (domain.ConsentRecord, error) {
|
||||
for index := len(ledger.records) - 1; index >= 0; index-- {
|
||||
if record := ledger.records[index]; record.SubjectID() == subjectID &&
|
||||
record.Purpose() == domain.ConsentPurposePersonalData && record.Source() == domain.ConsentSourceRegistration &&
|
||||
record.RegistrationVersion() == registrationVersion && record.State() == domain.ConsentStateActive {
|
||||
return record, nil
|
||||
}
|
||||
}
|
||||
return domain.ConsentRecord{}, outbound.ErrConsentNotFound
|
||||
}
|
||||
|
||||
func (ledger *fakeLedger) CurrentContact(context.Context, uuid.UUID, uuid.UUID) ([]domain.ConsentRecord, error) {
|
||||
panic("the consent usecase does not read C4")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user