Skip to content

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 9 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
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'
}
1 change: 1 addition & 0 deletions appcheck/firebase-appcheck-playintegrity/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=16.0.0-beta01
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>
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);
}
}
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.
*/
public 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();
}
}
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.
*/
public 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import org.json.JSONException;
import org.json.JSONObject;

/**
* Client-side model of the GeneratePlayIntegrityChallengeResponse payload from the Firebase App
* Check Token Exchange API.
*/
public 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 JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
String challenge = jsonObject.optString(CHALLENGE_KEY, null);
String timeToLive = jsonObject.optString(TIME_TO_LIVE_KEY, null);
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;
}
}
Loading