President Trump’s decision to authorize the killing of a top Iranian general and his threats since are quickly reshaping the contours of the 2020 presidential race, with leaders in both parties evaluating advantages and risks as they attempt to position themselves for maximum gain.

Trump, who rarely makes major moves without first calculating the ramifications for his popularity and self-image, has confided to advisers that he sees a political upside in his hard-line approach to Iran at the dawn of this year’s campaign, according to two White House officials and several senior Republicans.

Trump believes he has an opportunity to expand his support among voters as a wartime commander in chief and is trying to cast his Democratic critics as soft on terrorism, they said. They added that he sees his party as more united behind him than ever, even as his impeachment trial looms in the Senate, with some Republicans now arguing that it would be irresponsible and dangerous to remove a president amid a national security crisis.

Trump struck a harshly partisan tone Monday that foreshadowed the political battles to come. Calling into Rush Limbaugh’s nationally syndicated radio show, the president maligned Democrats for “trying to make” Maj. Gen. Qasem Soleimani “sound like he was this wonderful human being” and argued that “elements of that party [are] openly supporting Iran, an enemy of the United States.”

Still, Trump risks cracking apart his support among voters who cheered his noninterventionist streak in 2016, when he called the Iraq War a “horrible mistake” while using militaristic and brutal language when speaking of Muslims and Middle Eastern issues.

Many of Trump’s Democratic challengers, meanwhile, believe the president’s actions carry a political benefit for them among swing voters fearful of yet another war, who could more sharply question Trump’s worldview and fitness for office.

On the campaign trail in Iowa, Sen. Elizabeth Warren (D-Mass.) repeatedly has blasted the killing of Soleimani as “reckless” and said she would not have ordered the strike were she president. Sen. Bernie Sanders (I-Vt.) has been critical as well, trying to rally antiwar Democrats to his side ahead of what his advisers envision as a potential showdown with former vice president Joe Biden for the Democratic presidential nomination.

But with less than a month to go before Iowa’s Feb. 3 caucuses, the consequences for the Democrats’ primary contest are so far unclear, as the party’s antiwar wing takes on the more moderate polling leader, Biden, who once supported military intervention in Iraq.

It is too early to accurately measure the political fallout, and operatives from both parties warned against making assumptions about public sentiments.

“President Trump is probably getting a lot of ‘attaboys’ from his friends, and he is producing a daily reality show, but politically and policy-wise, this is going to be much more complicated than just saying, ‘Hey, I took a bad guy out,’ ” said David Axelrod, a former senior adviser to President Barack Obama.

Michael Steele, a former Republican National Committee chairman, said: “The Republican Party is going to own this, and they need to be careful. You can cheerlead the president, but do you really know what he wants to do here, in the middle of a 17-year narrative, and how it’ll play out? No, you don’t.”

On Capitol Hill, congressional Republicans rallied around the president Monday. Senate Majority Leader Mitch McConnell (R-Ky.) said in a floor speech that some Democrats “rushed to downplay Soleimani’s evil while presenting our own president as the villain.” He added, “Can we not maintain a shred, just a shred of national unity for five minutes, for five minutes before deepening the partisan trenches?”

Beneath the veneer of unity, however, there is some dissent among Republicans. Sen. Rand Paul (R-Ky.), who has long been a noninterventionist, shied away from a direct confrontation with Trump but tweeted Monday: “Great nations avoid endless wars by becoming LESS involved in the turmoil of the Middle East, which would mean less troops not more.”

At his Mar-a-Lago resort in Florida over the past week, Trump privately told advisers he was surprised at how many Democrats criticized him for taking action — and he has spoken encouragingly about how his decision is being received by many Americans on social media and in news coverage, according to two White House officials who spoke on the condition of anonymity because they were not authorized to speak publicly.

Trump has boasted that he feels like he caught the Iranians “red-handed” as they attempted to plan future attacks and finds it “almost funny” that Democrats “can’t say, ‘Wow, this is great,’ ” according to one of the officials.

Trump’s confidence in himself and Republicans has been bolstered by Vice President Pence and Secretary of State Mike Pompeo, who have repeatedly assured the president that congressional Republicans support taking military action, the officials said. House Minority Leader Kevin McCarthy (R-Calif.), who dined with the president Thursday night, has done the same, they added.

Trump’s private comments, in phone conversations and as he mingled with guests there over dinner, have also routinely come back to his reelection chances — going between foreign policy decision-making and 2020 talk.

As last week’s strike was unfolding, Trump casually discussed whether “Bernie or Biden” would be easier to beat, according to conservative talk-show host Howie Carr, who published an essay about his time Thursday night at Mar-a-Lago.

“Who’d be better to run against? Bernie or Biden?” Trump asked, according to Carr’s account. “He wanted to talk 2020 politics.”

In particular, Trump is keeping close watch on Biden, whom he continues to see as his likely rival in the fall election campaign, the officials and others close to the president said.

White House officials and leading Republicans said, however, that Trump should not be considered a hawk and that he was prone in meetings to bemoan “nation-building.”

For Trump, appearing tough on Iran while remaining reluctant to commit ground troops is an uneasy balance but one achieved by past GOP presidents who wanted to be seen as strong but not as interventionists, according to former House speaker Newt Gingrich, a Trump ally.

“Nobody thought of [Ronald] Reagan as a wartime president because he was very surgical, used our strengths and didn’t put American boots on the ground,” Gingrich said.

For Trump, there are considerable political risks as he weighs options on Iran and contemplates additional military actions.

History is replete with examples of presidents accruing short-term benefits from war and military confrontations but suffering long-term consequences. Americans initially rallied around President Jimmy Carter in 1979 during the Iran hostage crisis, but public opinion quickly turned on him and Carter was defeated for reelection a year later.

More recently, George W. Bush initially was buoyed after sending Americans to war in Afghanistan and Iraq, but support for those conflicts waned, and he only narrowly won reelection in 2004, and his party lost its congressional majorities in 2006.

“Ask George W. Bush about how these things go,” said former Arizona senator Jeff Flake (R), a Trump critic. “It’s all fraught with danger.”

There are serious differences with today’s crisis, in part because of Trump’s provocative posture.

“If something bad happens, it will be more than on his watch; it will be on him individually,” said Peter Hart, a veteran Democratic pollster. “He precipitated the action, versus something that happened to the country, therefore we rallied around him. The downside is he owns it from beginning to end.”

Robert Shrum, who advised Democrat John F. Kerry’s unsuccessful campaign to defeat Bush in 2004, warned: “A new war in the Middle East would create incredible and unpredictable problems for Trump. Now, the country is not up for a new war.”

Former Vermont governor Howard Dean, who rode antiwar sentiment to a strong showing early in the 2004 Democratic presidential primaries, said Sanders could benefit from a renewed interest in Iraq but that such an upswing was far from certain. He said the “big push 29 days before Iowa is going to be about the whole chess game that’s going on among the front-runners, not just one issue.”

“The real way this matters down the road is in how Trump handles it, and it could show more people he’s incompetent. The more people who see that, the better,” Dean said. “As for the antiwar wing of the Democratic Party, they’re already galvanized against Trump, so it’s more about picking up voters around the margins.”

Sanders and Warren are not the only Democratic candidates seizing on Iran as an issue. Others, too, are navigating the terrain with new vigor, including two contenders rising in Iowa polls: former South Bend, Ind., mayor Pete Buttigieg and Sen. Amy Klobuchar (D-Minn.).

Klobuchar has stressed her experience, while Buttigieg, a retired military intelligence officer, has spoken about understanding the cost of war. “My mind is with the troops who are moving to the Middle East, and having known what it’s like to be on the inside of one of those airplanes, you need to be able to trust that everybody up your chain of command has thought through what’s ahead,” Buttigieg said Monday on ­MSNBC’s “Morning Joe.”

But Joseph I. Lieberman, a former senator and Democratic vice presidential nominee, issued a warning to the party’s leaders about a negative reaction to Trump’s order to kill Soleimani.

“If enough voters decide that Democrats can’t be trusted to keep America safe, Mr. Trump won’t have much trouble winning a second term in November,” Lieberman, who has long been known for his hawkish foreign policy views, wrote in a Wall Street Journal op-ed.

Amy B Wang contributed to this report.