Skip to content
Open
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
79e735e
@W-21146662: [Android] App attestation integration testing (Initial P…
JohnsonEricAtSalesforce Mar 12, 2026
e912590
@W-21933885: [MSDK Android] App Attestation Implementation (Adapt To …
JohnsonEricAtSalesforce Apr 9, 2026
00a2924
@W-21933885: [MSDK Android] App Attestation Implementation (Extract T…
JohnsonEricAtSalesforce Apr 10, 2026
9c62dbe
@W-21933885: [MSDK Android] App Attestation Implementation (Replace A…
JohnsonEricAtSalesforce Apr 15, 2026
d1376e5
@W-21933885: [MSDK Android] App Attestation Implementation (Improve C…
JohnsonEricAtSalesforce Apr 16, 2026
e7f94f6
@W-21933885: [MSDK Android] App Attestation Implementation (Temporari…
JohnsonEricAtSalesforce Apr 16, 2026
763515c
@W-21933885: [MSDK Android] App Attestation Implementation (Improve T…
JohnsonEricAtSalesforce Apr 16, 2026
91e6079
@W-21933885: [MSDK Android] App Attestation Implementation (Extract C…
JohnsonEricAtSalesforce Apr 16, 2026
62660cf
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore Fl…
JohnsonEricAtSalesforce Apr 16, 2026
ba416fd
@W-21933885: [MSDK Android] App Attestation Implementation (In-Line R…
JohnsonEricAtSalesforce Apr 16, 2026
4a40216
@W-21933885: [MSDK Android] App Attestation Implementation (Return Nu…
JohnsonEricAtSalesforce Apr 16, 2026
2d2ca63
@W-21933885: [MSDK Android] App Attestation Implementation (Resolve T…
JohnsonEricAtSalesforce Apr 16, 2026
80771f6
@W-21933885: [MSDK Android] App Attestation Implementation (Implement…
JohnsonEricAtSalesforce Apr 16, 2026
7842f19
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore Fl…
JohnsonEricAtSalesforce Apr 16, 2026
3ebedd4
@W-21933885: [MSDK Android] App Attestation Implementation (Increase …
JohnsonEricAtSalesforce Apr 17, 2026
5219194
@W-21933885: [MSDK Android] App Attestation Implementation (Increase …
JohnsonEricAtSalesforce Apr 17, 2026
09651ad
@W-21933885: [MSDK Android] App Attestation Implementation (Increase …
JohnsonEricAtSalesforce Apr 17, 2026
1ab0d06
@W-21933885: [MSDK Android] App Attestation Implementation (First Att…
JohnsonEricAtSalesforce Apr 17, 2026
44849dc
@W-21933885: [MSDK Android] App Attestation Implementation (Finalize …
JohnsonEricAtSalesforce Apr 17, 2026
fb5c845
@W-21933885: [MSDK Android] App Attestation Implementation (Improve C…
JohnsonEricAtSalesforce Apr 17, 2026
86377f5
@W-21933885: [MSDK Android] App Attestation Implementation (Test Cove…
JohnsonEricAtSalesforce Apr 17, 2026
a2a451f
@W-21933885: [MSDK Android] App Attestation Implementation (Update Te…
JohnsonEricAtSalesforce Apr 17, 2026
0b158b5
@W-21933885: [MSDK Android] App Attestation Implementation (Light Cle…
JohnsonEricAtSalesforce Apr 17, 2026
4076ab2
@W-21933885: [MSDK Android] App Attestation Implementation (Correct S…
JohnsonEricAtSalesforce Apr 17, 2026
b0b7370
@W-21933885: [MSDK Android] App Attestation Implementation (Replace T…
JohnsonEricAtSalesforce Apr 17, 2026
e374d58
@W-21933885: [MSDK Android] App Attestation Implementation (New OAuth…
JohnsonEricAtSalesforce Apr 20, 2026
b1e5bad
@W-21933885: [MSDK Android] App Attestation Implementation (Temporary…
JohnsonEricAtSalesforce Apr 20, 2026
d488ff0
@W-21933885: [MSDK Android] App Attestation Implementation (Light Sel…
JohnsonEricAtSalesforce Apr 20, 2026
1325829
@W-21933885: [MSDK Android] App Attestation Implementation (Address U…
JohnsonEricAtSalesforce Apr 20, 2026
1ebcfe2
@W-21933885: [MSDK Android] App Attestation Implementation (Correct s…
JohnsonEricAtSalesforce Apr 20, 2026
9bf16d1
@W-21933885: [MSDK Android] App Attestation Implementation (Correct C…
JohnsonEricAtSalesforce Apr 20, 2026
e3398ad
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 20, 2026
6e06e9e
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 20, 2026
3a0e417
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 20, 2026
91a434c
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 20, 2026
466b058
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 20, 2026
5845735
@W-21933885: [MSDK Android] App Attestation Implementation (Resolve T…
JohnsonEricAtSalesforce Apr 21, 2026
9a2aca8
@W-21933885: [MSDK Android] App Attestation Implementation (Resolve T…
JohnsonEricAtSalesforce Apr 21, 2026
5035087
@W-21933885: [MSDK Android] App Attestation Implementation (Light Aut…
JohnsonEricAtSalesforce Apr 22, 2026
b8f1790
@W-21933885: [MSDK Android] App Attestation Implementation (Updated T…
JohnsonEricAtSalesforce Apr 22, 2026
7e5c233
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 22, 2026
f984d45
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 22, 2026
1304062
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 22, 2026
ab2af33
@W-21933885: [MSDK Android] App Attestation Implementation (Automated…
JohnsonEricAtSalesforce Apr 22, 2026
4029772
@W-21933885: [MSDK Android] App Attestation Implementation (Revert Li…
JohnsonEricAtSalesforce Apr 22, 2026
f3fad52
@W-21933885: [MSDK Android] App Attestation Implementation (Restore P…
JohnsonEricAtSalesforce Apr 23, 2026
48f98cc
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore sc…
JohnsonEricAtSalesforce Apr 23, 2026
484e620
@W-21933885: [MSDK Android] App Attestation Implementation (Limit Int…
JohnsonEricAtSalesforce Apr 23, 2026
b6c4354
@W-21933885: [MSDK Android] App Attestation Implementation (Add Code …
JohnsonEricAtSalesforce Apr 23, 2026
86f4b77
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore Sc…
JohnsonEricAtSalesforce Apr 23, 2026
0f15179
@W-21933885: [MSDK Android] App Attestation Implementation (Add Idlin…
JohnsonEricAtSalesforce Apr 23, 2026
9ddfa9e
@W-21933885: [MSDK Android] App Attestation Implementation (Add expli…
JohnsonEricAtSalesforce Apr 23, 2026
aeb7405
@W-21933885: [MSDK Android] App Attestation Implementation (Refactor …
JohnsonEricAtSalesforce Apr 23, 2026
e9d4890
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore Sc…
JohnsonEricAtSalesforce Apr 23, 2026
6a36d44
@W-21933885: [MSDK Android] App Attestation Implementation (Revert Ch…
JohnsonEricAtSalesforce Apr 23, 2026
6cdb0d1
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore To…
JohnsonEricAtSalesforce Apr 23, 2026
ffce923
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Token…
JohnsonEricAtSalesforce Apr 23, 2026
1b994fb
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Cross…
JohnsonEricAtSalesforce Apr 23, 2026
e9b32a4
@W-21933885: [MSDK Android] App Attestation Implementation (Fix AuthC…
JohnsonEricAtSalesforce Apr 23, 2026
97228a7
@W-21933885: [MSDK Android] App Attestation Implementation (Remove Du…
JohnsonEricAtSalesforce Apr 23, 2026
f93c9e5
@W-21933885: [MSDK Android] App Attestation Implementation (Re-Enable…
JohnsonEricAtSalesforce Apr 23, 2026
8975f32
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Token…
JohnsonEricAtSalesforce Apr 23, 2026
732a127
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Token…
JohnsonEricAtSalesforce Apr 23, 2026
9a39ef0
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Token…
JohnsonEricAtSalesforce Apr 23, 2026
7131666
@W-21933885: [MSDK Android] App Attestation Implementation (Add unreg…
JohnsonEricAtSalesforce Apr 23, 2026
15a9b04
@W-21933885: [MSDK Android] App Attestation Implementation (Fix onRes…
JohnsonEricAtSalesforce Apr 23, 2026
d2998a3
@W-21933885: [MSDK Android] App Attestation Implementation (Add addit…
JohnsonEricAtSalesforce Apr 23, 2026
9994387
@W-21933885: [MSDK Android] App Attestation Implementation (Fix addit…
JohnsonEricAtSalesforce Apr 23, 2026
29beb71
@W-21933885: [MSDK Android] App Attestation Implementation (Increase …
JohnsonEricAtSalesforce Apr 23, 2026
2c44b99
@W-21933885: [MSDK Android] App Attestation Implementation (Add regis…
JohnsonEricAtSalesforce Apr 23, 2026
966c13a
@W-21933885: [MSDK Android] App Attestation Implementation (Updated T…
JohnsonEricAtSalesforce Apr 23, 2026
c724a1a
@W-21933885: [MSDK Android] App Attestation Implementation (Add Test …
JohnsonEricAtSalesforce Apr 24, 2026
2ef4eca
@W-21933885: [MSDK Android] App Attestation Implementation (Add Test …
JohnsonEricAtSalesforce Apr 24, 2026
3673200
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Nativ…
JohnsonEricAtSalesforce Apr 24, 2026
f674131
@W-21933885: [MSDK Android] App Attestation Implementation (Reorder I…
JohnsonEricAtSalesforce Apr 24, 2026
a44d1f0
@W-21933885: [MSDK Android] App Attestation Implementation (Disable t…
JohnsonEricAtSalesforce Apr 24, 2026
30d3ca8
@W-21933885: [MSDK Android] App Attestation Implementation (Add Compr…
JohnsonEricAtSalesforce Apr 24, 2026
ecdd9d2
@W-21933885: [MSDK Android] App Attestation Implementation (Fix Login…
JohnsonEricAtSalesforce Apr 24, 2026
e2070f0
@W-21933885: [MSDK Android] App Attestation Implementation (Add Compr…
JohnsonEricAtSalesforce Apr 24, 2026
3e822b6
@W-21933885: [MSDK Android] App Attestation Implementation (Update Na…
JohnsonEricAtSalesforce Apr 24, 2026
98efcc6
@W-21933885: [MSDK Android] App Attestation Implementation (Clean Up …
JohnsonEricAtSalesforce Apr 24, 2026
d154ec6
@W-21933885: [MSDK Android] App Attestation Implementation (Updated T…
JohnsonEricAtSalesforce Apr 24, 2026
55d370f
@W-21933885: [MSDK Android] App Attestation Implementation (NativeLog…
JohnsonEricAtSalesforce Apr 24, 2026
89400d3
@W-21933885: [MSDK Android] App Attestation Implementation (Add Depen…
JohnsonEricAtSalesforce Apr 24, 2026
8a60818
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore ID…
JohnsonEricAtSalesforce Apr 24, 2026
261f874
@W-21933885: [MSDK Android] App Attestation Implementation (Fix IDPAu…
JohnsonEricAtSalesforce Apr 24, 2026
63172a1
@W-21933885: [MSDK Android] App Attestation Implementation (Add Debug…
JohnsonEricAtSalesforce Apr 24, 2026
6946375
@W-21933885: [MSDK Android] App Attestation Implementation (Updated T…
JohnsonEricAtSalesforce Apr 24, 2026
2ba67fb
@W-21933885: [MSDK Android] App Attestation Implementation (Updated I…
JohnsonEricAtSalesforce Apr 24, 2026
5f968fa
@W-21933885: [MSDK Android] App Attestation Implementation (Updated I…
JohnsonEricAtSalesforce Apr 25, 2026
d526e06
@W-21933885: [MSDK Android] App Attestation Implementation (Significa…
JohnsonEricAtSalesforce Apr 25, 2026
06c3719
@W-21933885: [MSDK Android] App Attestation Implementation (Initializ…
JohnsonEricAtSalesforce Apr 26, 2026
beb685d
@W-21933885: [MSDK Android] App Attestation Implementation (Ignore Un…
JohnsonEricAtSalesforce Apr 26, 2026
8d48205
@W-21933885: [MSDK Android] App Attestation Implementation (Correct U…
JohnsonEricAtSalesforce Apr 26, 2026
da8bce9
@W-21933885: [MSDK Android] App Attestation Implementation (Update Un…
JohnsonEricAtSalesforce Apr 26, 2026
76c91d8
Fix JaCoCo source directory configuration for code coverage reporting
JohnsonEricAtSalesforce Apr 27, 2026
b1e380e
@W-21933885: [MSDK Android] App Attestation Implementation (Update Un…
JohnsonEricAtSalesforce Apr 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/reusable-lib-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
- name: Install Dependencies
env:
TEST_CREDENTIALS: ${{ secrets.TEST_CREDENTIALS }}
# On PR runs, only SalesforceReact consumes the bundled index.android.bundle,
# so skip the yarn install + react-native bundle step for every other lib to
# save ~3-5 min per matrix job. Nightly runs still produce the bundle.
SKIP_REACT_NATIVE_BUNDLE: ${{ (inputs.is_pr && inputs.lib != 'SalesforceReact') && '1' || '0' }}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the feature, but could be useful to consider. While spending a lot of time watching ~25m CI runs, I asked our tools where a quick win would be in trimming that time. The analysis found that we're re-running yarn from scratch for all the modules instead of just for SalesforceReact. I believe this brought my run down to ~12m! I need to verify that over a few more runs, but if that's the case this could be a nice optimization. @brandonpage?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

run: |
./install.sh
echo $TEST_CREDENTIALS > ./shared/test/test_credentials.json
Expand Down Expand Up @@ -109,7 +113,6 @@ jobs:

if $IS_PR ; then
LEVELS_TO_TEST=$PR_API_VERSION
RETRIES=1
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandonpage, a little later you'll see a fix in the RestClientTest.java when I had generated by our tools to resolve unreliable behavior I saw in that test. I've been marking it @Ignore a lot since it's so unpredictable. The fix looks solid - It's so solid that our tools believe we don't need retry anymore. I'd love to see a "faster fail" on pull request runs since they can take a very long time. Thoughts?

fi

# Build test-targets-for-shard arguments from config file
Expand Down
17 changes: 11 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ git submodule update
git -C external/shared checkout -- samples/mobilesyncexplorer/bootconfig.json samples/accounteditor/bootconfig.json 2>/dev/null || true

# get react native
pushd "libs/SalesforceReact"
rm -rf node_modules
rm yarn.lock
yarn install
./node_modules/.bin/react-native bundle --platform android --dev true --entry-file node_modules/react-native-force/test/alltests.js --bundle-output ../test/SalesforceReactTest/assets/index.android.bundle --assets-dest ../test/SalesforceReactTest/assets/
popd
# Set SKIP_REACT_NATIVE_BUNDLE=1 to skip the yarn install and bundle step for
# jobs that do not consume libs/test/SalesforceReactTest/assets/index.android.bundle.
# Default behavior is unchanged (the bundle is produced).
if [ "${SKIP_REACT_NATIVE_BUNDLE:-0}" != "1" ]; then
pushd "libs/SalesforceReact"
rm -rf node_modules
rm yarn.lock
yarn install
./node_modules/.bin/react-native bundle --platform android --dev true --entry-file node_modules/react-native-force/test/alltests.js --bundle-output ../test/SalesforceReactTest/assets/index.android.bundle --assets-dest ../test/SalesforceReactTest/assets/
popd
fi

# Apply bootconfig placeholder substitution. Usage:
# apply_bootconfig_paths [sample_file] path1 path2 ...
Expand Down
1 change: 1 addition & 0 deletions libs/SalesforceSDK/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api("androidx.browser:browser:1.8.0") // Update requires API 36 compileSdk
Copy link
Copy Markdown

@github-actions github-actions Bot Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ A newer version of androidx.browser:browser than 1.8.0 is available: 1.10.0

api("androidx.work:work-runtime-ktx:2.10.3")
Copy link
Copy Markdown

@github-actions github-actions Bot Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ A newer version of androidx.work:work-runtime-ktx than 2.10.3 is available: 2.11.2


implementation("com.google.android.play:integrity:1.6.0")
implementation("com.google.accompanist:accompanist-drawablepainter:0.37.3")
implementation("com.google.android.material:material:1.13.0") // remove this when all xml is gone
implementation("androidx.appcompat:appcompat:1.7.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import com.salesforce.androidsdk.app.Features.FEATURE_BROWSER_LOGIN
import com.salesforce.androidsdk.app.Features.FEATURE_NATIVE_LOGIN
import com.salesforce.androidsdk.app.SalesforceSDKManager.Theme.DARK
import com.salesforce.androidsdk.app.SalesforceSDKManager.Theme.SYSTEM_DEFAULT
import com.salesforce.androidsdk.auth.AppAttestationClient
import com.salesforce.androidsdk.auth.AuthenticatorService.KEY_INSTANCE_URL
import com.salesforce.androidsdk.auth.HttpAccess
import com.salesforce.androidsdk.auth.HttpAccess.DEFAULT
Expand Down Expand Up @@ -226,6 +227,54 @@ open class SalesforceSDKManager protected constructor(
*/
val loginActivityClass: Class<out Activity> = nativeLoginActivity ?: webViewLoginActivityClass

/**
* The client side implementation of the Salesforce App Attestation External
* Client App (ECA) Plugin or null when app attestation is disabled.
*
* This property is not intended for public use outside of Salesforce Mobile
* SDK
*
* TODO: Make this Kotlin-internal once it is no longer referenced by Java. ECJ20260420
*/
@Volatile
var appAttestationClient: AppAttestationClient? = null
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A crux change is creating a new object to encapsulate all the things for the new Salesforce "Challenge" API, the Integrity Token Provider, the Token and providing that in the "Attestation" format the auth and token refresh endpoints now expect. That's here.

Our tools had some great suggestion around making this property thread safe, so I added @volatile, the private setter and a dedicated lock object based on tool feedback.

@VisibleForTesting
internal set

/** Lock object for synchronized access to the app Attestation Client */
private val appAttestationClientLock = Any()

/**
* Updates the Salesforce App Attestation ECA Plugin Client for the selected
* login server and matching Google Cloud Project ID. When using App
* Attestation, this value must match the linked Google Cloud Project ID
* for the app in Google Play Console's Play Integrity API and provided to
* the Salesforce App Attestation External Client App Plugin.
*
* @param apiHostName The Salesforce App Attestation External Client App
* (ECA) Plugin Challenge API Host Name. This usually matches the selected
* login server
* @param googleCloudProjectId The Google Cloud Project ID or null to
* disable Salesforce App Attestation
*/
fun updateAppAttestationClient(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one reads the description of this pull request, this is the entry point for an app to actually enable App Attestation.

apiHostName: String,
googleCloudProjectId: Long? = null
) {
synchronized(appAttestationClientLock) {
appAttestationClient = googleCloudProjectId?.let { appAttestationGoogleCloudProjectId ->
AppAttestationClient(
context = appContext,
apiHostName = apiHostName,
deviceId = deviceId,
googleCloudProjectId = appAttestationGoogleCloudProjectId,
remoteAccessConsumerKey = getBootConfig(appContext).remoteAccessConsumerKey,
restClient = clientManager.peekUnauthenticatedRestClient()
)
}
}
}

/**
* ViewModel Factory the SDK will use in LoginActivity and composable functions. Setting this will allow for
* visual customization without overriding LoginActivity.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
* Copyright (c) 2026-present, salesforce.com, inc.
* All rights reserved.
* Redistribution and use of this software in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of salesforce.com, inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission of salesforce.com, inc.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.androidsdk.auth

import android.content.Context
import androidx.annotation.VisibleForTesting
import com.google.android.play.core.integrity.IntegrityManagerFactory.createStandard
import com.google.android.play.core.integrity.IntegrityServiceException
import com.google.android.play.core.integrity.StandardIntegrityManager
import com.google.android.play.core.integrity.StandardIntegrityManager.PrepareIntegrityTokenRequest
import com.google.android.play.core.integrity.StandardIntegrityManager.StandardIntegrityTokenProvider
import com.google.android.play.core.integrity.StandardIntegrityManager.StandardIntegrityTokenRequest
import com.google.android.play.core.integrity.model.StandardIntegrityErrorCode.INTEGRITY_TOKEN_PROVIDER_INVALID
import com.salesforce.androidsdk.rest.AppAttestationChallengeApiClient
import com.salesforce.androidsdk.rest.RestClient
import com.salesforce.androidsdk.util.SalesforceSDKLogger.w
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import java.nio.charset.StandardCharsets.UTF_8
import java.security.MessageDigest
import java.util.Base64

/**
* App attestation features supporting the Salesforce App Attestation External
* Client App (ECA) Plugin, the Salesforce Challenge API, Google Play Integrity
* API and integration of app attestation with Salesforce Authentication.
*
* This method is not intended for public use outside of Salesforce Mobile SDK.
*
* TODO: Make this class internal once Java support is removed. ECJ20260421
*
* @param apiHostName The Salesforce App Attestation Challenge API host
* @param deviceId The device id, usually provided by the Salesforce SDK Manager
* @param googleCloudProjectId The Google Cloud Project ID used with Google Play
* Integrity API
* @param integrityManager The Google Play App Integrity API Integrity Manager.
* This parameter is intended for testing purposes only. Defaults to a new
* instance
* @param remoteAccessConsumerKey The Salesforce Connected App (CA) or External
* Client App (ECA)remote access consumer key, usually provided by the boot
* config
* @param restClient The REST client, usually provided by the Salesforce SDK
* Manager's unauthenticated REST client
*/
class AppAttestationClient(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object is the heart 'n soul of App Attestation.

context: Context,
@property:VisibleForTesting
internal val apiHostName: String,
@property:VisibleForTesting
internal val deviceId: String,
@property:VisibleForTesting
internal val googleCloudProjectId: Long,
@property:VisibleForTesting
internal val integrityManager: StandardIntegrityManager = createStandard(context),
@property:VisibleForTesting
internal val remoteAccessConsumerKey: String,
@property:VisibleForTesting
internal val restClient: RestClient,
) {


/** The Google Play Integrity API Token Provider */
@VisibleForTesting
internal var integrityTokenProvider: StandardIntegrityTokenProvider? = null

init {
prepareIntegrityTokenProvider()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the "warm up" for Google Play Integrity API, as we often call it in internal discussion and docs.

}

/**
* (Re-)prepares the Google Play Integrity Token Provider. Calling this
* prior to requesting the Integrity Token via
* [createAppAttestation] reduces the latency of the request.
*/
@VisibleForTesting
internal fun prepareIntegrityTokenProvider() = integrityManager.prepareIntegrityToken(
PrepareIntegrityTokenRequest.builder()
.setCloudProjectNumber(googleCloudProjectId)
.build()
).addOnSuccessListener(
::onPrepareIntegrityTokenProviderSuccess
).addOnFailureListener(
::onPrepareIntegrityTokenProviderFailure
)

/**
* A success callback used by [prepareIntegrityTokenProvider].
* @param tokenProvider The Google Play API Integrity Token Provider
*/
@VisibleForTesting
internal fun onPrepareIntegrityTokenProviderSuccess(tokenProvider: StandardIntegrityTokenProvider) {
integrityTokenProvider = tokenProvider
}

/**
* A failure callback for [prepareIntegrityTokenProvider].
* @param exception The exception provided by Google Play Integrity API
*/
@VisibleForTesting
internal fun onPrepareIntegrityTokenProviderFailure(exception: Exception) {
w(javaClass.name, "Failed to prepare Google Play Integrity Token Provider: '${exception.message}'. App Attestation will be disabled.")
}

/**
* Creates a Salesforce App Attestation External Client App (ECA) Plugin
* "attestation". First a Salesforce Mobile App Attestation "Challenge" is
* requested for the device id. Then, a Google Play Integrity API Token is
* fetched using the "Challenge" as the Request Hash. The resulting token is
* encoded into a value usable as the "attestation" parameter in the
* Salesforce OAuth authorization request.
*
* This method is not intended for public use outside of Salesforce Mobile
* SDK.
*
* TODO: Make this Kotlin-internal once it is no longer referenced by Java. ECJ20260420
*
* @param appAttestationChallenge The Salesforce Mobile App Attestation
* External Client App (ECA) Plug-In "Challenge" to use
* @param integrityTokenProvider The Google Play App Integrity API Integrity
* Token Provider. This parameter is intended for testing purposes only
* @return The "attestation" value usable in Salesforce OAuth authorization
* and token refresh requests or null if the value cannot be created
*/
suspend fun createAppAttestation(
appAttestationChallenge: String,
integrityTokenProvider: StandardIntegrityTokenProvider? = this.integrityTokenProvider,
): String? {
// Guard to ensure the Google Play Integrity API Integrity Provider was asynchronously resolved or do so synchronously now.
val integrityTokenProviderResolved = integrityTokenProvider ?: prepareIntegrityTokenProvider().await()

// Fetch the Challenge from Salesforce Mobile App Attestation.
val salesforceAppAttestationChallengeHashByteArray = MessageDigest.getInstance("SHA-256")
.digest(appAttestationChallenge.toByteArray(UTF_8))
val salesforceAppAttestationChallengeHashHexString = salesforceAppAttestationChallengeHashByteArray.joinToString("") { "%02x".format(it) }

// Request the Google Play Integrity Token.
val integrityTokenResponse = integrityTokenProviderResolved.request(
StandardIntegrityTokenRequest.builder()
.setRequestHash(salesforceAppAttestationChallengeHashHexString)
.build()
)

/*
* Wait for the Google Play Integrity API response and return the
* Base64-encoded Salesforce OAuth authorization attestation parameter
* JSON. This may block the calling thread if the Google Play Integrity
* API introduces latency, though latency is expected to minimal as the
* API will have been prepared earlier in most scenarios.
*/
return runCatching {
integrityTokenResponse.await()

// When the Google Play Integrity API response is received, return the Base64-encoded Salesforce OAuth authorization attestation parameter JSON.
OAuthAuthorizationAttestation(
attestationId = deviceId,
attestationData = Base64.getEncoder().encodeToString(
integrityTokenResponse.getResult().token().encodeToByteArray()
)
).toBase64String()
}.getOrElse { e ->
// If the Google Play Integrity API failed due to the Integrity Token Provider being expired, re-prepare it once for an inline retry.
if ((e as? IntegrityServiceException)?.errorCode == INTEGRITY_TOKEN_PROVIDER_INVALID) {
createAppAttestation(
appAttestationChallenge = appAttestationChallenge,
integrityTokenProvider = null
)
} else {
null
}
}
}

/**
* A blocking Java-callable wrapper for [createAppAttestation]
*
* This method is not intended for public use outside of Salesforce Mobile
* SDK.
*
* TODO: Remove method when no longer referenced by Java. ECJ20260420
* @param appAttestationChallenge The Salesforce Mobile App Attestation
* External Client App (ECA) Plug-In "Challenge" to use
*/
fun createAppAttestationBlocking(appAttestationChallenge: String) = runBlocking {
createAppAttestation(appAttestationChallenge)
}

/**
* Fetches a new "Challenge" from the Salesforce App Attestation External
* Client App (ECA) Plug-In.
*
* This method is not intended for public use outside of Salesforce Mobile
* SDK.
*
* TODO: Make this Kotlin-internal once it is no longer referenced by Java. ECJ20260420
*
* @return The Salesforce App Attestation ECA Plug-In's "Challenge"
*/
fun fetchMobileAppAttestationChallenge(): String {
// Create the Salesforce App Attestation Challenge API client and fetch a new challenge.
val appAttestationChallengeApiClient = AppAttestationChallengeApiClient(
apiHostName = apiHostName,
restClient = restClient
)
return appAttestationChallengeApiClient.fetchChallenge(
attestationId = deviceId,
remoteConsumerKey = remoteAccessConsumerKey
)
}
}

/**
* A Salesforce OAuth 2.0 authorization "attestation" parameter.
* @param attestationId The attestation id used when creating the Salesforce
* Mobile App Attestation API Challenge. This is intended to be the
* Salesforce Mobile SDK device id
* @param attestationData The token provided by the Google Play Integrity API
*/
@Serializable
internal data class OAuthAuthorizationAttestation(
val attestationId: String,
val attestationData: String,
) {

/**
* Returns a Base64-encoded JSON representation of this object
*/
fun toBase64String(): String? = Base64.getEncoder().encodeToString(Json.encodeToString(serializer(), this).encodeToByteArray())
}

Loading
Loading