Skip to content

Commit f64dc05

Browse files
authored
Merge pull request firebase#312 from firebase/kc-appcheck
Add App Check snippets
2 parents 03cf10d + e7499ee commit f64dc05

39 files changed

+1016
-0
lines changed

appcheck/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

appcheck/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

appcheck/app/build.gradle

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'com.google.gms.google-services' // Google Services plugin
4+
5+
android {
6+
compileSdkVersion 30
7+
8+
defaultConfig {
9+
applicationId "com.google.firebase.example.appcheck"
10+
minSdkVersion 16
11+
targetSdkVersion 30
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_1_8
26+
targetCompatibility JavaVersion.VERSION_1_8
27+
}
28+
}
29+
30+
dependencies {
31+
implementation 'androidx.appcompat:appcompat:1.3.1'
32+
implementation 'com.google.android.material:material:1.4.0'
33+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
34+
35+
implementation 'com.google.firebase:firebase-appcheck:16.0.0-beta02'
36+
implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta02'
37+
implementation 'com.google.firebase:firebase-appcheck-safetynet:16.0.0-beta02'
38+
39+
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
40+
41+
implementation "androidx.core:core-ktx:1.6.0"
42+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
43+
}

appcheck/app/google-services.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"project_info": {
3+
"project_number": "948708767782",
4+
"project_id": "app-check-snippets",
5+
"storage_bucket": "app-check-snippets.appspot.com"
6+
},
7+
"client": [
8+
{
9+
"client_info": {
10+
"mobilesdk_app_id": "1:948708767782:android:2e694ab930ef4c3b4c4ee7",
11+
"android_client_info": {
12+
"package_name": "com.google.firebase.example.appcheck"
13+
}
14+
},
15+
"oauth_client": [
16+
{
17+
"client_id": "948708767782-2e42ddfu4rcsbe5nv9qat94q6v9c12bf.apps.googleusercontent.com",
18+
"client_type": 3
19+
}
20+
],
21+
"api_key": [
22+
{
23+
"current_key": "AIzaSyABXlodP5O5vv9d0nObDcgNHSmWpcA5wLc"
24+
}
25+
],
26+
"services": {
27+
"appinvite_service": {
28+
"other_platform_oauth_client": [
29+
{
30+
"client_id": "948708767782-2e42ddfu4rcsbe5nv9qat94q6v9c12bf.apps.googleusercontent.com",
31+
"client_type": 3
32+
}
33+
]
34+
}
35+
}
36+
}
37+
],
38+
"configuration_version": "1"
39+
}

appcheck/app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.google.firebase.example.appcheck">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.AppCheckSnippets">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.google.firebase.example.appcheck;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.google.android.gms.tasks.OnSuccessListener;
6+
import com.google.firebase.appcheck.AppCheckToken;
7+
import com.google.firebase.appcheck.FirebaseAppCheck;
8+
9+
import java.util.List;
10+
11+
import retrofit2.Call;
12+
import retrofit2.Retrofit;
13+
import retrofit2.http.GET;
14+
import retrofit2.http.Header;
15+
16+
// [START appcheck_custom_backend]
17+
public class ApiWithAppCheckExample {
18+
private interface YourExampleBackendService {
19+
@GET("yourExampleEndpoint")
20+
Call<List<String>> exampleData(
21+
@Header("X-Firebase-AppCheck") String appCheckToken);
22+
}
23+
24+
YourExampleBackendService yourExampleBackendService = new Retrofit.Builder()
25+
.baseUrl("https://yourbackend.example.com/")
26+
.build()
27+
.create(YourExampleBackendService.class);
28+
29+
public void callApiExample() {
30+
FirebaseAppCheck.getInstance()
31+
.getAppCheckToken(false)
32+
.addOnSuccessListener(new OnSuccessListener<AppCheckToken>() {
33+
@Override
34+
public void onSuccess(@NonNull AppCheckToken tokenResponse) {
35+
String appCheckToken = tokenResponse.getToken();
36+
Call<List<String>> apiCall =
37+
yourExampleBackendService.exampleData(appCheckToken);
38+
// ...
39+
}
40+
});
41+
}
42+
}
43+
// [END appcheck_custom_backend]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.google.firebase.example.appcheck;
2+
3+
import android.content.Context;
4+
5+
import androidx.annotation.NonNull;
6+
7+
import com.google.android.gms.tasks.Task;
8+
import com.google.android.gms.tasks.Tasks;
9+
import com.google.firebase.FirebaseApp;
10+
import com.google.firebase.appcheck.AppCheckProvider;
11+
import com.google.firebase.appcheck.AppCheckProviderFactory;
12+
import com.google.firebase.appcheck.AppCheckToken;
13+
import com.google.firebase.appcheck.FirebaseAppCheck;
14+
15+
public class CustomProvider {
16+
// [START appcheck_custom_provider]
17+
public class YourCustomAppCheckToken extends AppCheckToken {
18+
private String token;
19+
private long expiration;
20+
21+
YourCustomAppCheckToken(String token, long expiration) {
22+
this.token = token;
23+
this.expiration = expiration;
24+
}
25+
26+
@NonNull
27+
@Override
28+
public String getToken() {
29+
return token;
30+
}
31+
32+
@Override
33+
public long getExpireTimeMillis() {
34+
return expiration;
35+
}
36+
}
37+
38+
public class YourCustomAppCheckProvider implements AppCheckProvider {
39+
public YourCustomAppCheckProvider(FirebaseApp firebaseApp) {
40+
// ...
41+
}
42+
43+
@NonNull
44+
@Override
45+
public Task<AppCheckToken> getToken() {
46+
// Logic to exchange proof of authenticity for an App Check token and
47+
// expiration time.
48+
// [START_EXCLUDE]
49+
long expirationFromServer = 0L;
50+
String tokenFromServer = "token";
51+
// [END_EXCLUDE]
52+
53+
// Refresh the token early to handle clock skew.
54+
long expMillis = expirationFromServer * 1000L - 60000L;
55+
56+
// Create AppCheckToken object.
57+
AppCheckToken appCheckToken =
58+
new YourCustomAppCheckToken(tokenFromServer, expMillis);
59+
60+
return Tasks.forResult(appCheckToken);
61+
}
62+
}
63+
// [END appcheck_custom_provider]
64+
65+
// [START appcheck_custom_provider_factory]
66+
public class YourCustomAppCheckProviderFactory implements AppCheckProviderFactory {
67+
@NonNull
68+
@Override
69+
public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
70+
// Create and return an AppCheckProvider object.
71+
return new YourCustomAppCheckProvider(firebaseApp);
72+
}
73+
}
74+
// [END appcheck_custom_provider_factory]
75+
76+
private void init(Context context) {
77+
// [START appcheck_initialize_custom_provider]
78+
FirebaseApp.initializeApp(/*context=*/ context);
79+
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
80+
firebaseAppCheck.installAppCheckProviderFactory(
81+
new YourCustomAppCheckProviderFactory());
82+
// [END appcheck_initialize_custom_provider]
83+
}
84+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.google.firebase.example.appcheck;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.os.Bundle;
6+
7+
import com.google.firebase.FirebaseApp;
8+
import com.google.firebase.appcheck.FirebaseAppCheck;
9+
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory;
10+
import com.google.firebase.appcheck.safetynet.SafetyNetAppCheckProviderFactory;
11+
12+
public class MainActivity extends AppCompatActivity {
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_main);
18+
}
19+
20+
private void init() {
21+
// [START appcheck_initialize]
22+
FirebaseApp.initializeApp(/*context=*/ this);
23+
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
24+
firebaseAppCheck.installAppCheckProviderFactory(
25+
SafetyNetAppCheckProviderFactory.getInstance());
26+
// [END appcheck_initialize]
27+
}
28+
29+
private void initDebug() {
30+
// [START appcheck_initialize_debug]
31+
FirebaseApp.initializeApp(/*context=*/ this);
32+
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
33+
firebaseAppCheck.installAppCheckProviderFactory(
34+
DebugAppCheckProviderFactory.getInstance());
35+
// [END appcheck_initialize_debug]
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.google.firebase.example.appcheck.kotlin
2+
3+
import com.google.firebase.appcheck.FirebaseAppCheck
4+
import retrofit2.Call
5+
import retrofit2.Retrofit
6+
import retrofit2.http.GET
7+
import retrofit2.http.Header
8+
9+
10+
// [START appcheck_custom_backend]
11+
class ApiWithAppCheckExample {
12+
interface YourExampleBackendService {
13+
@GET("yourExampleEndpoint")
14+
fun exampleData(
15+
@Header("X-Firebase-AppCheck") appCheckToken: String
16+
): Call<List<String>>
17+
}
18+
19+
var yourExampleBackendService: YourExampleBackendService = Retrofit.Builder()
20+
.baseUrl("https://yourbackend.example.com/")
21+
.build()
22+
.create(YourExampleBackendService::class.java)
23+
24+
fun callApiExample() {
25+
FirebaseAppCheck.getInstance()
26+
.getAppCheckToken(false)
27+
.addOnSuccessListener { tokenResponse ->
28+
val appCheckToken = tokenResponse.token
29+
val apiCall = yourExampleBackendService.exampleData(appCheckToken)
30+
// ...
31+
}
32+
}
33+
}
34+
// [END appcheck_custom_backend]

0 commit comments

Comments
 (0)