Skip to content

Commit b57252b

Browse files
Add App Check snippets
1 parent 03cf10d commit b57252b

37 files changed

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

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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(@Header("X-Firebase-AppCheck") String appCheckToken);
21+
}
22+
23+
YourExampleBackendService yourExampleBackendService = new Retrofit.Builder()
24+
.baseUrl("https://yourbackend.example.com/yourApiEndpoint")
25+
.build()
26+
.create(YourExampleBackendService.class);
27+
28+
public void callApiExample() {
29+
FirebaseAppCheck.getInstance()
30+
.getAppCheckToken(false)
31+
.addOnSuccessListener(new OnSuccessListener<AppCheckToken>() {
32+
@Override
33+
public void onSuccess(@NonNull AppCheckToken tokenResponse) {
34+
String appCheckToken = tokenResponse.getToken();
35+
Call<List<String>> apiCall = yourExampleBackendService.exampleData(appCheckToken);
36+
// ...
37+
}
38+
});
39+
}
40+
}
41+
// [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/yourApiEndpoint")
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]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.google.firebase.example.appcheck.kotlin
2+
3+
import android.content.Context
4+
import com.google.android.gms.tasks.Task
5+
import com.google.android.gms.tasks.Tasks
6+
import com.google.firebase.FirebaseApp
7+
import com.google.firebase.appcheck.AppCheckProvider
8+
import com.google.firebase.appcheck.AppCheckProviderFactory
9+
import com.google.firebase.appcheck.AppCheckToken
10+
import com.google.firebase.appcheck.FirebaseAppCheck
11+
12+
class CustomProvider {
13+
// [START appcheck_custom_provider]
14+
class YourCustomAppCheckToken(
15+
private val token: String,
16+
private val expiration: Long
17+
) : AppCheckToken() {
18+
override fun getToken(): String = token
19+
override fun getExpireTimeMillis(): Long = expiration
20+
}
21+
22+
class YourCustomAppCheckProvider(firebaseApp: FirebaseApp) : AppCheckProvider {
23+
override fun getToken(): Task<AppCheckToken> {
24+
// Logic to exchange proof of authenticity for an App Check token and
25+
// expiration time.
26+
// [START_EXCLUDE]
27+
val expirationFromServer = 0L
28+
val tokenFromServer = "token"
29+
// [END_EXCLUDE]
30+
31+
// Refresh the token early to handle clock skew.
32+
val expMillis = expirationFromServer * 1000L - 60000L
33+
34+
// Create AppCheckToken object.
35+
val appCheckToken: AppCheckToken = YourCustomAppCheckToken(tokenFromServer, expMillis)
36+
return Tasks.forResult(appCheckToken)
37+
}
38+
}
39+
// [END appcheck_custom_provider]
40+
41+
// [START appcheck_custom_provider_factory]
42+
class YourCustomAppCheckProviderFactory : AppCheckProviderFactory {
43+
override fun create(firebaseApp: FirebaseApp): AppCheckProvider {
44+
// Create and return an AppCheckProvider object.
45+
return YourCustomAppCheckProvider(firebaseApp)
46+
}
47+
}
48+
// [END appcheck_custom_provider_factory]
49+
50+
private fun init(context: Context) {
51+
// [START appcheck_initialize_custom_provider]
52+
FirebaseApp.initializeApp(/*context=*/ context)
53+
val firebaseAppCheck = FirebaseAppCheck.getInstance()
54+
firebaseAppCheck.installAppCheckProviderFactory(
55+
YourCustomAppCheckProviderFactory()
56+
)
57+
// [END appcheck_initialize_custom_provider]
58+
}
59+
}

0 commit comments

Comments
 (0)