dmfs/oauth2-essentials

Implement Serializable

koh-osug opened this issue · 2 comments

Under Android the initial created AuthorizationCodeGrant is lost when leaving the activity to approve the access on a web page using the code grant flow. One option is to make the AuthorizationCodeGrant and all related classes implement Serializable to be able persist it. Or an alternative constructor from String values would be useful.

dmfs commented

We've already considered this. OAuth2InteractiveGrant, the interface implemented by AuthorizationCodeGrant has a state() method which issues a Serializable OAuth2InteractiveGrant.OAuth2GrantState object. This object can be used later on to restore the AuthorizationCodeGrant object.

This is how it would look like:

// create your auth code grant object
OAuth2InteractiveGrant authCodeGrant = …
…
// if the auth code grant needs to be serialized just call the state method
OAuth2GrantState grantState = authCodeGrant.state();
// grantState is Serializable and can be put into a Bundle// later, when you need the auth code grant you can get it back from the state
// by calling the grant(OAuth2Client) method 
OAuth2InteractiveGrant authCodeGrant = grantState.grant(oauth2Client);

The result will be in exactly the same state as the grant when you called the state method.

The advantage of this method is that only the state object needs to be Serializable.

Great. This works.