A Canadian man was sentenced to 40 years in federal prison on Wednesday for plotting to attack Times Square in New York and the city's subway.

U.S. prosecutors had asked for life in prison, while the defence argued Abdulrahman El Bahnasawy should only receive four or five years and get treatment for mental health and drug addiction problems.

El Bahnasawy, 20, of Mississauga, Ont., pleaded guilty in October 2016 to conspiring with ISIS operatives to commit terrorist acts, which included failed plans to bomb Times Square and the city's subway system, and to carry out mass shootings of civilians at concerts.

Judge Richard Berman said the sentence "was appropriate and necessary because of the serious and heinous nature of the offence."

Berman said he had taken El Bahnasawy's problems into account and wished him "the best of luck in his recovery," but noted the chance of a relapse.

"We just don't know, and in these matters there is no room for error."

El Bahnasawy's mother stood up and shouted in protest, "This is a sick boy! This is crazy. You have no justice," before being ushered out of the courtroom.

El Bahnasawy's lawyer, Dennis Edney, said the young man had been diagnosed with bipolar disorder, experienced psychotic episodes and has been addicted to drugs since age 14.

A portion of El Bahnasawy's letter to the court. (CBC)

Edney told CBC News the FBI was aware of his history because it was provided to him by the RCMP, which "unlawfully obtained" his client's medical records.

"This information contradicts the FBI's portrait of my client as a terrorist. Instead, he is a mentally ill youth who has attempted suicide on three occasions while incarcerated."

Edney said the FBI entrapped El Bahnasawy.​

"Today is not only a disgrace, it's very sad, particularly for a young, 20-year-old who'll spend 40 years in horrible conditions in an American prison."

El Bahnasawy's parents had repeatedly said their son's history of mental health and drug addiction made him susceptible to radicalization — first by ISIS recruiters, and then by the intelligence agents who were tracking him.

El Bahnasawy was 18 when he was arrested in New Jersey on a family trip. He had shipped bomb-making materials to a contact in the U.S. who turned out to be an undercover FBI agent. The agent had been communicating electronically with El Bahnasawy and two others in the months leading up to the failed attack.

El Bahnasawy was charged with seven terror-related offences, including conspiracy to use weapons of mass destruction and conspiracy to commit acts of terrorism transcending national boundaries.

El Bahnasawy ordered 18 kilos of peroxide online and sent it to an undercover FBI agent in New York. (FBI)

Two other men — Talha Haroon, a 19-year-old U.S. citizen living in Pakistan, and Russell Salic, a 37-year-old Philippine citizen — were arrested outside of the U.S. for their roles in the plot. They remain in foreign custody while the U.S. continues proceedings to extradite them.

El Bahnasawy himself wrote a 24-page submission to the court ahead of his sentencing hearing in April, asking a judge for a second chance, saying his frustration with how the West treated Islam had turned him toward extremism.

He apologized for his behaviour, writing, "I want to experience life away from drugs and away from war and violence."

The judge said he is open to a possible transfer to Canada so Bahnasawy could serve his sentence here.

Edney said he would consider such an application but wasn't optimistic.

"History will tell you that applications to transfer Canadians across the border to Canada [are] often very unsuccessful," he said.