Image caption Prime Minister John Key said the legislation was not about "wholesale spying"

New Zealand has passed legislation allowing its main intelligence agency to spy on residents and citizens.

Parliament voted 61 to 59 on Wednesday to expand the powers of the Government Communications Security Bureau (GCSB).

GCSB was only previously allowed to spy on those with no right to reside in New Zealand.

The new legislation follows a row over the GCSB's surveillance of internet mogul Kim Dotcom, a German national with New Zealand residency.

GCSB worked with US officials to investigate Mr Dotcom over allegations of online piracy and money laundering. Mr Dotcom, who denied the charges, is fighting extradition to the US.

The case prompted an apology from Prime Minister John Key to Mr Dotcom last year.

In March, a court ruled that Kim Dotcom could sue the GCSB for illegally spying on him.

'Surveillance state'

The bill was passed after three readings in parliament.

"This is not, and never will be, about wholesale spying on New Zealanders," Mr Key said in parliament.

He added the legislation "makes it clear what the GCSB may or may not do".

Under the new legislation, the GCSB can now spy on residents and citizens to assist the police, Security Intelligence Service and the military with lawful authorisation.

The legislation was faced with opposition, included from rights groups, legal advocates and technology groups.

Mr Dotcom said on Tuesday its approval would make New Zealand a "surveillance state".

Mr Key had said that he has "rarely seen so much misinformation and conspiracy as has been perpetrated about this bill".

"That has some citizens agitated and alarmed, which I regret," he added.