Could tidy most of this away I think if you use a pre configured server with a custom token generator.