-
Notifications
You must be signed in to change notification settings - Fork 393
@W-21933885: [MSDK Android] App Attestation Implementation #2868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 47 commits
79e735e
e912590
00a2924
9c62dbe
d1376e5
e7f94f6
763515c
91e6079
62660cf
ba416fd
4a40216
2d2ca63
80771f6
7842f19
3ebedd4
5219194
09651ad
1ab0d06
44849dc
fb5c845
86377f5
a2a451f
0b158b5
4076ab2
b0b7370
e374d58
b1e5bad
d488ff0
1325829
1ebcfe2
9bf16d1
e3398ad
6e06e9e
3a0e417
91a434c
466b058
5845735
9a2aca8
5035087
b8f1790
7e5c233
f984d45
1304062
ab2af33
4029772
f3fad52
48f98cc
484e620
b6c4354
86f4b77
0f15179
9ddfa9e
aeb7405
e9d4890
6a36d44
6cdb0d1
ffce923
1b994fb
e9b32a4
97228a7
f93c9e5
8975f32
732a127
9a39ef0
7131666
15a9b04
d2998a3
9994387
29beb71
2c44b99
966c13a
c724a1a
2ef4eca
3673200
f674131
a44d1f0
30d3ca8
ecdd9d2
e2070f0
3e822b6
98efcc6
d154ec6
55d370f
89400d3
8a60818
261f874
63172a1
6946375
2ba67fb
5f968fa
d526e06
06c3719
beb685d
8d48205
da8bce9
76c91d8
b1e380e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' }} | ||
| run: | | ||
| ./install.sh | ||
| echo $TEST_CREDENTIALS > ./shared/test/test_credentials.json | ||
|
|
@@ -109,7 +113,6 @@ jobs: | |
|
|
||
| if $IS_PR ; then | ||
| LEVELS_TO_TEST=$PR_API_VERSION | ||
| RETRIES=1 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brandonpage, a little later you'll see a fix in the |
||
| fi | ||
|
|
||
| # Build test-targets-for-shard arguments from config file | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ dependencies { | |||
| api("androidx.browser:browser:1.8.0") // Update requires API 36 compileSdk | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| api("androidx.work:work-runtime-ktx:2.10.3") | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
|
|
||||
| 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") | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
| 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( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find!