Skip to content

Commit

Permalink
avoid updating user when MfaWeakestDevice doesn't change (#47390)
Browse files Browse the repository at this point in the history
This PR introduces a small change that skips user updates when adding or removing MFA devices if the end value is equal to the start value of `MfaWeakestDevice`.

Signed-off-by: Tiago Silva <[email protected]>
  • Loading branch information
tigrato authored Oct 9, 2024
1 parent a6037e6 commit 8492e07
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/services/local/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,10 @@ func (s *IdentityService) upsertUserStatusMFADevice(ctx context.Context, user st
user,
false, /*withSecrets*/
func(u types.User) (bool, error) {
// If the user already has the weakest device, don't update.
if u.GetWeakestDevice() == mfaState {
return false, nil
}
u.SetWeakestDevice(mfaState)
return true, nil
})
Expand Down
5 changes: 5 additions & 0 deletions lib/services/local/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,7 @@ func TestWeakestMFADeviceKind(t *testing.T) {
got, err = identity.GetUser(ctx, "bob", false)
require.NoError(t, err)
require.Equal(t, types.MFADeviceKind_MFA_DEVICE_KIND_TOTP, got.GetWeakestDevice())
oldRevision := got.GetMetadata().Revision

u2fDev := &types.MFADevice{
Metadata: types.Metadata{
Expand All @@ -1451,6 +1452,7 @@ func TestWeakestMFADeviceKind(t *testing.T) {
got, err = identity.GetUser(ctx, "bob", false)
require.NoError(t, err)
require.Equal(t, types.MFADeviceKind_MFA_DEVICE_KIND_TOTP, got.GetWeakestDevice())
require.Equal(t, oldRevision, got.GetMetadata().Revision, "revision should not change")

// Create webauthn device but state should still be MFA_DEVICE_KIND_TOTP
// because it shows the weakest state.
Expand All @@ -1477,6 +1479,7 @@ func TestWeakestMFADeviceKind(t *testing.T) {
got, err = identity.GetUser(ctx, "bob", false)
require.NoError(t, err)
require.Equal(t, types.MFADeviceKind_MFA_DEVICE_KIND_TOTP, got.GetWeakestDevice())
require.Equal(t, oldRevision, got.GetMetadata().Revision, "revision should not change")

// Delete the TOTP device and the state should be MFA_DEVICE_KIND_WEBAUTHN
err = identity.DeleteMFADevice(ctx, "bob", totpDevice.Id)
Expand All @@ -1485,6 +1488,7 @@ func TestWeakestMFADeviceKind(t *testing.T) {
got, err = identity.GetUser(ctx, "bob", false)
require.NoError(t, err)
require.Equal(t, types.MFADeviceKind_MFA_DEVICE_KIND_WEBAUTHN, got.GetWeakestDevice())
oldRevision = got.GetMetadata().Revision

// Delete the U2F device and the state should be MFA_DEVICE_KIND_WEBAUTHN
err = identity.DeleteMFADevice(ctx, "bob", u2fDev.Id)
Expand All @@ -1493,6 +1497,7 @@ func TestWeakestMFADeviceKind(t *testing.T) {
got, err = identity.GetUser(ctx, "bob", false)
require.NoError(t, err)
require.Equal(t, types.MFADeviceKind_MFA_DEVICE_KIND_WEBAUTHN, got.GetWeakestDevice())
require.Equal(t, oldRevision, got.GetMetadata().Revision, "revision should not change")

// Delete the Webauthn device and the state should be MFA_DEVICE_KIND_UNSET
err = identity.DeleteMFADevice(ctx, "bob", webauthnDevice.Id)
Expand Down

0 comments on commit 8492e07

Please sign in to comment.