Skip to content

feat: implement Firebase App Check KTX #3893

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
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions appcheck/firebase-appcheck/ktx/api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Signature format: 2.0
package com.google.firebase.appcheck.ktx {

public final class FirebaseAppCheckKt {
ctor public FirebaseAppCheckKt();
method @NonNull public static com.google.firebase.appcheck.FirebaseAppCheck appCheck(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
method @NonNull public static operator String component1(@NonNull com.google.firebase.appcheck.AppCheckToken);
method public static operator long component2(@NonNull com.google.firebase.appcheck.AppCheckToken);
method @NonNull public static com.google.firebase.appcheck.FirebaseAppCheck getAppCheck(@NonNull com.google.firebase.ktx.Firebase);
}

}

1 change: 1 addition & 0 deletions appcheck/firebase-appcheck/ktx/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
android.enableUnitTestBinaryResources=true
72 changes: 72 additions & 0 deletions appcheck/firebase-appcheck/ktx/ktx.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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'
id 'kotlin-android'
}

firebaseLibrary {
releaseWith project(':appcheck:firebase-appcheck')
testLab.enabled = true
publishJavadoc = true
publishSources = true
}

android {
compileSdkVersion project.targetSdkVersion
defaultConfig {
minSdkVersion 16
multiDexEnabled true
targetSdkVersion project.targetSdkVersion
versionName version
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java {
srcDir 'src/test/kotlin'
}
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
testOptions.unitTests.includeAndroidResources = true
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

implementation project(':firebase-common')
implementation project(':firebase-components')
implementation project(':firebase-common:ktx')
implementation 'androidx.annotation:annotation:1.1.0'
implementation project(path: ':appcheck:firebase-appcheck')
implementation project(path: ':appcheck:firebase-appcheck-interop')

testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation 'junit:junit:4.12'
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'org.mockito:mockito-core:2.25.0'

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
}
13 changes: 13 additions & 0 deletions appcheck/firebase-appcheck/ktx/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.appcheck.ktx">
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
<!--<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />-->
<uses-permission android:name="android.permission.INTERNET"/>
<application>
<uses-library android:name="android.test.runner" />
</application>

<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.google.firebase.appcheck.ktx" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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.ktx

import androidx.test.core.app.ApplicationProvider
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.google.common.truth.Truth.assertThat
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.appcheck.AppCheckToken
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.app
import com.google.firebase.ktx.initialize
import com.google.firebase.platforminfo.UserAgentPublisher
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

const val APP_ID = "APP_ID"
const val API_KEY = "API_KEY"

const val EXISTING_APP = "existing"

@RunWith(AndroidJUnit4ClassRunner::class)
abstract class BaseTestCase {
@Before
fun setUp() {
Firebase.initialize(
ApplicationProvider.getApplicationContext(),
FirebaseOptions.Builder()
.setApplicationId(APP_ID)
.setApiKey(API_KEY)
.setProjectId("123")
.build()
)

Firebase.initialize(
ApplicationProvider.getApplicationContext(),
FirebaseOptions.Builder()
.setApplicationId(APP_ID)
.setApiKey(API_KEY)
.setProjectId("123")
.build(),
EXISTING_APP
)
}

@After
fun cleanUp() {
FirebaseApp.clearInstancesForTest()
}
}

@RunWith(AndroidJUnit4ClassRunner::class)
class FirebaseAppCheckTests : BaseTestCase() {
@Test
fun appCheck_default_callsDefaultGetInstance() {
assertThat(Firebase.appCheck).isSameInstanceAs(FirebaseAppCheck.getInstance())
}

@Test
fun appCheck_with_custom_firebaseapp_calls_GetInstance() {
val app = Firebase.app(EXISTING_APP)
assertThat(Firebase.appCheck(app))
.isSameInstanceAs(FirebaseAppCheck.getInstance(app))
}

@Test
fun appCheckToken_destructuring_declaration_works() {
val mockAppCheckToken = object : AppCheckToken() {
override fun getToken(): String = "randomToken"

override fun getExpireTimeMillis(): Long = 23121997
}

val (token, expiration) = mockAppCheckToken

assertThat(token).isEqualTo(mockAppCheckToken.token)
assertThat(expiration).isEqualTo(mockAppCheckToken.expireTimeMillis)
}
}

internal const val LIBRARY_NAME: String = "fire-app-check-ktx"

@RunWith(AndroidJUnit4ClassRunner::class)
class LibraryVersionTest : BaseTestCase() {
@Test
fun libraryRegistrationAtRuntime() {
val publisher = Firebase.app.get(UserAgentPublisher::class.java)
assertThat(publisher.userAgent).contains(LIBRARY_NAME)
}
}
25 changes: 25 additions & 0 deletions appcheck/firebase-appcheck/ktx/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- 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.ktx">
<uses-sdk android:minSdkVersion="16"/>

<application>
<service android:name="com.google.firebase.components.ComponentDiscoveryService" android:exported="false">
<meta-data android:name="com.google.firebase.components:com.google.firebase.appcheck.ktx.FirebaseAppCheckKtxRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.ktx

import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.AppCheckToken
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.ktx.Firebase

/** Returns the [FirebaseAppCheck] instance of the default [FirebaseApp]. */
val Firebase.appCheck: FirebaseAppCheck
get() = FirebaseAppCheck.getInstance()

/** Returns the [FirebaseAppCheck] instance of a given [FirebaseApp]. */
fun Firebase.appCheck(app: FirebaseApp) = FirebaseAppCheck.getInstance(app)

/**
* Destructuring declaration for [AppCheckToken] to provide token.
*
* @return the token of the [AppCheckToken]
*/
operator fun AppCheckToken.component1() = token

/**
* Destructuring declaration for [AppCheckToken] to provide expireTimeMillis.
*
* @return the expireTimeMillis of the [AppCheckToken]
*/
operator fun AppCheckToken.component2() = expireTimeMillis
1 change: 1 addition & 0 deletions subprojects.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ appcheck:firebase-appcheck-interop
appcheck:firebase-appcheck-playintegrity
appcheck:firebase-appcheck-safetynet
appcheck:firebase-appcheck
appcheck:firebase-appcheck:ktx

firebase-abt
firebase-annotations
Expand Down