-
Notifications
You must be signed in to change notification settings - Fork 615
Add Play Integrity support to App Check #3658
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf98935
Initial App Check <> Play Integrity commit (#3607)
rosalyntan 295c8f5
Implement Play Integrity token exchange. (#3613)
rosalyntan c32c180
Implement Play Integrity attestation flow. (#3618)
rosalyntan ae07804
Update test app. (#3655)
rosalyntan b9e0c36
Merge branch 'master' into play-integrity
rosalyntan ee1af7a
Update version in `gradle.properties`.
rosalyntan 2c65bca
Fix tests broken after merge.
rosalyntan 0953557
Address review comments.
rosalyntan 0fed01d
Address review comment.
rosalyntan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
appcheck/firebase-appcheck-playintegrity/firebase-appcheck-playintegrity.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
plugins { | ||
id 'firebase-library' | ||
} | ||
|
||
firebaseLibrary { | ||
publishSources = true | ||
} | ||
|
||
android { | ||
adbOptions { | ||
timeOutInMs 60 * 1000 | ||
} | ||
|
||
compileSdkVersion project.targetSdkVersion | ||
defaultConfig { | ||
targetSdkVersion project.targetSdkVersion | ||
minSdkVersion project.minSdkVersion | ||
versionName version | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
testOptions.unitTests.includeAndroidResources = false | ||
} | ||
|
||
dependencies { | ||
implementation project(':firebase-common') | ||
implementation project(':firebase-components') | ||
implementation project(':appcheck:firebase-appcheck') | ||
implementation 'com.google.android.gms:play-services-base:18.0.1' | ||
implementation 'com.google.android.gms:play-services-tasks:18.0.1' | ||
implementation 'com.google.android.play:integrity:1.0.1' | ||
|
||
testImplementation 'junit:junit:4.13.2' | ||
testImplementation 'org.mockito:mockito-core:3.4.6' | ||
testImplementation "com.google.truth:truth:$googleTruthVersion" | ||
testImplementation "org.robolectric:robolectric:$robolectricVersion" | ||
testImplementation 'androidx.test:core:1.4.0' | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version=16.0.0 |
20 changes: 20 additions & 0 deletions
20
appcheck/firebase-appcheck-playintegrity/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright 2022 Google LLC --> | ||
<!-- --> | ||
<!-- Licensed under the Apache License, Version 2.0 (the "License"); --> | ||
<!-- you may not use this file except in compliance with the License. --> | ||
<!-- You may obtain a copy of the License at --> | ||
<!-- --> | ||
<!-- http://www.apache.org/licenses/LICENSE-2.0 --> | ||
<!-- --> | ||
<!-- Unless required by applicable law or agreed to in writing, software --> | ||
<!-- distributed under the License is distributed on an "AS IS" BASIS, --> | ||
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --> | ||
<!-- See the License for the specific language governing permissions and --> | ||
<!-- limitations under the License. --> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.google.firebase.appcheck.playintegrity"> | ||
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds--> | ||
<!--<uses-sdk android:minSdkVersion="16"/>--> | ||
</manifest> |
46 changes: 46 additions & 0 deletions
46
...java/com/google/firebase/appcheck/playintegrity/PlayIntegrityAppCheckProviderFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.appcheck.playintegrity; | ||
|
||
import androidx.annotation.NonNull; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.appcheck.AppCheckProvider; | ||
import com.google.firebase.appcheck.AppCheckProviderFactory; | ||
import com.google.firebase.appcheck.playintegrity.internal.PlayIntegrityAppCheckProvider; | ||
|
||
/** | ||
* Implementation of an {@link AppCheckProviderFactory} that builds {@link | ||
* PlayIntegrityAppCheckProvider}s. This is the default implementation. | ||
*/ | ||
public class PlayIntegrityAppCheckProviderFactory implements AppCheckProviderFactory { | ||
|
||
private static final PlayIntegrityAppCheckProviderFactory instance = | ||
new PlayIntegrityAppCheckProviderFactory(); | ||
|
||
/** | ||
* Gets an instance of this class for installation into a {@link | ||
* com.google.firebase.appcheck.FirebaseAppCheck} instance. | ||
*/ | ||
@NonNull | ||
public static PlayIntegrityAppCheckProviderFactory getInstance() { | ||
return instance; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) { | ||
return new PlayIntegrityAppCheckProvider(firebaseApp); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...om/google/firebase/appcheck/playintegrity/internal/ExchangePlayIntegrityTokenRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.appcheck.playintegrity.internal; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* Client-side model of the ExchangePlayIntegrityTokenRequest payload from the Firebase App Check | ||
* Token Exchange API. | ||
*/ | ||
class ExchangePlayIntegrityTokenRequest { | ||
|
||
@VisibleForTesting static final String PLAY_INTEGRITY_TOKEN_KEY = "playIntegrityToken"; | ||
|
||
private final String playIntegrityToken; | ||
|
||
public ExchangePlayIntegrityTokenRequest(@NonNull String playIntegrityToken) { | ||
this.playIntegrityToken = playIntegrityToken; | ||
} | ||
|
||
@NonNull | ||
public String toJsonString() throws JSONException { | ||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put(PLAY_INTEGRITY_TOKEN_KEY, playIntegrityToken); | ||
|
||
return jsonObject.toString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...oogle/firebase/appcheck/playintegrity/internal/GeneratePlayIntegrityChallengeRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.appcheck.playintegrity.internal; | ||
|
||
import androidx.annotation.NonNull; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* Client-side model of the GeneratePlayIntegrityChallengeRequest payload from the Firebase App | ||
* Check Token Exchange API. | ||
*/ | ||
class GeneratePlayIntegrityChallengeRequest { | ||
|
||
public GeneratePlayIntegrityChallengeRequest() {} | ||
|
||
@NonNull | ||
public String toJsonString() { | ||
JSONObject jsonObject = new JSONObject(); | ||
|
||
// GeneratePlayIntegrityChallenge takes an empty POST body since the app ID is in the URL. | ||
return jsonObject.toString(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ogle/firebase/appcheck/playintegrity/internal/GeneratePlayIntegrityChallengeResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.appcheck.playintegrity.internal; | ||
|
||
import static com.google.android.gms.common.internal.Preconditions.checkNotNull; | ||
import static com.google.android.gms.common.util.Strings.emptyToNull; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import com.google.firebase.FirebaseException; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* Client-side model of the GeneratePlayIntegrityChallengeResponse payload from the Firebase App | ||
* Check Token Exchange API. | ||
*/ | ||
class GeneratePlayIntegrityChallengeResponse { | ||
|
||
@VisibleForTesting static final String CHALLENGE_KEY = "challenge"; | ||
@VisibleForTesting static final String TIME_TO_LIVE_KEY = "ttl"; | ||
|
||
private String challenge; | ||
private String timeToLive; | ||
|
||
@NonNull | ||
public static GeneratePlayIntegrityChallengeResponse fromJsonString(@NonNull String jsonString) | ||
throws FirebaseException, JSONException { | ||
JSONObject jsonObject = new JSONObject(jsonString); | ||
String challenge = emptyToNull(jsonObject.optString(CHALLENGE_KEY)); | ||
String timeToLive = emptyToNull(jsonObject.optString(TIME_TO_LIVE_KEY)); | ||
rosalyntan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (challenge == null || timeToLive == null) { | ||
throw new FirebaseException("Unexpected server response."); | ||
} | ||
return new GeneratePlayIntegrityChallengeResponse(challenge, timeToLive); | ||
} | ||
|
||
private GeneratePlayIntegrityChallengeResponse( | ||
@NonNull String challenge, @NonNull String timeToLive) { | ||
checkNotNull(challenge); | ||
checkNotNull(timeToLive); | ||
this.challenge = challenge; | ||
this.timeToLive = timeToLive; | ||
} | ||
|
||
@NonNull | ||
public String getChallenge() { | ||
return challenge; | ||
} | ||
|
||
@NonNull | ||
public String getTimeToLive() { | ||
return timeToLive; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...va/com/google/firebase/appcheck/playintegrity/internal/PlayIntegrityAppCheckProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.appcheck.playintegrity.internal; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.android.gms.tasks.Tasks; | ||
import com.google.android.play.core.integrity.IntegrityManager; | ||
import com.google.android.play.core.integrity.IntegrityManagerFactory; | ||
import com.google.android.play.core.integrity.IntegrityTokenRequest; | ||
import com.google.android.play.core.integrity.IntegrityTokenResponse; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.appcheck.AppCheckProvider; | ||
import com.google.firebase.appcheck.AppCheckToken; | ||
import com.google.firebase.appcheck.internal.DefaultAppCheckToken; | ||
import com.google.firebase.appcheck.internal.NetworkClient; | ||
import com.google.firebase.appcheck.internal.RetryManager; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class PlayIntegrityAppCheckProvider implements AppCheckProvider { | ||
|
||
private static final String UTF_8 = "UTF-8"; | ||
|
||
private final String projectNumber; | ||
private final IntegrityManager integrityManager; | ||
private final NetworkClient networkClient; | ||
private final ExecutorService backgroundExecutor; | ||
private final RetryManager retryManager; | ||
|
||
public PlayIntegrityAppCheckProvider(@NonNull FirebaseApp firebaseApp) { | ||
this( | ||
firebaseApp.getOptions().getGcmSenderId(), | ||
IntegrityManagerFactory.create(firebaseApp.getApplicationContext()), | ||
rosalyntan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new NetworkClient(firebaseApp), | ||
Executors.newCachedThreadPool(), | ||
new RetryManager()); | ||
} | ||
|
||
@VisibleForTesting | ||
PlayIntegrityAppCheckProvider( | ||
@NonNull String projectNumber, | ||
@NonNull IntegrityManager integrityManager, | ||
@NonNull NetworkClient networkClient, | ||
@NonNull ExecutorService backgroundExecutor, | ||
@NonNull RetryManager retryManager) { | ||
this.projectNumber = projectNumber; | ||
this.integrityManager = integrityManager; | ||
this.networkClient = networkClient; | ||
this.backgroundExecutor = backgroundExecutor; | ||
this.retryManager = retryManager; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Task<AppCheckToken> getToken() { | ||
return getPlayIntegrityAttestation() | ||
.onSuccessTask( | ||
integrityTokenResponse -> { | ||
ExchangePlayIntegrityTokenRequest request = | ||
new ExchangePlayIntegrityTokenRequest(integrityTokenResponse.token()); | ||
return Tasks.call( | ||
backgroundExecutor, | ||
() -> | ||
networkClient.exchangeAttestationForAppCheckToken( | ||
request.toJsonString().getBytes(UTF_8), | ||
NetworkClient.PLAY_INTEGRITY, | ||
retryManager)); | ||
}) | ||
.onSuccessTask( | ||
appCheckTokenResponse -> { | ||
return Tasks.forResult( | ||
DefaultAppCheckToken.constructFromAppCheckTokenResponse(appCheckTokenResponse)); | ||
}); | ||
} | ||
|
||
@NonNull | ||
private Task<IntegrityTokenResponse> getPlayIntegrityAttestation() { | ||
GeneratePlayIntegrityChallengeRequest generateChallengeRequest = | ||
new GeneratePlayIntegrityChallengeRequest(); | ||
Task<GeneratePlayIntegrityChallengeResponse> generateChallengeTask = | ||
Tasks.call( | ||
backgroundExecutor, | ||
() -> | ||
GeneratePlayIntegrityChallengeResponse.fromJsonString( | ||
networkClient.generatePlayIntegrityChallenge( | ||
generateChallengeRequest.toJsonString().getBytes(UTF_8), retryManager))); | ||
return generateChallengeTask.onSuccessTask( | ||
generatePlayIntegrityChallengeResponse -> { | ||
return integrityManager.requestIntegrityToken( | ||
IntegrityTokenRequest.builder() | ||
.setCloudProjectNumber(Long.parseLong(projectNumber)) | ||
.setNonce(generatePlayIntegrityChallengeResponse.getChallenge()) | ||
.build()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.