Browse Source

check admins' two-factor requirements in the disable API as well

Signed-off-by: Nicola Murino <[email protected]>
Nicola Murino 1 year ago
parent
commit
76ffa107dd
2 changed files with 31 additions and 0 deletions
  1. 7 0
      internal/httpd/api_admin.go
  2. 24 0
      internal/httpd/httpd_test.go

+ 7 - 0
internal/httpd/api_admin.go

@@ -100,6 +100,13 @@ func disableAdmin2FA(w http.ResponseWriter, r *http.Request) {
 		sendAPIResponse(w, r, err, "", getRespStatus(err))
 		return
 	}
+	if admin.Username == claims.Username {
+		if admin.Filters.RequireTwoFactor {
+			err := util.NewValidationError("two-factor authentication must be enabled")
+			sendAPIResponse(w, r, err, "", getRespStatus(err))
+			return
+		}
+	}
 	admin.Filters.RecoveryCodes = nil
 	admin.Filters.TOTPConfig = dataprovider.AdminTOTPConfig{
 		Enabled: false,

+ 24 - 0
internal/httpd/httpd_test.go

@@ -3689,6 +3689,30 @@ func TestAdminTwoFactorRequirements(t *testing.T) {
 	assert.Contains(t, string(bodyResp), "two-factor authentication must be enabled")
 	err = resp.Body.Close()
 	assert.NoError(t, err)
+	// try to disable 2FA using the dedicated API
+	req, err = http.NewRequest(http.MethodPut, fmt.Sprintf("%v%v", httpBaseURL, path.Join(adminPath, altAdminUsername, "2fa", "disable")), nil)
+	assert.NoError(t, err)
+	setBearerForReq(req, token)
+	resp, err = httpclient.GetHTTPClient().Do(req)
+	assert.NoError(t, err)
+	assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
+	bodyResp, err = io.ReadAll(resp.Body)
+	assert.NoError(t, err)
+	assert.Contains(t, string(bodyResp), "two-factor authentication must be enabled")
+	err = resp.Body.Close()
+	assert.NoError(t, err)
+	// disabling 2FA using another admin should work
+	token, err = getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
+	assert.NoError(t, err)
+	req, err = http.NewRequest(http.MethodPut, path.Join(adminPath, altAdminUsername, "2fa", "disable"), nil)
+	assert.NoError(t, err)
+	setBearerForReq(req, token)
+	rr = executeRequest(req)
+	checkResponseCode(t, http.StatusOK, rr)
+	// check
+	admin, _, err = httpdtest.GetAdminByUsername(altAdminUsername, http.StatusOK)
+	assert.NoError(t, err)
+	assert.False(t, admin.Filters.TOTPConfig.Enabled)
 
 	_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
 	assert.NoError(t, err)