Configuring an Android App as an Identity Provider

You can configure any app built on Mobile SDK 11.0 or later as an identity provider. You call a method to define which identity provider client apps you want to connect to, then select the identity provider client in your app’s UI.
The easiest way to create an identity provider app is by using the Mobile SDK AndroidIDPTemplate. This template is available on GitHub in the github.com/forcedotcom/SalesforceMobileSDK-Templates repo. Use the forcedroid createwithtemplate command with the URI of the template repo, as shown in the following command-line example.
1$ forcedroid createwithtemplate
2Enter URI of repo containing template application: AndroidIDPTemplate
3Enter your application name: MyIDP-Android
4Enter your package name: com.acme.android
5Enter your organization name (Acme, Inc.): Acme Systems
6Enter output directory for your app (leave empty for the current directory): MyIDP-Android

Convert an Existing Mobile SDK Android App into an Identity Provider

To let the identity provider app know about the client app you want it to service, define one (or multiple) client app configurations in the onCreate method of its application subclass.
1class MyApplication : Application() {
2
3 override fun onCreate() {
4   super.onCreate()
5   SalesforceSDKManager.initNative(applicationContext, MainActivity::class.java)
6   SalesforceSDKManager.getInstance()
7     .setAllowedSPApps(listOf(
8     SPConfig(
9       "com....restexplorer", /* Package name of SP app */
10       "com....restexplorer.ExplorerActivity", /* Main activity of SP app */
11       "<-- the oauth consumer key of the sp app -->",
12       "<-- the oauth callback url of the sp app -->",
13       arrayOf("api", "web") /* Oauth scopes of the SP app */
14   ),
15 }  
16 /* Code here not shown */
17}
To kick off the IDP-initiated login flow, the following example code calls kickOffIDPInitiatedLoginFlow for the chosen client app package name, which handles the status updates. In this example, updates are presented in a toast notification, but the application ultimately decides how to show progress to the user. You can find the corresponding version of this code in the UI of your IDP app. Examples of the selection UI can be found on GitHub, in the https://github.com/forcedotcom/SalesforceMobileSDK-Templates repo. For Android, check out the AndroidIDPTemplate.
1SalesforceSDKManager.getInstance().idpManager?.let { idpManager ->
2   idpManager.kickOffIDPInitiatedLoginFlow(this, spAppPackageName,
3       object:IDPManager.StatusUpdateCallback {
4           override fun onStatusUpdate(status: IDPManager.Status) {
5               CoroutineScope(Dispatchers.Main).launch {
6                   Toast.makeText(
7                       applicationContext,
8                       getString(status.resIdForDescription),
9                       Toast.LENGTH_SHORT
10                   ).show()
11               }
12           }
13       }
14   )
15} ?: run {
16   Log.e(TAG, "Cannot proceed with launch of ${appName} - not configured as IDP")
17}
The onStatusUpdate callback can return any of the following status updates.
  • LOGIN_REQUEST_SENT_TO_SP
  • GETTING_AUTH_CODE_FROM_SERVER
  • ERROR_RECEIVED_FROM_SERVER
  • AUTH_CODE_SENT_TO_SP
  • SP_LOGIN_COMPLETE