In the fall of 2018, when former CIA director John Brennan decided to write his memoir, he asked the agency for his official records, including his notes and any documents that he had reviewed and signed that were classified. The CIA, where Brennan had worked for nearly 30 years, said no.

It was a break with decades of tradition. The CIA routinely lets former directors review classified files when writing books about their careers. Their manuscripts are scrutinized to ensure they don’t expose any national secrets.

After months of “haggling,” Brennan learned that the CIA was following the orders of the man he had spent the previous two years publicly excoriating — President Trump, who in August 2018 “had issued a directive . . . that purportedly forbids anyone in the intelligence community from sharing classified information with me.”

Brennan recounts his battles with the president in the memoir he eventually wrote, with limited access to unclassified and heavily redacted material: “Undaunted: My Fight Against America’s Enemies, at Home and Abroad.” The Washington Post reviewed portions of the book, which is scheduled to be published on Oct. 6.

Trump’s directive appears tailored to one of his most prominent critics. In tweets, op-eds and television appearances, Brennan has denounced Trump as a unique threat to U.S. national security and democracy, once labeling his comments at a joint news conference in Helsinki with the president of Russia as “treasonous,” a comment that even some of Brennan’s friends and fellow Trump detractors said went too far.

Trump has accused Brennan of being a leading figure in a “deep state” conspiracy to undermine his campaign and discredit his election.

But national security experts said they’d never heard of a president targeting a former high-ranking official this way, critic or otherwise.

“It’s unprecedented, as far as I know,” said Mark Zaid, a lawyer who has represented government whistleblowers and former intelligence agency employees who have written books.

“This is demonstrative, once again, of a vindictive, political president whose actions have nothing to do with actual national security decisions,” Zaid said.

White House spokesman Judd Deere acknowledged that Trump had issued the directive. “The President has constitutional authority to control access to classified information, which he exercised here in view of Mr. Brennan’s erratic behavior and the President’s belief that access to classified information should be solely for the benefit of the government and the American people,” Deere said.

In August 2018, the same month that Brennan says Trump issued his order, the president said he was revoking Brennan’s security clearances.

Trump accused Brennan of making “a series of unfounded and outrageous allegations — wild outbursts on the Internet and television — about this Administration,” according to a statement then-White House press secretary Sarah Sanders read to reporters at a briefing.

Trump’s threat turns out to have been an empty one. “My security clearances have never been revoked because there is no legal basis to do so,” Brennan writes.

In January of this year, Brennan says, he wrote to the current CIA director, Gina Haspel, after learning about the president’s order to keep him from receiving classified information. “It is impossible to avoid the conclusion that the Agency’s refusal to grant my request reflects the current administration’s desire to punish and retaliate against me for speaking out as a private citizen — an abuse of power designed to chill the exercise of my First Amendment rights,” he argued.

Brennan says Haspel never responded to his letter or contacted him to discuss the situation, a silence he found “very disappointing” given their years working together at the CIA.

“So much for my fervent hope that interactions with my successors would be unencumbered by Washington’s partisan waters,” Brennan writes, in a dig at Haspel, who current and former officials say has made it her practice to stay on Trump’s good side.

The CIA declined to comment on Brennan’s book or the president’s directive.

Elsewhere in the memoir, Brennan recounts his first meeting with President-elect Trump, at a Jan. 6, 2017, briefing at Trump Tower in New York about Russia’s interference in the 2016 election. Brennan was joined by FBI Director James B. Comey, National Intelligence Director James R. Clapper Jr. and National Security Agency Director Mike Rogers.

Brennan writes that he had “serious doubts” Trump would guard the classified information he was about to receive from the country’s intelligence chiefs, in light of his “public praise of WikiLeaks,” which had published Democratic emails stolen by Russian intelligence agencies, and his “strange obsequiousness toward Vladimir Putin,” the Russian president.

“I had decided beforehand that I would share the full substance of CIA intelligence and analysis on Russian interference in the election without providing any specific details on the provenance of our knowledge,” Brennan writes.

Trump, he said, seemed more interested in challenging the intelligence assessment than in understanding the threat from Russia.

“Trump’s alertness never faded during the briefing, but his demeanor as well as his questions strongly revealed that he was uninterested in finding out what the Russians had done or in holding them to account,” Brennan writes.

“It was also my clear impression,” he continues, “that he was seeking most to learn what we knew and how we knew it. That deeply troubled me, as I worried about what he might do with the information he was being given.”

Trump repeatedly tried to deflect blame from Russia, Brennan writes, asserting on several occasions, “It could have been the Chinese.”

“We each took turns debunking his counterclaims,” Brennan writes, noting that the intelligence directors were unanimous in their conclusions that Russia had engaged in “an intense, determined, and broad-based effort” to interfere in the election.

As the briefing wrapped up, Trump turned to Brennan for what he describes as one last attempt to discredit the CIA’s findings.

“Anyone will say anything if you pay them enough,” the president-elect said.

“I stared at Trump, shook my head in disgusted disagreement and bit my tongue nearly hard enough to draw blood,” Brennan writes. He said he kept silent in the hope of not spoiling the incoming president’s relationship with the CIA.

Days later, Trump would compare intelligence officials to “Nazis” and accuse them of trying to destroy his reputation, after news accounts revealed Trump had also been briefed on a report by a former British intelligence officer alleging that Russia had compiled incriminating information about Trump from his earlier travels in the country.

From that point on, the intelligence community, and Brennan in particular, became one of Trump’s favorite political foils. Looking back, Brennan regrets that he didn’t challenge Trump in their first and only conversation.

“It was one of the few times in my professional career that I successfully suppressed my Irish temper when dealing with a politician,” he writes. “I wish I hadn’t.”