ANKARA — Turkey’s parliament approved a controversial law on Thursday expanding the powers of the national spy agency and setting prison terms for publishing leaked information as the government fights back against a widening corruption scandal.

The new legislation, passed after a heated debate in parliament, is the latest ammunition being deployed by Prime Minister Recep Tayyip Erdogan as he seeks to push back against the greatest challenge yet to his 11-year rule.

The law was first introduced by lawmakers from Erdogan’s AKP party in the run-up to local elections last month and has undergone a series of amendments to satisfy demands made by President Abdullah Gul, who is now expected the ratify the measures.

The Islamic-rooted AKP won a resounding victory in the local polls despite a corruption scandal that has been fueled by a steady flow of leaked classified information.

The new law is a direct attempt to stop the leaks, which have implicated key members of Erdogan’s inner circle and included an alleged recording of the prime minister ordering his son to hide illicit cash.

It provides expanded scope for the MIT spy agency to tap into private phone conversations and collect intelligence related to “terrorism, international crimes and external intelligence gathering.”

It also sets prison terms of up to 10 years for journalists and others who publish leaked information.

Also under the law, MIT agents who make contact with “terrorist” organisations as part of their job, including with the Kurdish separatist group PKK, will be granted immunity.

Earlier this year, MIT agents were stopped by Turkish police officers smuggling weapons into Syria, despite denials by Ankara that the government was aiding the rebels there.

Turkey’s opposition fiercely opposes the law and has vowed to continue the fight in the Constitutional Court, which has become a key battleground in the political feuds embroiling the country.

“With this law, the government will succeed in burying all the corruption investigations it faces and Turkey will go from being a country of laws to a country where democracy is destroyed,” said senior lawmaker Atilla Kart, of the main opposition party CHP.

Kart said the law would silence critics of Erdogan’s rule amid other moves by the government to face down criticism and allegations of corruption, including bans of Internet sites such as Twitter and YouTube.

Erdogan’s AKP party defends the law and says the only intention is to improve the powers of the spy agency to protect the country.

The premier has accused Fethullah Gulen, a US-based Muslim cleric, and his loyalists in the Turkish police and justice system of being behind the corruption probe and the leaks.

The government has also reacted by embarking on a mass purge of police and prosecutors believed to be close to Gulen’s Hizmet movement.

Last week, the Constitutional Court annulled sections of another controversial law intended to tighten the government’s control over the judiciary.