diff --git a/server/channels/api4/user.go b/server/channels/api4/user.go index ad93928874..ec57306c2f 100644 --- a/server/channels/api4/user.go +++ b/server/channels/api4/user.go @@ -1650,7 +1650,12 @@ func updateUserMfa(c *Context, w http.ResponseWriter, r *http.Request) { return } - if user, err := c.App.GetUser(c.Params.UserId); err == nil { + if appErr := c.App.MFARequired(c.AppContext); !c.AppContext.Session().Local && c.AppContext.Session().UserId != c.Params.UserId && appErr != nil { + c.Err = appErr + return + } + + if user, appErr := c.App.GetUser(c.Params.UserId); appErr == nil { audit.AddEventParameterAuditable(auditRec, "user", user) } @@ -1672,8 +1677,8 @@ func updateUserMfa(c *Context, w http.ResponseWriter, r *http.Request) { c.LogAudit("attempt") - if err := c.App.UpdateMfa(c.AppContext, activate, c.Params.UserId, code); err != nil { - c.Err = err + if appErr := c.App.UpdateMfa(c.AppContext, activate, c.Params.UserId, code); appErr != nil { + c.Err = appErr return } diff --git a/server/channels/api4/user_test.go b/server/channels/api4/user_test.go index 2b8146b0dd..ece4cf0039 100644 --- a/server/channels/api4/user_test.go +++ b/server/channels/api4/user_test.go @@ -3443,19 +3443,43 @@ func TestUpdateUserMfa(t *testing.T) { defer th.TearDown() th.App.Srv().SetLicense(model.NewTestLicense("mfa")) - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableMultifactorAuthentication = true }) + t.Run("Without enforcing", func(t *testing.T) { + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableMultifactorAuthentication = true }) - session, _ := th.App.GetSession(th.Client.AuthToken) - session.IsOAuth = true - th.App.AddSessionToCache(session) + session, _ := th.App.GetSession(th.Client.AuthToken) + session.IsOAuth = true + th.App.AddSessionToCache(session) - resp, err := th.Client.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) - require.Error(t, err) - CheckForbiddenStatus(t, resp) + defer th.Server.Platform().ClearUserSessionCacheLocal(th.BasicUser.Id) - th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { - _, err = client.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) + resp, err := th.Client.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) + require.Error(t, err) + CheckForbiddenStatus(t, resp) + + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, err := client.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) + require.NoError(t, err) + }) + }) + + t.Run("Enforcing", func(t *testing.T) { + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.EnableMultifactorAuthentication = true + *cfg.ServiceSettings.EnforceMultifactorAuthentication = true + }) + + resp, err := th.Client.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) require.NoError(t, err) + CheckOKStatus(t, resp) + + resp, err = th.LocalClient.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) + require.NoError(t, err) + CheckOKStatus(t, resp) + + resp, err = th.SystemAdminClient.UpdateUserMfa(context.Background(), th.BasicUser.Id, "12345", false) + require.Error(t, err) + CheckForbiddenStatus(t, resp) + CheckErrorID(t, err, "api.context.mfa_required.app_error") }) }