From 5d78074dd94f069c0e20fcb710dabc81d2cd3534 Mon Sep 17 00:00:00 2001 From: Ellet Date: Wed, 15 Nov 2023 02:38:13 +0300 Subject: [PATCH] Version 0.0.4-dev --- .github/workflows/main.yml | 15 ++ CHANGELOG.md | 11 +- README.md | 10 +- example/build.gradle.kts | 1 - library/build.gradle.kts | 3 +- .../firebase_app_check/FirebaseAppCheck.kt | 4 - .../FirebaseAppCheckPluginConfiguration.kt | 5 +- .../core/FirebaseAppCheckSecureStrategy.kt | 2 + .../FirebaseAppCheckTokenVerifierService.kt | 2 +- .../extensions/ApplicationCallExtensions.kt | 40 +--- .../firebase_app_check/ApplicationTest.kt | 203 ++++++++++++++++++ ...irebaseAppCheckTokenVerifierServiceMock.kt | 5 +- .../firebase_app_check/TestConstants.kt | 5 +- .../firebase_app_check/ApplicationTest.kt | 90 -------- 14 files changed, 251 insertions(+), 145 deletions(-) create mode 100644 .github/workflows/main.yml create mode 100644 library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt rename library/src/test/{net => kotlin}/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt (95%) rename library/src/test/{net => kotlin}/freshplatform/ktor_server/firebase_app_check/TestConstants.kt (83%) delete mode 100644 library/src/test/net/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..1b85d91 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,15 @@ +name: CI + +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Setup JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'adopt' + - name: Build with Gradle + run: ./gradlew build diff --git a/CHANGELOG.md b/CHANGELOG.md index e7b7f08..583bd97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,13 @@ -## [next] +# Changelog + +All notable changes to this project will be documented in this file. + +[//]: # (## [next]) + +## 0.0.4-dev +* The library is now dev state +* Improve the tests +* Fix typos ## 0.0.3-experimental * **Breaking Change**: Now you don't need to pass the configuration class as a value, just add the properties directly diff --git a/README.md b/README.md index 5709dd0..7508398 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Firebase App Check for Ktor server -AN **experimental** Ktor server plugin for configuring [Firebase App Check](https://firebase.google.com/products/app-check) easily and with simplicity. +A Ktor server plugin for configuring [Firebase App Check](https://firebase.google.com/products/app-check) easily and with simplicity. It is **not affiliated** with Firebase or Google and may not be suitable for production use **yet**. [//]: # (Note: this repository name might be changed to [ktor-server-guardian](https://github.com/freshtechtips/ktor-server-guardian)) @@ -8,7 +8,7 @@ It is **not affiliated** with Firebase or Google and may not be suitable for pro feel free to share your opinion in the discussions [![](https://jitpack.io/v/freshtechtips/ktor-server-firebase-app-check.svg)](https://jitpack.io/#freshtechtips/ktor-server-firebase-app-check) -[![Build Status](https://travis-ci.org/freshtechtips/ktor-server-firebase-app-check.svg?branch=master)](https://travis-ci.org/freshtechtips/ktor-server-firebase-app-check) +[![Build Status](https://travis-ci.org/freshtechtips/ktor-server-firebase-app-check.svg?branch=main)](https://travis-ci.org/freshtechtips/ktor-server-firebase-app-check) ## Table of Contents @@ -57,7 +57,7 @@ Use this section to describe how to install your project. For example: 3. Add the dependency: ```groovy dependencies { - implementation("com.github.freshtechtips:ktor-server-firebase-app-check:v0.0.3-experimental") // use the latest version above + implementation("com.github.freshtechtips:ktor-server-firebase-app-check:0.0.4-alpha") // use the latest version above } ``` @@ -87,7 +87,7 @@ Pass the following environment variables, ``` By default, the plugin runs the app check only when the development is false. -You can override this bypass `overrideIsShouldVerifyToken = true` in the configuration +You can override this bypass `isShouldVerifyToken = true` in the configuration ## Usage @@ -141,7 +141,7 @@ eyJraWQiOiJ2Yy1sVEEiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxOjgwMjA4OTE ## Features List the key features of the library -please notice the library is still **experimental** +please notice the library is still **Alpha** ```markdown ## Features diff --git a/example/build.gradle.kts b/example/build.gradle.kts index 02b75e2..e9c4e68 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -47,5 +47,4 @@ dependencies { testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion") implementation(project(":library")) -// implementation("com.github.freshtechtips:ktor-server-firebase-app-check:v0.0.3-experimental") } diff --git a/library/build.gradle.kts b/library/build.gradle.kts index 699dc8b..4b6e977 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -11,9 +11,8 @@ plugins { } group = "net.freshplatform.ktor_server.firebase_app_check" -version = "0.0.3-experimental" +version = "0.0.4-dev" description = "A Ktor server plugin for configuring Firebase App Check easily and with simplicity. It is not affiliated with Firebase or Google and may not be suitable for production use yet." -extra["experimental"] = true application { mainClass.set("${group}.FirebaseAppCheckKt") diff --git a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheck.kt b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheck.kt index 243d4b4..74d559a 100644 --- a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheck.kt +++ b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheck.kt @@ -9,10 +9,6 @@ import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppChec import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppCheckTokenVerifierServiceImpl import net.freshplatform.ktor_server.firebase_app_check.utils.extensions.verifyAppTokenRequest -val firebaseAppCheckTokenVerifierService: FirebaseAppCheckTokenVerifierService by lazy { - FirebaseAppCheckTokenVerifierServiceImpl() -} - /** * A Ktor server plugin for configuring Firebase App Check easily and with simplicity. * It is not affiliated with Firebase or Google and may not be suitable for production use yet. diff --git a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckPluginConfiguration.kt b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckPluginConfiguration.kt index 20ae60f..c8a0658 100644 --- a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckPluginConfiguration.kt +++ b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckPluginConfiguration.kt @@ -10,6 +10,8 @@ import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCh import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtErrorType import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtErrorType.* import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtException +import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppCheckTokenVerifierService +import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppCheckTokenVerifierServiceImpl import net.freshplatform.ktor_server.firebase_app_check.utils.FirebaseAppCheckMessages /** @@ -173,7 +175,8 @@ class FirebaseAppCheckPluginConfiguration( FirebaseAppCheckMessages( pluginConfiguration = it ) - } + }, + var serviceImpl: FirebaseAppCheckTokenVerifierService = FirebaseAppCheckTokenVerifierServiceImpl(), ) { /** diff --git a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckSecureStrategy.kt b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckSecureStrategy.kt index e100118..d6c0225 100644 --- a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckSecureStrategy.kt +++ b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/core/FirebaseAppCheckSecureStrategy.kt @@ -1,5 +1,7 @@ package net.freshplatform.ktor_server.firebase_app_check.core +import net.freshplatform.ktor_server.firebase_app_check.utils.extensions.protectRouteWithAppCheck + /** * A sealed class that defines different strategies for securing routes with Firebase App Check. * if you want to secure the whole app use [FirebaseAppCheckSecureStrategy.ProtectAll] for all the requests diff --git a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/services/FirebaseAppCheckTokenVerifierService.kt b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/services/FirebaseAppCheckTokenVerifierService.kt index ea592b8..99bdbb1 100644 --- a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/services/FirebaseAppCheckTokenVerifierService.kt +++ b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/services/FirebaseAppCheckTokenVerifierService.kt @@ -49,7 +49,7 @@ data class FetchFirebaseAppCheckPublicKeyRateLimitedConfig( */ interface FirebaseAppCheckTokenVerifierService { /** - * Suspended function to fetch a Firebase App Check public key. + * Suspended function to fetch a Firebase App Check a public key. * * @param jwtString to get the kid which is the Key ID. * @param url The URL for fetching the public key. diff --git a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/utils/extensions/ApplicationCallExtensions.kt b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/utils/extensions/ApplicationCallExtensions.kt index a228d1b..f23c414 100644 --- a/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/utils/extensions/ApplicationCallExtensions.kt +++ b/library/src/main/kotlin/net/freshplatform/ktor_server/firebase_app_check/utils/extensions/ApplicationCallExtensions.kt @@ -7,7 +7,6 @@ import io.ktor.server.response.* import io.ktor.server.routing.* import net.freshplatform.ktor_server.firebase_app_check.FirebaseAppCheckPlugin import net.freshplatform.ktor_server.firebase_app_check.core.FirebaseAppCheckSecureStrategy -import net.freshplatform.ktor_server.firebase_app_check.firebaseAppCheckTokenVerifierService import net.freshplatform.ktor_server.firebase_app_check.services.FetchFirebaseAppCheckPublicKeyConfig /** @@ -39,12 +38,12 @@ suspend fun ApplicationCall.verifyAppTokenRequest() { try { - val publicKey = firebaseAppCheckTokenVerifierService.fetchFirebaseAppCheckPublicKey( + val publicKey = pluginConfig.serviceImpl.fetchFirebaseAppCheckPublicKey( jwtString = firebaseAppCheckToken, url = pluginConfig.firebaseAppCheckPublicJwtSetUrl, config = FetchFirebaseAppCheckPublicKeyConfig() ) - val verifiedJwt = firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( + val verifiedJwt = pluginConfig.serviceImpl.verifyFirebaseAppCheckToken( firebaseProjectId = pluginConfig.firebaseProjectId, firebaseProjectNumber = pluginConfig.firebaseProjectNumber, jwtString = firebaseAppCheckToken, @@ -110,18 +109,10 @@ fun Route.protectRouteWithAppCheck( val configuration = application.plugin(FirebaseAppCheckPlugin).config val protectedRoute = createChild(ProtectedRouteSelector()) -// var isRouteProtected = protectedRoute.attributes.getOrNull(isProtectedRouteKey) -// if (isRouteProtected == null) { -// protectedRoute.attributes.put(isProtectedRouteKey, true) -// isRouteProtected = true -// } val isShouldVerifyToken = configuration.isShouldVerifyToken(environment) if (isShouldVerifyToken) { protectedRoute.intercept(ApplicationCallPipeline.Call) { _ -> -// if (!isRouteProtected) { -// return@intercept -// } call.verifyAppTokenRequest() } } @@ -134,29 +125,4 @@ class ProtectedRouteSelector : RouteSelector() { } override fun toString(): String = "protected" -} - -//class UnProtectedRouteSelector : RouteSelector() { -// override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation { -// return RouteSelectorEvaluation.Transparent -// } -// -// override fun toString(): String = "unprotected" -//} - -///** -// * The plugin secure strategy need to be configured with [FirebaseAppCheckSecureStrategy.ProtectSpecificRoutes] -// * This will only unprotect a route that is protected -// * */ -//fun Route.unProtectRouteWithAppCheck( -// build: Route.() -> Route, -//) { -// application.plugin(FirebaseAppCheckPlugin).config -// -// val unProtectedRoute = createChild(UnProtectedRouteSelector()) -// unProtectedRoute.attributes.put( -// isProtectedRouteKey, -// false -// ) -// unProtectedRoute.build() -//} \ No newline at end of file +} \ No newline at end of file diff --git a/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt b/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt new file mode 100644 index 0000000..6b88a62 --- /dev/null +++ b/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt @@ -0,0 +1,203 @@ +package freshplatform.ktor_server.firebase_app_check + +import io.ktor.client.request.* +import io.ktor.client.statement.* +import io.ktor.http.* +import io.ktor.server.application.* +import io.ktor.server.response.* +import io.ktor.server.routing.* +import io.ktor.server.testing.* +import net.freshplatform.ktor_server.firebase_app_check.FirebaseAppCheckPlugin +import net.freshplatform.ktor_server.firebase_app_check.core.FirebaseAppCheckPluginConfiguration +import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtErrorType +import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtException +import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppCheckTokenVerifierService +import net.freshplatform.ktor_server.firebase_app_check.utils.FirebaseAppCheckMessages +import net.freshplatform.ktor_server.firebase_app_check.utils.extensions.protectRouteWithAppCheck +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.fail + +val firebaseAppCheckTokenVerifierService: FirebaseAppCheckTokenVerifierService by lazy { + FirebaseAppCheckTokenVerifierServiceMock() +} + +class ApplicationTest { + @Test + fun testRoot() = testApplication { + val pluginConfiguration = FirebaseAppCheckPluginConfiguration( + firebaseProjectNumber = TestConstants.FIREBASE_PROJECT_NUMBER, + firebaseProjectId = TestConstants.FIREBASE_PROJECT_ID, + serviceImpl = firebaseAppCheckTokenVerifierService, + isShouldVerifyToken = true + ) + val messages = FirebaseAppCheckMessages( + pluginConfiguration = pluginConfiguration, + ) + install(FirebaseAppCheckPlugin) { + firebaseProjectId = pluginConfiguration.firebaseProjectId + firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber + serviceImpl = pluginConfiguration.serviceImpl + isShouldVerifyToken = pluginConfiguration.isShouldVerifyToken + } + routing { + get("/") { + call.respondText( + text = TestConstants.APP_CHECK_NOT_REQUIRED_MSG, + ) + } + protectRouteWithAppCheck { + route("/products") { + get("/1") { + call.respondText( + text = TestConstants.APP_CHECK_REQUIRED_MSG, + ) + } + get("/2") { + call.respondText( + text = TestConstants.APP_CHECK_REQUIRED_MSG, + ) + } + } + } + route("/products") { + get("/3") { + call.respondText( + text = TestConstants.APP_CHECK_NOT_REQUIRED_MSG, + ) + } + } + get("/test") { + call.respondText( + text = TestConstants.APP_CHECK_NOT_REQUIRED_MSG, + ) + } + protectRouteWithAppCheck { + post("/test") { + call.respondText( + text = TestConstants.APP_CHECK_REQUIRED_MSG, + ) + } + } + } + val jwtString = TestConstants.TOKEN_OF_THE_PROJECT + val publicKey = firebaseAppCheckTokenVerifierService.fetchFirebaseAppCheckPublicKey( + jwtString = jwtString, + url = pluginConfiguration.firebaseAppCheckPublicJwtSetUrl + ) + try { + firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( + jwtString = jwtString, + publicKey = publicKey, + firebaseProjectId = pluginConfiguration.firebaseProjectId, + firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber, + issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl + ) + } catch (e: Exception) { + fail("Test failed while verify the firebase app check token: $e") + } + + + val verifiedJwtWithDifferentProjectId = assertFailsWith { + firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( + jwtString = jwtString, + publicKey = publicKey, + firebaseProjectId = "myapp-eb212", + firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber, + issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl + ) + } + assertEquals( + FirebaseAppCheckVerifyJwtErrorType.GenericJwtVerificationError, + verifiedJwtWithDifferentProjectId.errorType + ) + + val verifiedJwtWithDifferentProjectNumber = assertFailsWith { + firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( + jwtString = jwtString, + publicKey = publicKey, + firebaseProjectId = pluginConfiguration.firebaseProjectId, + firebaseProjectNumber = "32132312123", + issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl + ) + } + assertEquals( + FirebaseAppCheckVerifyJwtErrorType.GenericJwtVerificationError, + verifiedJwtWithDifferentProjectNumber.errorType + ) + + val invalidJwtException = assertFailsWith { + firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( + jwtString = "eyInvalidJwt", + publicKey = publicKey, + firebaseProjectId = pluginConfiguration.firebaseProjectId, + firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber, + issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl + ) + } + + assertEquals(FirebaseAppCheckVerifyJwtErrorType.TokenIsNotValid, invalidJwtException.errorType) + + client.get("/").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(TestConstants.APP_CHECK_NOT_REQUIRED_MSG, bodyAsText()) + } + + (1..2).forEach { productNumber -> + client.get("/products/${productNumber}").apply { + assertEquals(HttpStatusCode.Unauthorized, status) + assertEquals(messages.appCheckIsNotDefinedResponse, bodyAsText()) + } + client.get("/products/${productNumber}") { + headers { + header(pluginConfiguration.firebaseAppCheckHeaderName, "Bearer ${TestConstants.TOKEN_OF_THE_PROJECT}") + } + }.apply { + assertEquals(HttpStatusCode.Unauthorized, status) + assertEquals(messages.tokenIsNotValidResponse, bodyAsText()) + } + + client.get("/products/${productNumber}") { + headers { + header(pluginConfiguration.firebaseAppCheckHeaderName, TestConstants.TOKEN_OF_THE_PROJECT) + } + }.apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(TestConstants.APP_CHECK_REQUIRED_MSG, bodyAsText()) + } + } + client.get("/products/3").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(TestConstants.APP_CHECK_NOT_REQUIRED_MSG, bodyAsText()) + } + + client.get("/test").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(TestConstants.APP_CHECK_NOT_REQUIRED_MSG, bodyAsText()) + } + + client.post("/test").apply { + assertEquals(HttpStatusCode.Unauthorized, status) + assertEquals(messages.appCheckIsNotDefinedResponse, bodyAsText()) + } + + client.post("/test") { + headers { + header(pluginConfiguration.firebaseAppCheckHeaderName, "Bearer ${TestConstants.TOKEN_OF_THE_PROJECT}") + } + }.apply { + assertEquals(HttpStatusCode.Unauthorized, status) + assertEquals(messages.tokenIsNotValidResponse, bodyAsText()) + } + + client.post("/test") { + headers { + header(pluginConfiguration.firebaseAppCheckHeaderName, TestConstants.TOKEN_OF_THE_PROJECT) + } + }.apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(TestConstants.APP_CHECK_REQUIRED_MSG, bodyAsText()) + } + } +} diff --git a/library/src/test/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt b/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt similarity index 95% rename from library/src/test/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt rename to library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt index a3d698e..0482829 100644 --- a/library/src/test/net/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt +++ b/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/FirebaseAppCheckTokenVerifierServiceMock.kt @@ -1,4 +1,4 @@ -package net.freshplatform.ktor_server.firebase_app_check +package freshplatform.ktor_server.firebase_app_check import com.auth0.jwt.JWT import com.auth0.jwt.exceptions.JWTDecodeException @@ -11,7 +11,7 @@ import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppChec import java.security.PublicKey import kotlin.time.Duration.Companion.milliseconds -class PublicKeyMock : PublicKey { +private class PublicKeyMock : PublicKey { override fun getAlgorithm(): String { return "RS256" } @@ -64,6 +64,7 @@ class FirebaseAppCheckTokenVerifierServiceMock : FirebaseAppCheckTokenVerifierSe } return verified } catch (e: JWTDecodeException) { + println("Fuck: $e") throw FirebaseAppCheckVerifyJwtException( "Token is not valid: $e", errorType = FirebaseAppCheckVerifyJwtErrorType.TokenIsNotValid diff --git a/library/src/test/net/freshplatform/ktor_server/firebase_app_check/TestConstants.kt b/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/TestConstants.kt similarity index 83% rename from library/src/test/net/freshplatform/ktor_server/firebase_app_check/TestConstants.kt rename to library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/TestConstants.kt index 37ca52b..611e099 100644 --- a/library/src/test/net/freshplatform/ktor_server/firebase_app_check/TestConstants.kt +++ b/library/src/test/kotlin/freshplatform/ktor_server/firebase_app_check/TestConstants.kt @@ -1,7 +1,10 @@ -package net.freshplatform.ktor_server.firebase_app_check +package freshplatform.ktor_server.firebase_app_check object TestConstants { const val FIREBASE_PROJECT_ID = "mynotes-eb717" const val FIREBASE_PROJECT_NUMBER = "802089142559" const val TOKEN_OF_THE_PROJECT = "eyJraWQiOiJ2Yy1sVEEiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxOjgwMjA4OTE0MjU1OTphbmRyb2lkOjI2ZDhjMDA3ZGVkMDNmODQyYTg4MmEiLCJhdWQiOlsicHJvamVjdHNcLzgwMjA4OTE0MjU1OSIsInByb2plY3RzXC9teW5vdGVzLWViNzE3Il0sInByb3ZpZGVyIjoiZGVidWciLCJpc3MiOiJodHRwczpcL1wvZmlyZWJhc2VhcHBjaGVjay5nb29nbGVhcGlzLmNvbVwvODAyMDg5MTQyNTU5IiwiZXhwIjoxNjk3MTM0NDg3LCJpYXQiOjE2OTcxMzA4ODcsImp0aSI6InZLZERfNTRhQ2tzVmpHV0xBN3d1TjZmWlFUQWRYZzRBWGJhYVBzRUZDV0EifQ.H_LGsCe5I-Z2uAgYU1isDmxQ-6PecdmjEqvkrZp9AWthNhsiMdlVYjUe2DaSmt3lhIlwCJyCh2YooOLvSlFAvdx5n__kB5O5C9Fw-Vw-zjSTOAi6lNB0hi8OEkIJhNgw2b_UipeVFd1I6ICkCdV93Ewr-clv-eDeMIg_b8vr3w6HtypZDVu3hAl6BjfxY9r7cm5eBmHGnOxwb1-flSKRJdBmrh4Bm0_imaDPSHw_rUwCUXHOAM-QfdQ-D4C15L_IJH4X6kT7nm8GMj47rQjr1d6CQZbW3xoIsTJvnpreOR1xyiHZiLydj1cwPt6r2DfmjRL6-tFs2u8c72CcoqQ4hhsJE9ZSk1BHXpnGw6t5PLPWmk-K7wCrn49U20SYsbOGzyMmwPs-nRyYL3QeV00brlaQWFN7pnjquYHtgJZgkVZlIe1Hh_8mBzTSLygc3-0Xw3FKf1X6p_jOyyN7Qi3Wf5GHvBdp_sYyuBtXMYVwhKQ56lYBX3waLP0KHSiDiDUW" + + const val APP_CHECK_REQUIRED_MSG = "App Check for this route is required." + const val APP_CHECK_NOT_REQUIRED_MSG = "This route doesn't requires App Check" } \ No newline at end of file diff --git a/library/src/test/net/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt b/library/src/test/net/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt deleted file mode 100644 index c250466..0000000 --- a/library/src/test/net/freshplatform/ktor_server/firebase_app_check/ApplicationTest.kt +++ /dev/null @@ -1,90 +0,0 @@ -package net.freshplatform.ktor_server.firebase_app_check - -import io.ktor.server.application.* -import io.ktor.server.routing.* -import io.ktor.server.testing.* -import net.freshplatform.ktor_server.firebase_app_check.core.FirebaseAppCheckPlugin -import net.freshplatform.ktor_server.firebase_app_check.core.FirebaseAppCheckPluginConfiguration -import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtErrorType -import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtException -import net.freshplatform.ktor_server.firebase_app_check.services.FirebaseAppCheckTokenVerifierService -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.fail - -val firebaseAppCheckTokenVerifierService: FirebaseAppCheckTokenVerifierService by lazy { - FirebaseAppCheckTokenVerifierServiceMock() -} - -class ApplicationTest { - @Test - fun testRoot() = testApplication { - val pluginConfiguration = FirebaseAppCheckPluginConfiguration( - firebaseProjectNumber = TestConstants.FIREBASE_PROJECT_NUMBER, - firebaseProjectId = TestConstants.FIREBASE_PROJECT_ID - ) - application { - install(Routing) - install(FirebaseAppCheckPlugin) { - configuration = pluginConfiguration - } - } - val jwtString = TestConstants.TOKEN_OF_THE_PROJECT - val publicKey = firebaseAppCheckTokenVerifierService.fetchFirebaseAppCheckPublicKey( - jwtString = jwtString, - url = pluginConfiguration.firebaseAppCheckPublicJwtSetUrl - ) - try { - firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( - jwtString = jwtString, - publicKey = publicKey, - firebaseProjectId = pluginConfiguration.firebaseProjectId, - firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber, - issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl - ) - } catch (e: Exception) { - fail("Test failed while verify the firebase app check token: $e") - } - - - val verifiedJwtWithDifferentProjectId = assertFailsWith { - firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( - jwtString = jwtString, - publicKey = publicKey, - firebaseProjectId = "myapp-eb212", - firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber, - issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl - ) - } - assertEquals(FirebaseAppCheckVerifyJwtErrorType.GenericJwtVerificationError, verifiedJwtWithDifferentProjectId.errorType) - - val verifiedJwtWithDifferentProjectNumber = assertFailsWith { - firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( - jwtString = jwtString, - publicKey = publicKey, - firebaseProjectId = pluginConfiguration.firebaseProjectId, - firebaseProjectNumber = "32132312123", - issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl - ) - } - assertEquals(FirebaseAppCheckVerifyJwtErrorType.GenericJwtVerificationError, verifiedJwtWithDifferentProjectNumber.errorType) - - val invalidJwtException = assertFailsWith { - firebaseAppCheckTokenVerifierService.verifyFirebaseAppCheckToken( - jwtString = "eyInvalidJwt", - publicKey = publicKey, - firebaseProjectId = pluginConfiguration.firebaseProjectId, - firebaseProjectNumber = pluginConfiguration.firebaseProjectNumber, - issuerBaseUrl = pluginConfiguration.firebaseAppCheckApiBaseUrl - ) - } - - assertEquals(FirebaseAppCheckVerifyJwtErrorType.TokenIsNotValid, invalidJwtException.errorType) - -// client.get("/").apply { -// assertEquals(HttpStatusCode.OK, status) -// assertEquals("Hello World!", bodyAsText()) -// } - } -}