Skip to content

Commit 955885f

Browse files
fix(consent): store no consent on a flow that can never record one (#1920)
1 parent b13eab6 commit 955885f

2 files changed

Lines changed: 66 additions & 14 deletions

File tree

‎core/authenticate/service.go‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ func (s Service) StartFlow(ctx context.Context, request RegistrationStartRequest
220220
return nil, ErrUnsupportedMethod
221221
}
222222
// the gate runs before anything is sent or redirected
223-
if err := s.gateFlowConsent(request.Intent, request.AcceptedDocumentIDs); err != nil {
223+
consented, err := s.gateFlowConsent(request.Intent, request.AcceptedDocumentIDs)
224+
if err != nil {
224225
return nil, err
225226
}
226227
// both mail strategies know the address before anything is sent, and share
@@ -246,7 +247,7 @@ func (s Service) StartFlow(ctx context.Context, request RegistrationStartRequest
246247
if request.Intent != FlowIntentUnspecified {
247248
flow.Metadata[flowIntentKey] = request.Intent.String()
248249
}
249-
if len(request.AcceptedDocumentIDs) > 0 {
250+
if len(consented) > 0 {
250251
flow.Metadata[flowConsentKey] = map[string]any{
251252
consentDocumentIDsKey: request.AcceptedDocumentIDs,
252253
consentIPAddressKey: request.IPAddress,
@@ -455,21 +456,22 @@ func (s Service) gateFlowStart(ctx context.Context, intent FlowIntent, email str
455456
// decides which rule applies, because without one a signup and a login look
456457
// identical: a signup has to be complete here, an unset intent only has to
457458
// name known ids, and a login checks nothing because it writes no record.
458-
func (s Service) gateFlowConsent(intent FlowIntent, ids []string) error {
459+
func (s Service) gateFlowConsent(intent FlowIntent, ids []string) ([]consent.Document, error) {
459460
if s.consentService == nil || intent == FlowIntentLogin {
460-
return nil
461+
return nil, nil
461462
}
462463

464+
var documents []consent.Document
463465
var err error
464466
if intent == FlowIntentSignup {
465-
_, err = s.consentService.ResolveAll(ids)
467+
documents, err = s.consentService.ResolveAll(ids)
466468
} else {
467-
_, err = s.consentService.Resolve(ids)
469+
documents, err = s.consentService.Resolve(ids)
468470
}
469471
if err != nil {
470-
return fmt.Errorf("%w: %w", ErrConsentRequired, err)
472+
return nil, fmt.Errorf("%w: %w", ErrConsentRequired, err)
471473
}
472-
return nil
474+
return documents, nil
473475
}
474476

475477
// applyMailOTP actions when user submitted otp from the email

‎core/authenticate/service_test.go‎

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,10 +1298,16 @@ func TestService_StartFlow_WritesIntentAndConsent(t *testing.T) {
12981298
storedFlow = flow
12991299
}).Return(nil)
13001300

1301+
enabled := consent.NewService(slog.New(slog.NewTextHandler(io.Discard, nil)),
1302+
consent.Config{Enabled: true, Documents: map[string]consent.DocumentConfig{
1303+
"terms_of_service": {Title: "Terms & Conditions", Version: "v2", URL: "https://example.org/t"},
1304+
"privacy_policy": {Title: "Privacy Policy", Version: "v1", URL: "https://example.org/p"},
1305+
}}, nil)
1306+
13011307
srv := authenticate.NewService(nil, authenticate.Config{
13021308
MailOTP: authenticate.MailOTPConfig{Validity: 10 * time.Minute},
13031309
TestUsers: testusers.Config{Enabled: true, OTP: "111111", Domain: "example.com"},
1304-
}, mockFlowRepo, mailer.NewMockDialer(), nil, nil, mockUserService, nil, nil, nil, nil)
1310+
}, mockFlowRepo, mailer.NewMockDialer(), nil, nil, mockUserService, nil, nil, nil, enabled)
13051311
srv.Now = func() time.Time { return timeNow }
13061312

13071313
_, err := srv.StartFlow(ctx, request)
@@ -1453,25 +1459,35 @@ func TestService_StartFlow_Consent(t *testing.T) {
14531459
assert.ErrorIs(t, err, consent.ErrUnknownDocuments)
14541460
})
14551461

1456-
t.Run("a login checks nothing, because it writes no record", func(t *testing.T) {
1462+
t.Run("a login checks nothing, and persists nothing, because it writes no record", func(t *testing.T) {
14571463
mockConsent := mocks.NewConsentService(t)
14581464

14591465
mockFlowRepo, mockUserService, _, _, _ := createMocks(t)
14601466
ctx := context.Background()
14611467
mockUserService.EXPECT().GetByID(ctx, email).Return(user.User{ID: "user-id", Email: email}, nil)
1462-
mockFlowRepo.EXPECT().Set(ctx, mock.Anything).Return(nil)
1468+
1469+
var storedFlow *authenticate.Flow
1470+
mockFlowRepo.EXPECT().Set(ctx, mock.Anything).Run(func(_ context.Context, flow *authenticate.Flow) {
1471+
storedFlow = flow
1472+
}).Return(nil)
14631473

14641474
srv := authenticate.NewService(nil, authenticate.Config{
14651475
MailOTP: authenticate.MailOTPConfig{Validity: 10 * time.Minute},
14661476
TestUsers: testusers.Config{Enabled: true, OTP: "111111", Domain: "example.com"},
14671477
}, mockFlowRepo, mailer.NewMockDialer(), nil, nil, mockUserService, nil, nil, nil, mockConsent)
14681478

14691479
_, err := srv.StartFlow(ctx, authenticate.RegistrationStartRequest{
1470-
Method: authenticate.MailOTPAuthMethod.String(),
1471-
Email: email,
1472-
Intent: authenticate.FlowIntentLogin,
1480+
Method: authenticate.MailOTPAuthMethod.String(),
1481+
Email: email,
1482+
Intent: authenticate.FlowIntentLogin,
1483+
AcceptedDocumentIDs: acceptedIDs,
1484+
IPAddress: "10.0.0.1",
14731485
})
14741486
require.NoError(t, err)
1487+
require.NotNil(t, storedFlow)
1488+
1489+
_, ok := storedFlow.Consent()
1490+
assert.False(t, ok, "a login must persist no consent block")
14751491
})
14761492

14771493
t.Run("a deployment with consent disabled ignores the ids rather than rejecting them", func(t *testing.T) {
@@ -1487,6 +1503,40 @@ func TestService_StartFlow_Consent(t *testing.T) {
14871503
require.NoError(t, err)
14881504
require.NotNil(t, got)
14891505
})
1506+
1507+
t.Run("a deployment with consent disabled persists no consent onto the flow", func(t *testing.T) {
1508+
disabled := consent.NewService(slog.New(slog.NewTextHandler(io.Discard, nil)),
1509+
consent.Config{Enabled: false}, nil)
1510+
1511+
ctx := context.Background()
1512+
mockFlowRepo, mockUserService, _, _, _ := createMocks(t)
1513+
mockUserService.EXPECT().GetByID(ctx, email).
1514+
Return(user.User{}, errors.New("user not found")).Maybe()
1515+
1516+
var storedFlow *authenticate.Flow
1517+
mockFlowRepo.EXPECT().Set(ctx, mock.Anything).Run(func(_ context.Context, flow *authenticate.Flow) {
1518+
storedFlow = flow
1519+
}).Return(nil)
1520+
1521+
srv := authenticate.NewService(nil, authenticate.Config{
1522+
MailOTP: authenticate.MailOTPConfig{Validity: 10 * time.Minute},
1523+
TestUsers: testusers.Config{Enabled: true, OTP: "111111", Domain: "example.com"},
1524+
}, mockFlowRepo, mailer.NewMockDialer(), nil, nil, mockUserService, nil, nil, nil, disabled)
1525+
1526+
_, err := srv.StartFlow(ctx, authenticate.RegistrationStartRequest{
1527+
Method: authenticate.MailOTPAuthMethod.String(),
1528+
Email: email,
1529+
Intent: authenticate.FlowIntentSignup,
1530+
AcceptedDocumentIDs: acceptedIDs,
1531+
IPAddress: "10.0.0.1",
1532+
})
1533+
require.NoError(t, err)
1534+
require.NotNil(t, storedFlow)
1535+
1536+
_, ok := storedFlow.Consent()
1537+
assert.False(t, ok, "a disabled deployment must persist no consent block")
1538+
assert.Equal(t, authenticate.FlowIntentSignup, storedFlow.Intent())
1539+
})
14901540
}
14911541

14921542
// TestFlow_IntentAndConsent covers the accessors directly: the JSON round trip

0 commit comments

Comments
 (0)