|
| 1 | +/* |
| 2 | + * Copyright (c) 2025-present, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Redistribution and use of this software in source and binary forms, with or |
| 5 | + * without modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * - Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer in the documentation |
| 11 | + * and/or other materials provided with the distribution. |
| 12 | + * - Neither the name of salesforce.com, inc. nor the names of its contributors |
| 13 | + * may be used to endorse or promote products derived from this software without |
| 14 | + * specific prior written permission of salesforce.com, inc. |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +package com.salesforce.androidsdk.ui |
| 29 | + |
| 30 | +import android.net.Uri |
| 31 | +import androidx.core.net.toUri |
| 32 | +import com.salesforce.androidsdk.app.SalesforceSDKManager |
| 33 | +import com.salesforce.androidsdk.config.BootConfig |
| 34 | +import com.salesforce.androidsdk.config.LoginServerManaging |
| 35 | +import com.salesforce.androidsdk.config.RuntimeConfig.ConfigKey.AppServiceHosts |
| 36 | +import com.salesforce.androidsdk.config.RuntimeConfig.ConfigKey.OnlyShowAuthorizedHosts |
| 37 | +import com.salesforce.androidsdk.config.RuntimeConfig.getRuntimeConfig |
| 38 | + |
| 39 | +/** |
| 40 | + * For Salesforce Identity UI Bridge API support, an overriding front door |
| 41 | + * bridge URL to use in place of the default initial URL. |
| 42 | + */ |
| 43 | +internal class FrontdoorBridgeLoginOverride( |
| 44 | + /** |
| 45 | + * For Salesforce Identity UI Bridge API support, an overriding front door |
| 46 | + * bridge URL to use in place of the default initial URL |
| 47 | + */ |
| 48 | + val frontdoorBridgeUrl: Uri, |
| 49 | + |
| 50 | + /** |
| 51 | + * For Salesforce Identity UI Bridge API support, the optional web server |
| 52 | + * flow code verifier accompanying the front door bridge URL |
| 53 | + */ |
| 54 | + val codeVerifier: String? = null, |
| 55 | + |
| 56 | + /** |
| 57 | + * The selected app login server. This is intended for test automation only |
| 58 | + */ |
| 59 | + selectedAppLoginServer: String = SalesforceSDKManager.getInstance().loginServerManager.selectedLoginServer.url, |
| 60 | + |
| 61 | + /** |
| 62 | + * The preference for using mobile device management preferences for |
| 63 | + * allowing the addition and switching of app login servers. This is |
| 64 | + * intended for test automation only |
| 65 | + */ |
| 66 | + addingAndSwitchingLoginServersPerMdm: Boolean = true, |
| 67 | + |
| 68 | + /** |
| 69 | + * The preference for allowing the addition and switching of app login |
| 70 | + * servers when the MDM preference is ignored. This is intended for test |
| 71 | + * automation only |
| 72 | + */ |
| 73 | + addingAndSwitchingLoginServerOverride: Boolean = false |
| 74 | +) { |
| 75 | + |
| 76 | + /** |
| 77 | + * For Salesforce Identity UI Bridge API support, indicates if the |
| 78 | + * overriding front door bridge URL has a consumer key value that matches |
| 79 | + * the app config. |
| 80 | + */ |
| 81 | + var matchesConsumerKey: Boolean = false |
| 82 | + private set |
| 83 | + |
| 84 | + /** |
| 85 | + * For Salesforce Identity UI Bridge API support, indicates if the |
| 86 | + * overriding front door bridge URL has a host that matches the app's |
| 87 | + * selected login server. |
| 88 | + */ |
| 89 | + var matchesLoginHost: Boolean = false |
| 90 | + private set |
| 91 | + |
| 92 | + init { |
| 93 | + val frontdoorBridgeUrlComponents = frontdoorBridgeUrl.toString() |
| 94 | + val frontdoorBridgeUri = frontdoorBridgeUrlComponents.toUri() |
| 95 | + val startUrlParam = frontdoorBridgeUri.getQueryParameter("startURL") |
| 96 | + |
| 97 | + // Check if the client_id matches the app's consumer key |
| 98 | + startUrlParam?.let { startUrlString -> |
| 99 | + val startUri = startUrlString.toUri() |
| 100 | + val frontdoorBridgeUrlClientId = startUri.getQueryParameter("client_id") |
| 101 | + |
| 102 | + frontdoorBridgeUrlClientId?.let { clientId -> |
| 103 | + val appConsumerKey = BootConfig.getBootConfig(SalesforceSDKManager.getInstance().appContext).remoteAccessConsumerKey |
| 104 | + matchesConsumerKey = clientId == appConsumerKey |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Check if the front door URL host matches the app's selected login server |
| 109 | + val addingAndSwitchingLoginServersAllowedResolved = if (!addingAndSwitchingLoginServersPerMdm) { |
| 110 | + addingAndSwitchingLoginServerOverride |
| 111 | + } else { |
| 112 | + addingAndSwitchingLoginServersAllowed |
| 113 | + } |
| 114 | + |
| 115 | + val frontdoorBridgeUrlAppLoginServerMatch = FrontdoorBridgeUrlAppLoginServerMatch( |
| 116 | + frontdoorBridgeUrl = frontdoorBridgeUrl, |
| 117 | + loginServerManaging = loginServerManager, |
| 118 | + addingAndSwitchingLoginServersAllowed = addingAndSwitchingLoginServersAllowedResolved, |
| 119 | + selectedAppLoginServer = selectedAppLoginServer |
| 120 | + ) |
| 121 | + |
| 122 | + var appLoginServer = frontdoorBridgeUrlAppLoginServerMatch.appLoginServerMatch |
| 123 | + if (appLoginServer == null && addingAndSwitchingLoginServersAllowedResolved) { |
| 124 | + appLoginServer = frontdoorBridgeUrl.host |
| 125 | + } |
| 126 | + |
| 127 | + appLoginServer?.let { appLoginServer -> |
| 128 | + matchesLoginHost = true |
| 129 | + // Set the login appLoginServer on the server manager |
| 130 | + val loginServerManager = SalesforceSDKManager.getInstance().loginServerManager |
| 131 | + val loginUrl = "https://$appLoginServer" |
| 132 | + loginServerManager.addCustomLoginServer(loginUrl, loginUrl) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private val addingAndSwitchingLoginServersAllowed: Boolean |
| 137 | + get() { |
| 138 | + val runtimeConfig = getRuntimeConfig(SalesforceSDKManager.getInstance().appContext) |
| 139 | + val onlyShowAuthorizedServers = runtimeConfig.getBoolean(OnlyShowAuthorizedHosts) |
| 140 | + val mdmLoginServers = try { |
| 141 | + runtimeConfig.getStringArrayStoredAsArrayOrCSV(AppServiceHosts) |
| 142 | + } catch (_: Exception) { |
| 143 | + null |
| 144 | + } |
| 145 | + return !onlyShowAuthorizedServers && (mdmLoginServers?.isEmpty() != false) |
| 146 | + } |
| 147 | + |
| 148 | + private val loginServerManager: LoginServerManaging |
| 149 | + get() = SalesforceSDKManager.getInstance().loginServerManager |
| 150 | +} |
| 151 | + |
0 commit comments