A federal judge has spared from a lengthy prison sentence a man who left his home in Brooklyn to join the Islamic State, crediting him for almost two years of time served and for cooperating with the United States government after he became disillusioned with the organization.

The defendant in the case, who is identified as John Doe in court papers to protect his family while he works with American authorities, was convicted of two terrorism-related charges and faced a statutory maximum of 25 years in prison.