twilio / authy-java

Java Client for Twilio Authy Two-Factor Authentication (2FA) API
https://www.twilio.com/docs/authy/api
MIT License
66 stars 46 forks source link

Add onetouch support #19

Closed onlynone closed 7 years ago

onlynone commented 8 years ago

These commits add support for the one touch endpoint.

First I had to allow POSTing of data serialized in formats other than xml since the One Touch endpoint doesn't support xml for some reason, only application/x-www-form-urlencoded. I had to change things so that Resource.request() takes a newly created Request type rather than Response (I don't know why the data being sent to the API is called a Response). The new Request type has methods to serialize itself to xml, json, and a standard form encoded query string. It also lets subclasses specify which is their preferred serialization. The Resource.writeXml() method actually serializes the given Request object however the object prefers and sets the Content-Type appropriately.

Then I added the necessary classes to support creating approval requests, and then checking their status. There was some complication because of the way every response is wrapped in a hash, except when there's an error in which case the top level xml element is errors. I created a new class called HashWrapper which the new response classes inherit from. I also create a new Users.instanceFromXml() method which will create any HashWrapper from the given xml. It also takes care of when errors are returned instead. It will create a new object and then call obj.setErrors(errors) with the errors object and return the object.

There's a lot I had to shoe-horn in here to make it all work, and I'm not sure if this code should be merged as-is. I'd love some feedback, especially from other users of the API. I think there should be discussion about how to implement support for One Touch and other unsupported endpoints. Including scrapping the entire API as written and starting from scratch. It's very confusing to have Responses sent with the request, and for there to be the User, Users, and Users.User classes, and Token, Tokens, and Tokens.InternalToken.