Skip to content

Commit 7c277af

Browse files
thatfiredevehsannasrlazolfkelloggwu-hui
authored
feat: implement Firebase App Check KTX (#3893)
* feat: implement Firebase App Check KTX * File must end with a newline (\n) * Use unmodifiableList for CompositeFilters. (#3881) * Use ImmutableList (rather than List) for CompositeFilters. * use unmodifiableList. * Update the query protos. (#3880) * Clean up FirebaseApp instances before setting up a new one. (#3903) This should fix errors seen in CI. * Fix reports endpoint URL (#3899) * Update name of file in CPP repo. (#3901) To match the change done in the IOS repo firebase/firebase-ios-sdk#10009 * Ensure Client exists (#3900) * Fix 238932261 * Change log Co-authored-by: Ehsan <[email protected]> Co-authored-by: Rodrigo Lazo <[email protected]> Co-authored-by: Lee Kellogg <[email protected]> Co-authored-by: wu-hui <[email protected]>
1 parent fe17099 commit 7c277af

File tree

8 files changed

+272
-0
lines changed

8 files changed

+272
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Signature format: 2.0
2+
package com.google.firebase.appcheck.ktx {
3+
4+
public final class FirebaseAppCheckKt {
5+
ctor public FirebaseAppCheckKt();
6+
method @NonNull public static com.google.firebase.appcheck.FirebaseAppCheck appCheck(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
7+
method @NonNull public static operator String component1(@NonNull com.google.firebase.appcheck.AppCheckToken);
8+
method public static operator long component2(@NonNull com.google.firebase.appcheck.AppCheckToken);
9+
method @NonNull public static com.google.firebase.appcheck.FirebaseAppCheck getAppCheck(@NonNull com.google.firebase.ktx.Firebase);
10+
}
11+
12+
}
13+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
android.enableUnitTestBinaryResources=true
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
id 'firebase-library'
17+
id 'kotlin-android'
18+
}
19+
20+
firebaseLibrary {
21+
releaseWith project(':appcheck:firebase-appcheck')
22+
testLab.enabled = true
23+
publishJavadoc = true
24+
publishSources = true
25+
}
26+
27+
android {
28+
compileSdkVersion project.targetSdkVersion
29+
defaultConfig {
30+
minSdkVersion 16
31+
multiDexEnabled true
32+
targetSdkVersion project.targetSdkVersion
33+
versionName version
34+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
35+
}
36+
sourceSets {
37+
main.java.srcDirs += 'src/main/kotlin'
38+
test.java {
39+
srcDir 'src/test/kotlin'
40+
}
41+
androidTest.java.srcDirs += 'src/androidTest/kotlin'
42+
}
43+
compileOptions {
44+
sourceCompatibility JavaVersion.VERSION_1_8
45+
targetCompatibility JavaVersion.VERSION_1_8
46+
}
47+
kotlinOptions {
48+
jvmTarget = '1.8'
49+
}
50+
testOptions.unitTests.includeAndroidResources = true
51+
}
52+
53+
dependencies {
54+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
55+
56+
implementation project(':firebase-common')
57+
implementation project(':firebase-components')
58+
implementation project(':firebase-common:ktx')
59+
implementation 'androidx.annotation:annotation:1.1.0'
60+
implementation project(path: ':appcheck:firebase-appcheck')
61+
implementation project(path: ':appcheck:firebase-appcheck-interop')
62+
63+
testImplementation "org.robolectric:robolectric:$robolectricVersion"
64+
testImplementation 'junit:junit:4.12'
65+
testImplementation "com.google.truth:truth:$googleTruthVersion"
66+
testImplementation 'org.mockito:mockito-core:2.25.0'
67+
68+
androidTestImplementation 'junit:junit:4.12'
69+
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
70+
androidTestImplementation 'androidx.test:runner:1.2.0'
71+
androidTestImplementation 'androidx.test:core:1.2.0'
72+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.google.firebase.appcheck.ktx">
3+
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
4+
<!--<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />-->
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<application>
7+
<uses-library android:name="android.test.runner" />
8+
</application>
9+
10+
<instrumentation
11+
android:name="androidx.test.runner.AndroidJUnitRunner"
12+
android:targetPackage="com.google.firebase.appcheck.ktx" />
13+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.appcheck.ktx
16+
17+
import androidx.test.core.app.ApplicationProvider
18+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
19+
import com.google.common.truth.Truth.assertThat
20+
import com.google.firebase.FirebaseApp
21+
import com.google.firebase.FirebaseOptions
22+
import com.google.firebase.appcheck.AppCheckToken
23+
import com.google.firebase.appcheck.FirebaseAppCheck
24+
import com.google.firebase.ktx.Firebase
25+
import com.google.firebase.ktx.app
26+
import com.google.firebase.ktx.initialize
27+
import com.google.firebase.platforminfo.UserAgentPublisher
28+
import org.junit.After
29+
import org.junit.Before
30+
import org.junit.Test
31+
import org.junit.runner.RunWith
32+
33+
const val APP_ID = "APP_ID"
34+
const val API_KEY = "API_KEY"
35+
36+
const val EXISTING_APP = "existing"
37+
38+
@RunWith(AndroidJUnit4ClassRunner::class)
39+
abstract class BaseTestCase {
40+
@Before
41+
fun setUp() {
42+
Firebase.initialize(
43+
ApplicationProvider.getApplicationContext(),
44+
FirebaseOptions.Builder()
45+
.setApplicationId(APP_ID)
46+
.setApiKey(API_KEY)
47+
.setProjectId("123")
48+
.build()
49+
)
50+
51+
Firebase.initialize(
52+
ApplicationProvider.getApplicationContext(),
53+
FirebaseOptions.Builder()
54+
.setApplicationId(APP_ID)
55+
.setApiKey(API_KEY)
56+
.setProjectId("123")
57+
.build(),
58+
EXISTING_APP
59+
)
60+
}
61+
62+
@After
63+
fun cleanUp() {
64+
FirebaseApp.clearInstancesForTest()
65+
}
66+
}
67+
68+
@RunWith(AndroidJUnit4ClassRunner::class)
69+
class FirebaseAppCheckTests : BaseTestCase() {
70+
@Test
71+
fun appCheck_default_callsDefaultGetInstance() {
72+
assertThat(Firebase.appCheck).isSameInstanceAs(FirebaseAppCheck.getInstance())
73+
}
74+
75+
@Test
76+
fun appCheck_with_custom_firebaseapp_calls_GetInstance() {
77+
val app = Firebase.app(EXISTING_APP)
78+
assertThat(Firebase.appCheck(app))
79+
.isSameInstanceAs(FirebaseAppCheck.getInstance(app))
80+
}
81+
82+
@Test
83+
fun appCheckToken_destructuring_declaration_works() {
84+
val mockAppCheckToken = object : AppCheckToken() {
85+
override fun getToken(): String = "randomToken"
86+
87+
override fun getExpireTimeMillis(): Long = 23121997
88+
}
89+
90+
val (token, expiration) = mockAppCheckToken
91+
92+
assertThat(token).isEqualTo(mockAppCheckToken.token)
93+
assertThat(expiration).isEqualTo(mockAppCheckToken.expireTimeMillis)
94+
}
95+
}
96+
97+
internal const val LIBRARY_NAME: String = "fire-app-check-ktx"
98+
99+
@RunWith(AndroidJUnit4ClassRunner::class)
100+
class LibraryVersionTest : BaseTestCase() {
101+
@Test
102+
fun libraryRegistrationAtRuntime() {
103+
val publisher = Firebase.app.get(UserAgentPublisher::class.java)
104+
assertThat(publisher.userAgent).contains(LIBRARY_NAME)
105+
}
106+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!-- Copyright 2022 Google LLC -->
2+
<!-- -->
3+
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
4+
<!-- you may not use this file except in compliance with the License. -->
5+
<!-- You may obtain a copy of the License at -->
6+
<!-- -->
7+
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
8+
<!-- -->
9+
<!-- Unless required by applicable law or agreed to in writing, software -->
10+
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
11+
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
12+
<!-- See the License for the specific language governing permissions and -->
13+
<!-- limitations under the License. -->
14+
15+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
16+
package="com.google.firebase.appcheck.ktx">
17+
<uses-sdk android:minSdkVersion="16"/>
18+
19+
<application>
20+
<service android:name="com.google.firebase.components.ComponentDiscoveryService" android:exported="false">
21+
<meta-data android:name="com.google.firebase.components:com.google.firebase.appcheck.ktx.FirebaseAppCheckKtxRegistrar"
22+
android:value="com.google.firebase.components.ComponentRegistrar" />
23+
</service>
24+
</application>
25+
</manifest>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.appcheck.ktx
16+
17+
import com.google.firebase.FirebaseApp
18+
import com.google.firebase.appcheck.AppCheckToken
19+
import com.google.firebase.appcheck.FirebaseAppCheck
20+
import com.google.firebase.ktx.Firebase
21+
22+
/** Returns the [FirebaseAppCheck] instance of the default [FirebaseApp]. */
23+
val Firebase.appCheck: FirebaseAppCheck
24+
get() = FirebaseAppCheck.getInstance()
25+
26+
/** Returns the [FirebaseAppCheck] instance of a given [FirebaseApp]. */
27+
fun Firebase.appCheck(app: FirebaseApp) = FirebaseAppCheck.getInstance(app)
28+
29+
/**
30+
* Destructuring declaration for [AppCheckToken] to provide token.
31+
*
32+
* @return the token of the [AppCheckToken]
33+
*/
34+
operator fun AppCheckToken.component1() = token
35+
36+
/**
37+
* Destructuring declaration for [AppCheckToken] to provide expireTimeMillis.
38+
*
39+
* @return the expireTimeMillis of the [AppCheckToken]
40+
*/
41+
operator fun AppCheckToken.component2() = expireTimeMillis

subprojects.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ appcheck:firebase-appcheck-interop
55
appcheck:firebase-appcheck-playintegrity
66
appcheck:firebase-appcheck-safetynet
77
appcheck:firebase-appcheck
8+
appcheck:firebase-appcheck:ktx
89

910
firebase-abt
1011
firebase-annotations

0 commit comments

Comments
 (0)