Skip to content

Commit

Permalink
Version 0.0.4-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellet committed Nov 14, 2023
1 parent 9eb4174 commit 5d78074
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 145 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 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))

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

Expand Down Expand Up @@ -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
}
```
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
3 changes: 1 addition & 2 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -173,7 +175,8 @@ class FirebaseAppCheckPluginConfiguration(
FirebaseAppCheckMessages(
pluginConfiguration = it
)
}
},
var serviceImpl: FirebaseAppCheckTokenVerifierService = FirebaseAppCheckTokenVerifierServiceImpl(),
) {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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()
//}
}
Loading

0 comments on commit 5d78074

Please sign in to comment.