Switching OAuth Clients within the app

The core app provides the ability to define multiple OAuth clients for different urls. You can change the base URL from the Settings Screen inside the app and the relevant OAuth client settings will be picked up.
Click the three-dot menu on the login page and you will be navigated to the Settings Screen, where you can click on OpenSRP base URL and will be presented with the following.

How to implement

To support switching between multiple environments, extend PropertiesSyncConfiguration for the implementation of SyncConfiguration. PropertiesSyncConfiguration depends on EnvironmentManager to load different Environment's.

Environment is a POJO that defines the OAuth client.secret, client.id against a url and a name.

The EnvironmentManager class requires a serialized JSON array of Environment objects. You can load this JSON Array from your assets folder or generate it using build.gradle and local.properties. Once you have a serialized JSON Array initialize EnvironmentManager using it.

After implementing the PropertiesSyncConfiguration and initializing the CoreLibrary with it. The app automatically picks the client.id and client.secret for the base URL stored in preference at DRISHTI_BASE_URL and use it for subsequent calls.

Sample Implementation

In your Application onCreate initialize CoreLibary with the SyncConfiguration

String json = loadEnvironments(); EnvironmentManager envManager = new EnvironmentManager(json); SyncConfiguration syncConfig = new MyPropertiesSyncConfiguration(envManager); CoreLibrary.init(context, syncConfig, timeStamp, new P2POptions(true));

 

MyPropertiesSyncConfiguration.java

class MyPropertiesSyncConfiguration extends PropertiesSyncConfiguration { //Implement abstract methods ... }

Sample JSON Array defining the environmets

[ { "env": "preview", "client.id": "preview-server", "client.secret": "19aaaf41-eac2-4ef9-a537-4fe62416b522", "url": "https://preview.smartregister.org/opensrp/" }, { "env": "qa", "client.id": "qa-server", "client.secret": "7a89e3fe-99fe-4af8-a699-c213192c634a", "url": "https://qa.smartregister.org/opensrp/" }, { "env": "stage", "client.id": "stage-server", "client.secret": "7d68f02d-7c32-4e3f-9c48-065dc2a6b8d0", "url": "https://stage.smartregister.org/opensrp" } ]

Once you have this in place the app will automatically pick the client.id and client.secret for the base URL stored in preference at DRISHTI_BASE_URL and all subsequent calls that need OAuth configs will use these.

Sample Code to load OAuth Configs from local.properties using build.gradle

In your local.properties file define the environments as:

Add the following to your app level build.gradle:

Define getJavaMap in your gradle file as:

You can now use BuildConig.ENV_ARRAY to initialize EnviromentManageer: